def test_get_catalogs_for_xx_without_outdated(dir): (dir / 'loc1' / 'xx' / 'LC_MESSAGES').makedirs() (dir / 'loc1' / 'xx' / 'LC_MESSAGES' / 'test1.po').write_text('#') (dir / 'loc1' / 'xx' / 'LC_MESSAGES' / 'test1.mo').write_text('#') (dir / 'loc1' / 'xx' / 'LC_MESSAGES' / 'test2.po').write_text('#') (dir / 'loc1' / 'xx' / 'LC_MESSAGES' / 'test2.mo').write_text('#') (dir / 'loc1' / 'xx' / 'LC_MESSAGES' / 'test3.pot').write_text('#') (dir / 'loc1' / 'xx' / 'LC_MESSAGES' / 'test3.mo').write_text('#') (dir / 'loc1' / 'xx' / 'LC_MESSAGES' / 'sub').makedirs() (dir / 'loc1' / 'xx' / 'LC_MESSAGES' / 'sub' / 'test4.po').write_text('#') (dir / 'loc1' / 'xx' / 'LC_MESSAGES' / 'sub' / 'test4.mo').write_text('#') (dir / 'loc1' / 'xx' / 'LC_MESSAGES' / 'sub' / 'test5.po').write_text('#') (dir / 'loc1' / 'xx' / 'LC_MESSAGES' / 'sub' / 'test5.mo').write_text('#') catalogs = i18n.find_catalog_source_files([dir / 'loc1'], 'xx', force_all=False) assert not catalogs catalogs = i18n.find_catalog_source_files([dir / 'loc1'], 'xx', force_all=True) domains = set(c.domain for c in catalogs) assert domains == set([ 'test1', 'test2', 'sub/test4', 'sub/test5', ])
def compile_update_catalogs(self): catalogs = i18n.find_catalog_source_files( [path.join(self.srcdir, x) for x in self.config.locale_dirs], self.config.language, gettext_compact=self.config.gettext_compact) message = 'targets for %d po files that are out of date' % len(catalogs) self.compile_catalogs(catalogs, message)
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')
def compile_all_catalogs(self): catalogs = i18n.find_catalog_source_files( [path.join(self.srcdir, x) for x in self.config.locale_dirs], self.config.language, gettext_compact=self.config.gettext_compact, force_all=True) message = 'all of %d po files' % len(catalogs) self.compile_catalogs(catalogs, message)
def test_get_catalogs_for_en(dir): (dir / 'loc1' / 'xx' / 'LC_MESSAGES').makedirs() (dir / 'loc1' / 'xx' / 'LC_MESSAGES' / 'xx_dom.po').write_text('#') (dir / 'loc1' / 'en' / 'LC_MESSAGES').makedirs() (dir / 'loc1' / 'en' / 'LC_MESSAGES' / 'en_dom.po').write_text('#') catalogs = i18n.find_catalog_source_files([dir / 'loc1'], 'en', force_all=False) domains = set(c.domain for c in catalogs) assert domains == set(['en_dom'])
def test_get_catalogs_excluded(tempdir): (tempdir / 'loc1' / 'en' / 'LC_MESSAGES' / '.git').makedirs() (tempdir / 'loc1' / 'en' / 'LC_MESSAGES' / 'en_dom.po').write_text('#') (tempdir / 'loc1' / 'en' / 'LC_MESSAGES' / '.git' / 'no_no.po').write_text('#') catalogs = i18n.find_catalog_source_files( [tempdir / 'loc1'], 'en', force_all=False, excluded=lambda path: '.git' in path) domains = set(c.domain for c in catalogs) assert domains == set(['en_dom'])
def compile_update_catalogs(self): # type: () -> None catalogs = i18n.find_catalog_source_files( [path.join(self.srcdir, x) for x in self.config.locale_dirs], self.config.language, charset=self.config.source_encoding, excluded=Matcher(['**/.?**'])) message = __('targets for %d po files that are out of date') % len(catalogs) self.compile_catalogs(catalogs, message)
def compile_all_catalogs(self): # type: () -> None catalogs = i18n.find_catalog_source_files( [path.join(self.srcdir, x) for x in self.config.locale_dirs], self.config.language, charset=self.config.source_encoding, force_all=True, excluded=Matcher(['**/.?**'])) message = __('all of %d po files') % len(catalogs) self.compile_catalogs(catalogs, message)
def compile_all_catalogs(self): # type: () -> None catalogs = i18n.find_catalog_source_files( [path.join(self.srcdir, x) for x in self.config.locale_dirs], self.config.language, charset=self.config.source_encoding, gettext_compact=self.config.gettext_compact, force_all=True) message = __('all of %d po files') % len(catalogs) self.compile_catalogs(catalogs, message)
def test_get_catalogs_from_multiple_locale_dirs(tempdir): (tempdir / 'loc1' / 'xx' / 'LC_MESSAGES').makedirs() (tempdir / 'loc1' / 'xx' / 'LC_MESSAGES' / 'test1.po').write_text('#') (tempdir / 'loc2' / 'xx' / 'LC_MESSAGES').makedirs() (tempdir / 'loc2' / 'xx' / 'LC_MESSAGES' / 'test1.po').write_text('#') (tempdir / 'loc2' / 'xx' / 'LC_MESSAGES' / 'test2.po').write_text('#') catalogs = i18n.find_catalog_source_files([tempdir / 'loc1', tempdir / 'loc2'], 'xx') domains = sorted(c.domain for c in catalogs) assert domains == ['test1', 'test1', 'test2']
def test_get_catalogs_from_multiple_locale_dirs(dir): (dir / 'loc1' / 'xx' / 'LC_MESSAGES').makedirs() (dir / 'loc1' / 'xx' / 'LC_MESSAGES' / 'test1.po').write_text('#') (dir / 'loc2' / 'xx' / 'LC_MESSAGES').makedirs() (dir / 'loc2' / 'xx' / 'LC_MESSAGES' / 'test1.po').write_text('#') (dir / 'loc2' / 'xx' / 'LC_MESSAGES' / 'test2.po').write_text('#') catalogs = i18n.find_catalog_source_files([dir / 'loc1', dir / 'loc2'], 'xx') domains = sorted(c.domain for c in catalogs) assert domains == ['test1', 'test1', 'test2']
def compile_update_catalogs(self): # type: () -> None catalogs = i18n.find_catalog_source_files( [path.join(self.srcdir, x) for x in self.config.locale_dirs], self.config.language, charset=self.config.source_encoding, gettext_compact=self.config.gettext_compact) message = 'targets for %d po files that are out of date' % len( catalogs) self.compile_catalogs(catalogs, message)
def compile_all_catalogs(self): # type: () -> None catalogs = i18n.find_catalog_source_files( [path.join(self.srcdir, x) for x in self.config.locale_dirs], self.config.language, charset=self.config.source_encoding, gettext_compact=self.config.gettext_compact, force_all=True) message = 'all of %d po files' % len(catalogs) self.compile_catalogs(catalogs, message)
def compile_update_catalogs(self): # type: () -> None catalogs = i18n.find_catalog_source_files( [path.join(self.srcdir, x) for x in self.config.locale_dirs], self.config.language, charset=self.config.source_encoding, excluded=Matcher(['**/.?**'])) message = __('targets for %d po files that are out of date') % len( catalogs) self.compile_catalogs(catalogs, message)
def test_get_catalogs_with_compact(dir): (dir / 'loc1' / 'xx' / 'LC_MESSAGES').makedirs() (dir / 'loc1' / 'xx' / 'LC_MESSAGES' / 'test1.po').write_text('#') (dir / 'loc1' / 'xx' / 'LC_MESSAGES' / 'test2.po').write_text('#') (dir / 'loc1' / 'xx' / 'LC_MESSAGES' / 'sub').makedirs() (dir / 'loc1' / 'xx' / 'LC_MESSAGES' / 'sub' / 'test3.po').write_text('#') (dir / 'loc1' / 'xx' / 'LC_MESSAGES' / 'sub' / 'test4.po').write_text('#') catalogs = i18n.find_catalog_source_files([dir / 'loc1'], 'xx', gettext_compact=True) domains = set(c.domain for c in catalogs) assert domains == set(['test1', 'test2', 'sub'])
def test_get_catalogs_for_en(tempdir): (tempdir / 'loc1' / 'xx' / 'LC_MESSAGES').makedirs() (tempdir / 'loc1' / 'xx' / 'LC_MESSAGES' / 'xx_dom.po').write_text('#') (tempdir / 'loc1' / 'en' / 'LC_MESSAGES').makedirs() (tempdir / 'loc1' / 'en' / 'LC_MESSAGES' / 'en_dom.po').write_text('#') catalogs = i18n.find_catalog_source_files([tempdir / 'loc1'], 'en', force_all=False) domains = set(c.domain for c in catalogs) assert domains == set(['en_dom'])
def compile_specific_catalogs(self, specified_files): def to_domain(fpath): docname, _ = path.splitext(path_stabilize(fpath)) dom = find_catalog(docname, self.config.gettext_compact) return dom specified_domains = set(map(to_domain, specified_files)) catalogs = i18n.find_catalog_source_files( [path.join(self.srcdir, x) for x in self.config.locale_dirs], self.config.language, domains=list(specified_domains), gettext_compact=self.config.gettext_compact) message = 'targets for %d po files that are specified' % len(catalogs) self.compile_catalogs(catalogs, message)
def init(locale_dirs, language, catalog='sphinx', charset='utf-8'): """Look for message catalogs in `locale_dirs` and *ensure* that there is at least a NullTranslations catalog set in `translators`. If called multiple times or if several ``.mo`` files are found, their contents are merged together (thus making ``init`` reentrable). """ global translators translator = translators.get(catalog) # ignore previously failed attempts to find message catalogs if isinstance(translator, gettext.NullTranslations): translator = None # the None entry is the system's default locale path has_translation = True # compile mo files if po file is updated # TODO: remove circular importing from sphinx.util.i18n import find_catalog_source_files for catinfo in find_catalog_source_files(locale_dirs, language, domains=[catalog], charset=charset): catinfo.write_mo(language) # loading for dir_ in locale_dirs: try: trans = gettext.translation(catalog, localedir=dir_, languages=[language]) if translator is None: translator = trans else: translator._catalog.update(trans._catalog) except Exception: # Language couldn't be found in the specified path pass # guarantee translators[catalog] exists if translator is None: translator = gettext.NullTranslations() has_translation = False translators[catalog] = translator if hasattr(translator, 'ugettext'): translator.gettext = translator.ugettext return translator, has_translation
def compile_specific_catalogs(self, specified_files): # type: (List[unicode]) -> None def to_domain(fpath): # type: (unicode) -> unicode docname = self.env.path2doc(path.abspath(fpath)) if docname: return find_catalog(docname, self.config.gettext_compact) else: return None specified_domains = set(map(to_domain, specified_files)) specified_domains.discard(None) catalogs = i18n.find_catalog_source_files( [path.join(self.srcdir, x) for x in self.config.locale_dirs], self.config.language, domains=list(specified_domains), charset=self.config.source_encoding, gettext_compact=self.config.gettext_compact) message = 'targets for %d po files that are specified' % len(catalogs) self.compile_catalogs(catalogs, message)
def compile_specific_catalogs(self, specified_files): # type: (List[unicode]) -> None def to_domain(fpath): # type: (unicode) -> unicode docname = self.env.path2doc(path.abspath(fpath)) if docname: return find_catalog(docname, self.config.gettext_compact) else: return None specified_domains = set(map(to_domain, specified_files)) specified_domains.discard(None) catalogs = i18n.find_catalog_source_files( [path.join(self.srcdir, x) for x in self.config.locale_dirs], self.config.language, domains=list(specified_domains), charset=self.config.source_encoding, excluded=Matcher(['**/.?**'])) message = __('targets for %d po files that are specified') % len(catalogs) self.compile_catalogs(catalogs, message)
def test_get_catalogs_for_xx(dir): (dir / 'loc1' / 'xx' / 'LC_MESSAGES').makedirs() (dir / 'loc1' / 'xx' / 'LC_MESSAGES' / 'test1.po').write_text('#') (dir / 'loc1' / 'xx' / 'LC_MESSAGES' / 'test2.po').write_text('#') (dir / 'loc1' / 'xx' / 'LC_MESSAGES' / 'test3.pot').write_text('#') (dir / 'loc1' / 'xx' / 'LC_MESSAGES' / 'sub').makedirs() (dir / 'loc1' / 'xx' / 'LC_MESSAGES' / 'sub' / 'test4.po').write_text('#') (dir / 'loc1' / 'xx' / 'LC_MESSAGES' / 'sub' / 'test5.po').write_text('#') (dir / 'loc1' / 'en' / 'LC_MESSAGES').makedirs() (dir / 'loc1' / 'en' / 'LC_MESSAGES' / 'test6.po').write_text('#') (dir / 'loc1' / 'xx' / 'LC_ALL').makedirs() (dir / 'loc1' / 'xx' / 'LC_ALL' / 'test7.po').write_text('#') catalogs = i18n.find_catalog_source_files([dir / 'loc1'], 'xx', force_all=False) domains = set(c.domain for c in catalogs) assert domains == set([ 'test1', 'test2', 'sub/test4', 'sub/test5', ])
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, self.warn) 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')
def test_get_catalogs_with_non_existent_locale(dir): catalogs = i18n.find_catalog_source_files([dir / 'loc1'], 'xx') assert not catalogs catalogs = i18n.find_catalog_source_files([dir / 'loc1'], None) assert not catalogs
def test_get_catalogs_with_non_existent_locale_dirs(): catalogs = i18n.find_catalog_source_files(['dummy'], 'xx') assert not catalogs