Ejemplo n.º 1
0
    def __init__(self, infile, encoding='utf_8'):
        """Add all strings and fix double escaped strings."""
        # Initialize the dictionary
        super(LangStrings, self).__init__()

        # Get the path to the given file
        self._mainfile = TRANSLATION_PATH.joinpath(infile + '.ini')

        # Does the file exist?
        if not self._mainfile.isfile():

            # Raise an error
            raise FileNotFoundError(
                'No file found at {0}'.format(self._mainfile))

        # Get the path to the server specific file
        self._serverfile = self._mainfile.parent.joinpath(
            self._mainfile.namebase + '_server.ini')

        # Get the strings from the main file
        main_strings = ConfigObj(self._mainfile, encoding=encoding)

        # Does the server specific file exist?
        if not self._serverfile.isfile() and not infile.startswith('_core/'):

            # Create the server specific file
            self._create_server_file()

        # Otherwise
        else:

            # Get any strings from the server specific file
            server_strings = ConfigObj(self._serverfile, encoding=encoding)

            # Merge the two ConfigObj instances together
            main_strings.merge(server_strings)

        # Loop through all strings
        for key in main_strings:

            # Is the current string not a Section?
            if not isinstance(main_strings[key], Section):

                # No need to go further
                continue

            # Get a TranslationStrings instance for the current string
            translation_strings = TranslationStrings()

            # Loop through all languages for the current string
            for lang in main_strings[key]:

                # Get the shortname of the current language
                language = language_manager.get_language(lang)

                # Was the language found?
                if language is None:

                    # Do not add this translation
                    # Possibly raise an error silently here
                    continue

                # Get the language's string and fix any escaped strings
                translation_strings[
                    language] = self._replace_escaped_sequences(
                    main_strings[key][lang])

            # Add the TranslationStrings instance for the current string
            self[key] = translation_strings

        # Is there any default language specified into the main file?
        if 'DEFAULT_LANGUAGE' in main_strings:

            # Get the default language
            default_language = main_strings['DEFAULT_LANGUAGE']

            # Make sure it is not a Section
            if not isinstance(default_language, Section):

                # Get the given language code
                language_code = language_manager.get_language(default_language)

                # Is the language valid?
                if language_code is not None:

                    # Set the default language
                    self.default_language = language_code

                # Delete the key from the main file as we are done with it
                del main_strings['DEFAULT_LANGUAGE']
Ejemplo n.º 2
0
#   Core
from core import SOURCE_ENGINE
#   Paths
from paths import TRANSLATION_PATH
#   Translations
from translations.strings import LangStrings

# Script Imports
from victim_stats.info import info


# =============================================================================
# >> SUPPORT VERIFICATION
# =============================================================================
# Verify that the engine is supported
if not TRANSLATION_PATH.joinpath(
        info.basename, '{0}.ini'.format(SOURCE_ENGINE)).isfile():
    raise NotImplementedError(
        'Engine "{0}" not supported'.format(SOURCE_ENGINE))


# =============================================================================
# >> GLOBAL VARIABLES
# =============================================================================
# Get the translations
victim_stats_strings = LangStrings('{0}/strings'.format(info.basename))

# Merge in the engine specific translations
victim_stats_strings.update(
    LangStrings('{0}/{1}'.format(info.basename, SOURCE_ENGINE)))

# Get the hitgroup translations