Esempio n. 1
0
    def get_language(self, language):
        """Return the language to be used."""
        # Get the given language's shortname
        language = language_manager.get_language(language)

        # Was a language found?
        if language is not None and language in self:

            # Return the language
            return language

        # Is the server's default language in the dictionary?
        if language_manager.default in self:

            # Return the server's default language
            return language_manager.default

        # Is there any default language defined?
        if hasattr(self, '_default_language'):

            # Is the default language available for that string?
            if self._default_language in self:

                # Return the default language to use
                return self._default_language

        # Is the server's fallback language in the dictionary?
        if language_manager.fallback in self:

            # Return the server's fallback language
            return language_manager.fallback

        # Return None as the language, as no language has been found
        return None
Esempio n. 2
0
    def get_language(self, language):
        """Return the language to be used."""
        # Get the given language's shortname
        language = language_manager.get_language(language)

        # Was a language found?
        if language is not None and language in self:

            # Return the language
            return language

        # Is the server's default language in the dictionary?
        if language_manager.default in self:

            # Return the server's default language
            return language_manager.default

        # Is there any default language defined?
        if hasattr(self, '_default_language'):

            # Is the default language available for that string?
            if self._default_language in self:

                # Return the default language to use
                return self._default_language

        # Is the server's fallback language in the dictionary?
        if language_manager.fallback in self:

            # Return the server's fallback language
            return language_manager.fallback

        # Return None as the language, as no language has been found
        return None
Esempio n. 3
0
    def __setattr__(self, attribute, value):
        """Register the default language."""
        # Is the given attribute the default language?
        if attribute == 'default_language':

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

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

                # Loop through all strings
                for key in self:

                    # Set the default language to use for that string
                    self[key]._default_language = language_code

                # Override the given value
                value = language_code

        # Set the attribute
        super().__setattr__(attribute, value)
Esempio n. 4
0
    def __setattr__(self, attribute, value):
        """Register the default language."""
        # Is the given attribute the default language?
        if attribute == 'default_language':

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

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

                # Loop through all strings
                for key in self:

                    # Set the default language to use for that string
                    self[key]._default_language = language_code

                # Override the given value
                value = language_code

        # Set the attribute
        super().__setattr__(attribute, value)
Esempio n. 5
0
    def __init__(self, infile, encoding='utf_8'):
        """Add all strings and fix double escaped strings."""
        # Initialize the dictionary
        super().__init__()

        # Get the path to the given file
        self._mainfile = TRANSLATION_PATH / 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 / '{0}_server.ini'.format(
            self._mainfile.namebase)

        # Get the strings from the main file
        main_strings = GameConfigObj(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 = GameConfigObj(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']
Esempio n. 6
0
    def __init__(self, infile, encoding='utf_8'):
        """Add all strings and fix double escaped strings."""
        # Initialize the dictionary
        super().__init__()

        # Get the path to the given file
        self._mainfile = TRANSLATION_PATH / 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 / '{0}_server.ini'.format(
            self._mainfile.namebase)

        # Get the strings from the main file
        main_strings = GameConfigObj(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 = GameConfigObj(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']