Ejemplo n.º 1
0
def load_translations(app):
    env = app.env
    # (try to) load string translations
    locale_dirs = [os.path.join(env.srcdir, x) for x in env.config.locale_dirs]
    print("loading '%s' translations [%s]..." %
          (_CATALOG, env.config.language))
    locale.init(locale_dirs, env.config.language, _CATALOG)
Ejemplo n.º 2
0
    def _init_i18n(self):
        # type: () -> None
        """Load translated strings from the configured localedirs if enabled in
        the configuration.
        """
        if self.config.language is None:
            self.translator, has_translation = locale.init([], None)
        else:
            logger.info(bold(
                __('loading translations [%s]... ') % self.config.language),
                        nonl=True)

            # compile mo files if sphinx.po file in user locale directories are updated
            repo = CatalogRepository(self.srcdir, self.config.locale_dirs,
                                     self.config.language,
                                     self.config.source_encoding)
            for catalog in repo.catalogs:
                if catalog.domain == 'sphinx' and catalog.is_outdated():
                    catalog.write_mo(self.config.language)

            locale_dirs = [None, path.join(package_dir, 'locale')] + list(
                repo.locale_dirs)
            self.translator, has_translation = locale.init(
                locale_dirs, self.config.language)
            if has_translation or self.config.language == 'en':
                # "en" never needs to be translated
                logger.info(__('done'))
            else:
                logger.info(__('not available for built-in messages'))
Ejemplo n.º 3
0
    def add_message_catalog(self, catalog: str, locale_dir: str) -> None:
        """Register a message catalog.

        The *catalog* is a name of catalog, and *locale_dir* is a base path
        of message catalog.  For more details, see
        :func:`sphinx.locale.get_translation()`.

        .. versionadded:: 1.8
        """
        locale.init([locale_dir], self.config.language, catalog)
        locale.init_console(locale_dir, catalog)
Ejemplo n.º 4
0
    def add_message_catalog(self, catalog, locale_dir):
        # type: (str, str) -> None
        """Register a message catalog.

        The *catalog* is a name of catalog, and *locale_dir* is a base path
        of message catalog.  For more details, see
        :func:`sphinx.locale.get_translation()`.

        .. versionadded:: 1.8
        """
        locale.init([locale_dir], self.config.language, catalog)
        locale.init_console(locale_dir, catalog)
Ejemplo n.º 5
0
 def _init_i18n(self):
     # type: () -> None
     """Load translated strings from the configured localedirs if enabled in
     the configuration.
     """
     if self.config.language is not None:
         logger.info(bold('loading translations [%s]... ' % self.config.language),
                     nonl=True)
         user_locale_dirs = [
             path.join(self.srcdir, x) for x in self.config.locale_dirs]
         # compile mo files if sphinx.po file in user locale directories are updated
         for catinfo in find_catalog_source_files(
                 user_locale_dirs, self.config.language, domains=['sphinx'],
                 charset=self.config.source_encoding):
             catinfo.write_mo(self.config.language)
         locale_dirs = [None, path.join(package_dir, 'locale')] + user_locale_dirs
     else:
         locale_dirs = []
     self.translator, has_translation = locale.init(locale_dirs, self.config.language)
     if self.config.language is not None:
         if has_translation or self.config.language == 'en':
             # "en" never needs to be translated
             logger.info(__('done'))
         else:
             logger.info('not available for built-in messages')
Ejemplo n.º 6
0
def test_create_see_index():
    locale.init([], None)

    # type, value, tid, main, index_key
    env = Environment({
        'index': [
            ('see', 'docutils; reStructuredText', 'id1', '', None),
            ('see', 'Python; interpreter', 'id2', '', None),
            ('see', 'Sphinx; documentation tool', 'id3', '', None),
        ],
    })
    index = IndexEntries(env).create_index(dummy_builder)
    assert len(index) == 3
    assert index[0] == (u'D', [(u'docutils', [[], [(u'see reStructuredText', [])], None])])
    assert index[1] == (u'P', [(u'Python', [[], [(u'see interpreter', [])], None])])
    assert index[2] == (u'S', [(u'Sphinx', [[], [(u'see documentation tool', [])], None])])
Ejemplo n.º 7
0
 def _init_i18n(self):
     """Load translated strings from the configured localedirs if enabled in
     the configuration.
     """
     if self.config.language is not None:
         self.info(bold('loading translations [%s]... ' %
                        self.config.language),
                   nonl=True)
         user_locale_dirs = [
             path.join(self.srcdir, x) for x in self.config.locale_dirs
         ]
         # compile mo files if sphinx.po file in user locale directories are updated
         for catinfo in find_catalog_source_files(
                 user_locale_dirs,
                 self.config.language,
                 domains=['sphinx'],
                 charset=self.config.source_encoding):
             catinfo.write_mo(self.config.language)
         locale_dirs = [None, path.join(package_dir, 'locale')
                        ] + user_locale_dirs
     else:
         locale_dirs = []
     self.translator, has_translation = locale.init(locale_dirs,
                                                    self.config.language)
     if self.config.language is not None:
         if has_translation or self.config.language == 'en':
             # "en" never needs to be translated
             self.info('done')
         else:
             self.info('not available for built-in messages')
Ejemplo n.º 8
0
def copy_js_translations(app, exception):
    if exception:
        return

    translator, has_translations = locale.init(
        [os.path.join(app.srcdir, x) for x in app.config.locale_dirs],
        app.config.language, 'theme')
    # Append our translations to the file
    with open(os.path.join(app.builder.outdir, '_static', 'translations.js'),
              'a+t') as f:
        if not has_translations:
            return

        translations = {}
        for (key, translation) in translator._catalog.items():
            if key and key != translation:
                translations[key] = translation

        logging.getLogger(__name__).info(
            'Appending %d JavaScript translations' % len(translations))

        # Append our translations
        f.write('$.extend(Documentation.TRANSLATIONS, ')
        json.dump(translations, f, sort_keys=True)
        f.write(');')
Ejemplo n.º 9
0
def test_init(rootdir):
    # not initialized yet
    _ = locale.get_translation('myext')
    assert _('Hello world') == 'Hello world'
    assert _('Hello sphinx') == 'Hello sphinx'
    assert _('Hello reST') == 'Hello reST'

    # load locale1
    locale.init([rootdir / 'test-locale' / 'locale1'], 'en', 'myext')
    _ = locale.get_translation('myext')
    assert _('Hello world') == 'HELLO WORLD'
    assert _('Hello sphinx') == 'Hello sphinx'
    assert _('Hello reST') == 'Hello reST'

    # load a catalog to unrelated namespace
    locale.init([rootdir / 'test-locale' / 'locale2'], 'en', 'myext', 'mynamespace')
    _ = locale.get_translation('myext')
    assert _('Hello world') == 'HELLO WORLD'
    assert _('Hello sphinx') == 'Hello sphinx'  # nothing changed here
    assert _('Hello reST') == 'Hello reST'

    # load locale2 in addition
    locale.init([rootdir / 'test-locale' / 'locale2'], 'en', 'myext')
    _ = locale.get_translation('myext')
    assert _('Hello world') == 'HELLO WORLD'
    assert _('Hello sphinx') == 'HELLO SPHINX'
    assert _('Hello reST') == 'Hello reST'
Ejemplo n.º 10
0
def test_init(rootdir):
    # not initialized yet
    _ = locale.get_translation('myext')
    assert _('Hello world') == 'Hello world'
    assert _('Hello sphinx') == 'Hello sphinx'
    assert _('Hello reST') == 'Hello reST'

    # load locale1
    locale.init([rootdir / 'test-locale' / 'locale1'], 'en', 'myext')
    _ = locale.get_translation('myext')
    assert _('Hello world') == 'HELLO WORLD'
    assert _('Hello sphinx') == 'Hello sphinx'
    assert _('Hello reST') == 'Hello reST'

    # load a catalog to unrelated namespace
    locale.init([rootdir / 'test-locale' / 'locale2'], 'en', 'myext',
                'mynamespace')
    _ = locale.get_translation('myext')
    assert _('Hello world') == 'HELLO WORLD'
    assert _('Hello sphinx') == 'Hello sphinx'  # nothing changed here
    assert _('Hello reST') == 'Hello reST'

    # load locale2 in addition
    locale.init([rootdir / 'test-locale' / 'locale2'], 'en', 'myext')
    _ = locale.get_translation('myext')
    assert _('Hello world') == 'HELLO WORLD'
    assert _('Hello sphinx') == 'HELLO SPHINX'
    assert _('Hello reST') == 'Hello reST'
Ejemplo n.º 11
0
 def _init_i18n(self):
     """Load translated strings from the configured localedirs if enabled in
     the configuration.
     """
     if self.config.language is not None:
         self.info(bold('loading translations [%s]... ' %
                        self.config.language), nonl=True)
         locale_dirs = [None, path.join(package_dir, 'locale')] + \
             [path.join(self.srcdir, x) for x in self.config.locale_dirs]
     else:
         locale_dirs = []
     self.translator, has_translation = locale.init(locale_dirs,
                                                    self.config.language)
     if self.config.language is not None:
         if has_translation:
             self.info('done')
         else:
             self.info('locale not available')
Ejemplo n.º 12
0
 def _init_i18n(self):
     """Load translated strings from the configured localedirs if enabled in
     the configuration.
     """
     if self.config.language is not None:
         self.info(bold("loading translations [%s]... " % self.config.language), nonl=True)
         locale_dirs = [None, path.join(package_dir, "locale")] + [
             path.join(self.srcdir, x) for x in self.config.locale_dirs
         ]
     else:
         locale_dirs = []
     self.translator, has_translation = locale.init(locale_dirs, self.config.language)
     if self.config.language is not None:
         if has_translation or self.config.language == "en":
             # "en" never needs to be translated
             self.info("done")
         else:
             self.info("not available for built-in messages")
Ejemplo n.º 13
0
 def _init_i18n(self):
     """Load translated strings from the configured localedirs if enabled in
     the configuration.
     """
     if self.config.language is not None:
         self.info(bold('loading translations [%s]... ' %
                        self.config.language), nonl=True)
         locale_dirs = [None, path.join(package_dir, 'locale')] + \
             [path.join(self.srcdir, x) for x in self.config.locale_dirs]
     else:
         locale_dirs = []
     self.translator, has_translation = locale.init(locale_dirs,
                                                    self.config.language)
     if self.config.language is not None:
         if has_translation:
             self.info('done')
         else:
             self.info('locale not available')
Ejemplo n.º 14
0
 def _init_i18n(self):
     """Load translated strings from the configured localedirs if enabled in
     the configuration.
     """
     if self.config.language is not None:
         self.info(bold('loading translations [%s]... ' %
                        self.config.language), nonl=True)
         locale_dirs = [None, path.join(package_dir, 'locale')] + \
             [path.join(self.srcdir, x) for x in self.config.locale_dirs]
     else:
         locale_dirs = []
     self.translator, has_translation = locale.init(locale_dirs,
                                                    self.config.language,
                                                    charset=self.config.source_encoding)
     if self.config.language is not None:
         if has_translation or self.config.language == 'en':
             # "en" never needs to be translated
             self.info('done')
         else:
             self.info('not available for built-in messages')
Ejemplo n.º 15
0
 def _init_i18n(self):
     """Load translated strings from the configured localedirs if enabled in
     the configuration.
     """
     if self.config.language is not None:
         self.info(bold('loading translations [%s]... ' %
                        self.config.language), nonl=True)
         locale_dirs = [None, path.join(package_dir, 'locale')] + \
             [path.join(self.srcdir, x) for x in self.config.locale_dirs]
     else:
         locale_dirs = []
     self.translator, has_translation = locale.init(locale_dirs,
                                                    self.config.language,
                                                    charset=self.config.source_encoding)
     if self.config.language is not None:
         if has_translation or self.config.language == 'en':
             # "en" never needs to be translated
             self.info('done')
         else:
             self.info('not available for built-in messages')
Ejemplo n.º 16
0
def test_init_with_unknown_language(rootdir):
    locale.init([rootdir / 'test-locale' / 'locale1'], 'unknown', 'myext')
    _ = locale.get_translation('myext')
    assert _('Hello world') == 'Hello world'
    assert _('Hello sphinx') == 'Hello sphinx'
    assert _('Hello reST') == 'Hello reST'
Ejemplo n.º 17
0
def test_init_with_unknown_language(rootdir):
    locale.init([rootdir / 'test-locale' / 'locale1'], 'unknown', 'myext')
    _ = locale.get_translation('myext')
    assert _('Hello world') == 'Hello world'
    assert _('Hello sphinx') == 'Hello sphinx'
    assert _('Hello reST') == 'Hello reST'