def test_pluralize_default_translations(self): # test that even without message ids loaded that # "localizer.pluralize" "works" instead of raising an inscrutable # "translations object has no attr 'plural' error; see # see https://github.com/Pylons/pyramid/issues/235 from pyramid.i18n import Translations translations = Translations() translations._catalog = {} localizer = self._makeOne(None, translations) result = localizer.pluralize('singular', 'plural', 2, domain='1', mapping={}) self.assertEqual(result, 'plural')
def make_localizer(current_locale_name, translation_directories, domain=None): """ Create a :class:`pyramid.i18n.Localizer` object corresponding to the provided locale name from the translations found in the list of translation directories.""" locale = Locale.parse(current_locale_name) translations = Translations(domain=domain) translations._catalog = {} locales_to_try = [] if not current_locale_name == locale.language: locales_to_try.append(locale.language) locales_to_try.append(current_locale_name) # intent: order locales left to right in least specific to most specific, # e.g. ['de', 'de_DE']. This services the intent of creating a # translations object that returns a "more specific" translation for a # region, but will fall back to a "less specific" translation for the # locale if necessary. Ordering from least specific to most specific # allows us to call translations.add in the below loop to get this # behavior. for tdir in translation_directories: locale_dirs = [] for lname in locales_to_try: ldir = os.path.realpath(os.path.join(tdir, lname)) if os.path.isdir(ldir): locale_dirs.append(ldir) for locale_dir in locale_dirs: messages_dir = os.path.join(locale_dir, 'LC_MESSAGES') if not os.path.isdir(os.path.realpath(messages_dir)): continue for mofile in os.listdir(messages_dir): mopath = os.path.realpath(os.path.join(messages_dir, mofile)) if mofile.endswith('.mo') and os.path.isfile(mopath): with open(mopath, 'rb') as mofp: domain = mofile[:-3] dtrans = Translations(mofp, domain) translations.add(dtrans) return Localizer(locale=locale, translations=translations)