Exemplo n.º 1
0
    def test_replace_params(self):
        self.assertEquals(
            parser.replace_params("%-3.3lld of {5} %$ {x} %d%%%d %"),
            ("{0} of {1} %$ {x} {2}%%{3} %", ["%-3.3lld", "{5}", "%d", "%d"]))

        self.assertEquals(parser.replace_params("{0}% complete"),
                          ("{0}% complete", ["{0}"]))
Exemplo n.º 2
0
def _restore_params_to_translation_dict(native_strings, translation_dict):
    """Strings stored in the translation files are filtered so that all
    format placeholders (e.g. "%d", "{0}") are replaced with incrementing
    placeholders "{0}", "{1}", etc. This allows us to use the same string with
    different formats across multiple platforms but only send one string for
    translation. See burton.parser.util.replace_params for more information on
    how strings are filtered.

    However, strings in the database are stored unchanged. This allows us to
    map the strings from the translation file to the original strings. We back
    into this by performing the same filtering on a copy of the original
    string and then searching the translation file for that string.

    This function modifies the translation dictionary passed into it in-place
    and then returns it.
    """
    for key in native_strings:
        replaced_key, replaced_params = parser.replace_params(key)
        if replaced_key in translation_dict and len(replaced_params) > 0 \
          and translation_dict[replaced_key] is not None \
          and replaced_key != key:
            translation_dict[key] = parser.restore_platform_specific_params(
                translation_dict[replaced_key],
                replaced_params,
            )

    return translation_dict
Exemplo n.º 3
0
def _remove_unused_strings_from_language_file(
    translation_keys,
    translation_dict,
    translation_file,
    filename
):
    replaced_translation_keys = []
    strings_to_remove         = []

    for key in translation_keys:
        replaced_key, replaced_params = parser.replace_params(key)
        replaced_translation_keys.append(replaced_key)

    for key in translation_dict:
        if key is not None and key not in replaced_translation_keys and \
          translation_dict[key] is None:
            strings_to_remove.append(key)

    if len(strings_to_remove) > 0:
        logger = logging.getLogger(logger_name)
        logger.info(
            "The following unused, untranslated strings were removed from " +
                filename
        )
        logger.info("\t" +  "\n\t".join(strings_to_remove) + "\n")

    for string in strings_to_remove:
        translation_file.delete_translation(string)
Exemplo n.º 4
0
def _restore_params_to_translation_dict(native_strings, translation_dict):
    """Strings stored in the translation files are filtered so that all
    format placeholders (e.g. "%d", "{0}") are replaced with incrementing
    placeholders "{0}", "{1}", etc. This allows us to use the same string with
    different formats across multiple platforms but only send one string for
    translation. See burton.parser.util.replace_params for more information on
    how strings are filtered.

    However, strings in the database are stored unchanged. This allows us to
    map the strings from the translation file to the original strings. We back
    into this by performing the same filtering on a copy of the original
    string and then searching the translation file for that string.

    This function modifies the translation dictionary passed into it in-place
    and then returns it.
    """
    for key in native_strings:
        replaced_key, replaced_params = parser.replace_params(key)
        if replaced_key in translation_dict and len(replaced_params) > 0 \
          and translation_dict[replaced_key] is not None \
          and replaced_key != key:
            translation_dict[key] = parser.restore_platform_specific_params(
                translation_dict[replaced_key],
                replaced_params,
            )

    return translation_dict
Exemplo n.º 5
0
def _remove_unused_strings_from_language_file(
    translation_keys,
    translation_dict,
    translation_file,
    filename
):
    replaced_translation_keys = []
    strings_to_remove         = []

    for key in translation_keys:
        replaced_key, replaced_params = parser.replace_params(key)
        replaced_translation_keys.append(replaced_key)

    for key in translation_dict:
        if key is not None and key not in replaced_translation_keys and \
          translation_dict[key] is None:
            strings_to_remove.append(key)

    if len(strings_to_remove) > 0:
        logger = logging.getLogger(logger_name)
        logger.info(
            "The following unused, untranslated strings were removed from " +
                filename
        )
        logger.info("\t" +  "\n\t".join(strings_to_remove) + "\n")

    for string in strings_to_remove:
        translation_file.delete_translation(string)
Exemplo n.º 6
0
def _add_new_keys_to_translation_file(translation_keys, translation_dict,
                                      translation_file):
    replaced_translation_keys = []
    for key in translation_keys:
        filtered_string, replaced_params = parser.replace_params(key)
        replaced_translation_keys.append(filtered_string)

    translation_keys = replaced_translation_keys

    for translation_key in translation_keys:
        if translation_key not in translation_dict:
            translation_file.add_translation(translation_key, None)
Exemplo n.º 7
0
def _add_new_keys_to_translation_file(
    translation_keys,
    translation_dict,
    translation_file
):
    replaced_translation_keys = [ ]
    for key in translation_keys:
        filtered_string, replaced_params = parser.replace_params(key)
        replaced_translation_keys.append(filtered_string)

    translation_keys = replaced_translation_keys

    for translation_key in translation_keys:
        if translation_key not in translation_dict:
            translation_file.add_translation(translation_key, None)