def read_catalog(filename, lang, region): try: is_pot = filename.endswith(".pot") f = (polib.mofile if filename.endswith(".mo") else polib.pofile)(filename) pf = f.metadata.get("Plural-Forms") if pf is None: quantities = None elif is_pot: quantities = ["one", "other"] else: match = re.search(r"nplurals=(\d+);", pf) if not match: raise Exception("Failed to parse Plural-Forms") nplurals = int(match.group(1)) try: locale = babel.Locale("{}_{}".format(lang, region)) except babel.UnknownLocaleError: locale = babel.Locale(lang) quantities = sorted(locale.plural_form.tags | {"other"}, key=["zero", "one", "two", "few", "many", "other"].index) if len(quantities) != nplurals: raise Exception("Plural-Forms says nplurals={}, but Babel has {} plural tags " "for this language {}" .format(nplurals, len(quantities), quantities)) catalog = {} for entry in f: try: msgid = entry.msgid if is_excluded(msgid): continue # Replace Python str.format syntax with Java String.format syntax. keywords = re.findall(r"\{(\w+)\}", msgid) def fix_format(s): s = s.replace("{}", "%s") for k in keywords: s = s.replace("{" + k + "}", "%{}$s".format(keywords.index(k) + 1)) return s msgid = fix_format(msgid) if entry.msgid_plural: msgstr_plural = ({0: msgid, 1: entry.msgid_plural} if is_pot else entry.msgstr_plural) if quantities is None: raise Exception("File contains a plural entry, but has no Plural-Forms") catalog[msgid] = {quantities[i]: fix_format(s) for i, s in msgstr_plural.items()} else: catalog[msgid] = msgid if is_pot else fix_format(entry.msgstr) except Exception: raise Exception("Failed to process entry '{}'".format(entry.msgid)) return catalog except Exception: raise Exception("Failed to process '{}'".format(filename))
def _DoStart(self): try: if self.DefaultLocaleName is None: self.DefaultLocale = babel.Locale.default() else: self.DefaultLocale = babel.Locale(self.DefaultLocaleName) except babel.UnknownLocaleError: self.DefaultLocale = babel.Locale('en')
def setUp(self): self.default_locale = babel.default_locale() self.foreign_locale = babel.Locale("it") if self.foreign_locale == self.default_locale: self.foreign_locale = babel.Locale("de") self.local_timezone = tzlocal.get_localzone() self.foreign_timezone = pytz.timezone("US/Eastern") if self.foreign_timezone == self.local_timezone: self.foreign_timezone = pytz.timezone("Europe/Berlin")
def __init__(self, context, request: Request): self.request = request self.context = context _ = request.translate self.helper = request.registry['i18n_helper'] self.lang = request.matchdict['lang'] self.domain = request.matchdict['domain'] self.locale = babel.Locale(*babel.parse_locale(self.lang)) self.pot_file_path = os.path.join(self.helper.locale_dir, '{0}.pot'.format(self.domain)) self.po_file_path = os.path.join(self.helper.locale_dir, self.lang, 'LC_MESSAGES', '{0}.po'.format(self.domain)) self.mo_file_path = os.path.join(self.helper.locale_dir, self.lang, 'LC_MESSAGES', '{0}.mo'.format(self.domain)) os.makedirs(os.path.join(self.helper.locale_dir, self.lang, 'LC_MESSAGES'), exist_ok=True) if os.path.exists(self.po_file_path): self.po = polib.pofile(self.po_file_path, encoding='UTF-8') else: self.po = polib.POFile(encoding='UTF-8') self.po.metadata = { 'Content-Transfer-Encoding': '8bit', 'Content-Type': 'text/plain; charset=UTF-8', 'Language': self.lang }
def __init__(self, master, title, start_date=None, locale='en', target_type=TargetShape.Circle, fonts=None, colors=None): super(DateDialog, self).__init__(master, title) self.date = None if not fonts: fonts = tks.load_fonts() if not colors: colors = tks.load_colors() if babel and not isinstance(locale, babel.Locale): locale = babel.Locale(locale) self.selector = DateSelector(self, start_date, locale=locale, target_type=target_type, fonts=fonts, colors=colors) self.deiconify() selector_size = self.selector.size # gi = tks.parse_geometry(self.winfo_geometry()) # self.minsize(gi[0], gi[1]) self.minsize(self.winfo_reqwidth(), self.winfo_reqheight()) self.resizable(width=False, height=False)
def init_first_day_of_week(): global FIRST_DAY_OF_WEEK try: import babel import locale mylocale = babel.Locale(locale.getdefaultlocale()[0]) if mylocale.first_week_day == 0: FIRST_DAY_OF_WEEK = MONDAY else: FIRST_DAY_OF_WEEK = SUNDAY logger.debug('According to babel first day of week is %i', FIRST_DAY_OF_WEEK) except ImportError: # Fallback gleaned from gtkcalendar.c - hence the inconsistency # with weekday numbers in iso calendar... t = _("calendar:week_start:0") # T: Translate to "calendar:week_start:0" if you want Sunday to be the first day of the week or to "calendar:week_start:1" if you want Monday to be the first day of the week if t[-1] == '0': FIRST_DAY_OF_WEEK = SUNDAY elif t[-1] == '1': FIRST_DAY_OF_WEEK = MONDAY else: logger.warn( "Whoever translated 'calendar:week_start:0' did so wrongly.") FIRST_DAY_OF_WEEK = SUNDAY
def new_lang_view(self): lang = self.request.POST.get('new_lang', '').strip() try: self.request.locale = babel.Locale(*babel.parse_locale(lang)) if not os.path.isdir(os.path.join(self.helper.locale_dir, lang)): os.mkdir(os.path.join(self.helper.locale_dir, lang)) os.mkdir( os.path.join(self.helper._dir, 'locale', lang, 'LC_MESSAGES')) # self.pot.save(os.path.join(self.helper.package_dir, 'locale', lang, 'LC_MESSAGES', # '{0}.po'.format(self.domain))) # self.pot.save_as_mofile(os.path.join(self.helper.package_dir, 'locale', lang, 'LC_MESSAGES', # '{0}.mo'.format(self.domain))) self.request.flash_message.add( message_type='success', body='i18n_new_lang_creation_success', domain='i18n_helper') return HTTPFound(location=self.request.route_url( 'i18n_helper.po', lang=lang, domain=self.domain)) else: self.request.flash_message.add(message_type='danger', body='i18n_new_lang_lang_exist', domain='i18n_helper') except: self.request.flash_message.add(message_type='danger', body='i18n_new_lang_creation_error', domain='i18n_helper') return self.get_view()
def normalize_language(language): # handle zh-XX_#Hanx for Android 7.0+ and zh-Hant-HK for iOS 11 is_special_chinese_locale = 'zh' in language and bool( re.findall(r'(Hans|Hant)', language)) if is_special_chinese_locale: country = re.findall(r'[_-]([A-Z]{2}|[a-z]{2})', language) country_code = country[0] if country else 'TW' language = '{}-{}'.format('zh', country_code) if language: language = language.replace('_', '-') else: language = '' try: parts = babel.Locale.parse(language, sep='-') except (babel.UnknownLocaleError, ValueError): parts = babel.Locale(get_default_lang()) language = parts.language script = parts.script region = parts.territory # Special handle for Chinese if language == 'zh': if region not in ['HK', 'TW', 'CN']: if script == 'Hans': region = 'CN' else: region = 'HK' language += '-' + region return language
def __get_env_language_for_babel(self): language_code = self.env.lang try: return babel.Locale.parse(language_code, sep='-') except babel.UnknownLocaleError as e: # fallback language return babel.Locale('en')
def __init__(self, master, locale): super(MonthSelector, self).__init__(master, style='Selector.tks.TFrame') self._master = master self._date = None if babel: if not isinstance(locale, babel.Locale): locale = babel.Locale(locale) self._months = locale.months['format']['wide'] else: self._months = calendar.month_name self._prev_btn = ttk.Button(self, text='<', width=2, command=self._prev_year, style='Selector.tks.TButton') self._prev_btn.grid(row=0, column=0, sticky=tk.W, padx=(0, 4)) self._year_btn = ttk.Button(self, style='Selector.tks.TButton') self._year_btn.grid(row=0, column=1, sticky=tk.EW) self._year_btn.bind('<ButtonRelease-1>', self._master.year_btn_clicked) self._next_btn = ttk.Button(self, text='>', width=2, command=self._next_year, style='Selector.tks.TButton') self._next_btn.grid(row=0, column=2, sticky=tk.E, padx=(4, 0)) btn_frame = ttk.Frame(self, style='tks.TFrame') self._buttons = [] for y in range(4): for x in range(3): month = y * 3 + x + 1 name = self._months[month] btn = ttk.Button(btn_frame, text=name, style='Month.Selector.tks.TButton', command=partial(self._btn_selected, month)) self._buttons.append(btn) btn.grid(row=y, column=x, pady=(0, 4)) btn_frame.columnconfigure(0, weight=1) btn_frame.columnconfigure(1, weight=1) btn_frame.columnconfigure(2, weight=1) btn_frame.grid(row=1, column=0, columnspan=3, pady=(4, 0), sticky=tk.NSEW) self.columnconfigure(0, weight=0) self.columnconfigure(1, weight=1) self.columnconfigure(2, weight=0)
def get_available_languages(): # Magic paths copied from pylons.i18n.translation._get_translator localedir = os.path.join(config['pylons.paths']['root'], 'i18n') messagefiles = gettext.find(config['pylons.package'], localedir, languages=babel.Locale('en').languages.keys(), all=True) return [path.split('/')[-3] for path in messagefiles]
def select_lang_view(self): lang = self.request.POST.get('select_lang', '').strip() self.request.locale = babel.Locale(*babel.parse_locale(lang)) return HTTPFound(location=self.request.route_url( 'i18n_helper.po', lang=lang, domain=self.domain))
def format_date(dt): ''' Format the date in a local aware format. ''' from pylons import tmpl_context as c return babel.dates.format_date(dt, format='long', locale=c.locale or babel.Locale('en', 'US'))
def get_locale(): try: return babel.Locale('en') except AttributeError: # As babel is optional, we may raise an AttributeError accessing it raise ImproperlyConfigured( 'Could not load get_locale function using Babel. Either ' 'install Babel or make a similar function and override it ' 'in this module.')
def __init__( self, expires: datetime, pattern: str = "'Expires='EEE, dd MMM yyyy HH:mm:ss 'GMT'", locale: babel.Locale = babel.Locale("en"), ): self._pattern: str = pattern self._locale: babel.Locale = locale self._expires: datetime = expires
def format_time(dt, set_timezone=True): ''' Format the date in a local aware format. ''' from pylons import tmpl_context as c if set_timezone: dt = local_datetime(dt) return babel.dates.format_time(dt, format='short', locale=c.locale or babel.Locale('en', 'US'))
def _run(self): nameToCode = {} codeToName = {} for localeCode in babel.Locale("en").languages.keys(): with contextlib.ignored(babel.UnknownLocaleError): locale = babel.Locale(localeCode) codeToName[localeCode] = locale.display_name nameToCode[localeCode] = localeCode for code, name in locale.languages.iteritems(): nameToCode[name.lower()] = code #make the test suite pass ;). nameToCode["frisian"] = "fy" nameToCode["frysk"] = "fy" nameToCode["fy"] = "fy" codeToName["fy"] = "Frysk" data = FILE_TEMPLATE % (repr(nameToCode), repr(codeToName)) print data.encode("UTF-8")
def title(self): current_locale = get_locale() if current_locale is None: current_locale = babel.Locale('en') # Special case possesive form in English if current_locale.language.lower() == 'en' and \ current_user.name[:-1] == u's': return u"{}' Requests".format(current_user.name) else: # TRANS: The title of the page listing all requests an individual # TRANS: user has made. return gettext(u"%(name)s's Requests", name=current_user.name)
def __get_env_language_for_babel(self): language_code = self.env.lang # A hack to avoid rejecting zh-hk locale. # This corresponds to the hack with LANGUAGE_SYNONYMS in const.py. # TODO: remove these 2 lines when a original code can be passed to here. if language_code == 'zhhk': language_code = 'zh-hk' try: return babel.Locale.parse(language_code, sep='-') except babel.UnknownLocaleError as e: # fallback language return babel.Locale('en')
def backwards(self, orm): """Delete all the languages from the UserProfile of a user.""" profiles = orm['users.UserProfile'].objects.all() available_languages = babel.Locale('en').languages for profile in profiles: lang_codes = profile.language_set.values_list('code', flat=True) user_languages = [] for code in lang_codes: user_languages.append(available_languages[code]) for language in user_languages: lang, created = orm['groups.Language'].objects.get_or_create(name=language.lower()) profile.languages.add(lang) orm['users.Language'].objects.all().delete()
def index(request: HttpRequest) -> JsonResponse: """List of { id, offset, name, aliases } timezones. Timezones are listed from /usr/share/zoneinfo (the IANA time zone database, a.k.a. "tzdata" or "zoneinfo"). They're in the "timezone" key. Aliases are important: "America/Buenos_Aires" was a timezone at one point, and now it's just an alias for "America/Argentina/Buenos_Aires". Clients must be aware of aliases, because a timezone ID selected today may become an alias ID tomorrow. That means users may have selected alias IDs. Offset and name are formatted according to the request locale. In English, it will look like (October 23, 2020): - { id: America/St_Johns, offset: GMT-03:30, name: Canada (St. John's) Time } The offset is calculated from the _request date_. On January 1, 2020: - { id: America/St_Johns, offset: GMT-04:30, name: Canada (St. John's) Time } The response is ordered by (offset[numeric], name[locale-collation]). """ # Note that CLDR has a different set of IDs for timezones: "BCP47" IDs. # BCP47 IDs and aliases are different from the IANA ones. The IANA ones # are The Standard. locale = babel.Locale(request.locale_id) now = datetime.datetime.utcnow() localized_timezones = _localize_timezones(locale) timestamped_timezones = [ _timestamp_localized_timezone(ltz, now) for ltz in localized_timezones ] timestamped_timezones.sort(key=lambda tz: tz.sort_key) json_timezones = [{ "id": tz.id, "offset": tz.offset, "name": tz.name, "aliases": tz.aliases } for tz in timestamped_timezones] response = JsonResponse( { "locale_id": request.locale_id, "timezones": json_timezones }, json_dumps_params={"ensure_ascii": False}, ) response["Content-Language"] = request.locale_id return response
class LanguageField(models.CharField): # TODO: This list will include extinct languages LANGUAGE_CODES = set(babel.Locale("en").languages.keys()) def __init__(self, *args, **kwargs): kwargs.setdefault("max_length", 10) kwargs["choices"] = [(code, code) for code in sorted(self.LANGUAGE_CODES)] super(LanguageField, self).__init__(*args, **kwargs) def get_choices(self, include_blank=True, blank_choice=BLANK_CHOICE_DASH): locale = get_current_babel_locale() translated_choices = [ (code, locale.languages.get(code, code)) for (code, _) in super(LanguageField, self).get_choices(include_blank, blank_choice) ] translated_choices.sort(key=lambda pair: pair[1].lower()) return translated_choices
def test_run_with_defaults(self, mock_slack): # given mock_slack.WebClient.return_value = SlackClientStub(team="T12345678") exporter = SlackChannelExporter("TOKEN_DUMMY") channels = ["C12345678", "C72345678"] # when response = exporter.run(channels) # then self.assertTrue(response["ok"]) for channel_id in channels: self.assertIn(channel_id, response["channels"]) res_channel = response["channels"][channel_id] channel_name = exporter._slack_service.channel_names()[channel_id] self.assertEqual( res_channel["filename_pdf"], str( currentdir.parent / (exporter._slack_service.team + "_" + channel_name + ".pdf") ), ) self.assertTrue(Path(res_channel["filename_pdf"]).is_file()) # assert export details are correct self.assertTrue(res_channel["ok"]) self.assertEqual(res_channel["dest_path"], str(currentdir.parent)) self.assertEqual(res_channel["page_format"], "a4") self.assertEqual(res_channel["page_orientation"], "portrait") self.assertEqual( res_channel["max_messages"], settings.MAX_MESSAGES_PER_CHANNEL, ) self.assertEqual(res_channel["timezone"], pytz.UTC) self.assertEqual(res_channel["locale"], babel.Locale("en", "US")) # assert infos in PDF file are correct pdf_file = open(res_channel["filename_pdf"], "rb") pdf_reader = PyPDF2.PdfFileReader(pdf_file) doc_info = pdf_reader.getDocumentInfo() self.assertEqual(doc_info.author, "Erik Kalkoken") self.assertEqual(doc_info.creator, f"Channel Export v{__version__}") self.assertEqual( doc_info.title, (exporter._slack_service.team + " / " + channel_name), )
def __init__(self, initial=None): if babel is None: raise ImproperlyConfigured( "The PhonePrefixSelect widget requires the babel package be installed." ) choices = [("", "---------")] language = translation.get_language() or settings.LANGUAGE_CODE locale = babel.Locale(translation.to_locale(language)) if not initial: initial = getattr(settings, "PHONENUMBER_DEFAULT_REGION", None) for prefix, values in COUNTRY_CODE_TO_REGION_CODE.items(): prefix = "+%d" % prefix if initial and initial in values: self.initial = prefix for country_code in values: country_name = locale.territories.get(country_code) if country_name: choices.append((prefix, f"{country_name} {prefix}")) super().__init__(choices=sorted(choices, key=lambda item: item[1]))
def prettify_lang(s): s = str(s) try: lang_name, region_name = s.split("-", 2)[:2] except ValueError: lang_name, region_name = s, None options = [ lang_name, ] if region_name is not None: options.insert(0, "{}_{}".format(lang_name, region_name.upper())) for option in options: try: return babel.Locale(lang_name).get_display_name(DISPLAY_LOCALE) except babel.core.UnknownLocaleError: pass return s
def get_currency_names(locale_str: str) -> typing.Dict[str, str]: """ Try to get currency names and symbols for a Babel locale. Returns: Dictionary whose keys are currency symbols (like "$") and whose values are currency names (like "USD") """ currency_names = {} try: import babel import babel.numbers locale = babel.Locale(locale_str) currency_names = { babel.numbers.get_currency_symbol(cn): cn for cn in locale.currency_symbols } except ImportError: pass return currency_names
def get_languages_for_locale(locale): """This method returns available languages localized in locale. If a language cannnot be localized, return REFERENCE_LANGUAGE. Translated dictionaries get cached in AVAILABLE_LANGUAGES. We use Babel to get translated language names. """ # Babel uses _ instead of - for locales. E.g. 'en_US' instead of # 'en-US' locale = locale.replace('-', '_') if locale not in AVAILABLE_LANGUAGES: try: local_lang = babel.Locale(locale).languages except babel.UnknownLocaleError: return AVAILABLE_LANGUAGES['en'] # If a translation is missing, add an untranslated entry from # the REFERENCE_LANGUAGE diff = [ lc for lc in REFERENCE_LANGUAGE.keys() if lc not in local_lang.keys() ] for lc in diff: local_lang[lc] = REFERENCE_LANGUAGE[lc] # Remove unwanted and testing languages. for lang in REMOVE_LANGS: if lang in local_lang: local_lang.pop(lang) # Sort based on language name. local_lang = sorted([(key, value.capitalize()) for key, value in local_lang.items()], key=lambda language: language[1]) AVAILABLE_LANGUAGES[locale] = local_lang return AVAILABLE_LANGUAGES[locale]
def __init__(self, output="Geographies.xml", languages=('es', 'ja', 'fr', 'de', 'zh')): self.output_fp = os.path.realpath( os.path.join(os.path.abspath(os.path.dirname(__file__)), "..", "output", output)) self.input_fp = os.path.realpath( os.path.join(os.path.abspath(os.path.dirname(__file__)), "..", "output", "all.csv")) self.languages = languages self.language_dictionary = {} for language in self.languages: # TODO: Can split string based on en-US, etc. locale = babel.Locale(language) self.language_dictionary[language] = locale.territories self.columns = [ u'id', u'name', u'shortname', u'kml', u'ISOTwoLetterCode', u'longitude', u'ISOThreeLetterCode', u'latitude' ] self.input_reader = csv.reader(open(self.input_fp, encoding='utf-8')) self.create_document() # Creates self.doc self.write_document()
from datetime import datetime, timedelta import pkgutil import babel from babel import Locale import babel.dates import formencode from pylons.i18n import _, add_fallback, set_lang from pylons import config, tmpl_context as c LOCALES = [ babel.Locale('de', 'DE'), babel.Locale('en', 'US'), babel.Locale('fr', 'FR'), babel.Locale('nl', 'NL'), babel.Locale('ru', 'RU') ] def get_default_locale(): try: if c.instance and c.instance.locale: return c.instance.locale locale = config.get('adhocracy.language', 'en_US') return babel.Locale.parse(locale) except TypeError: return babel.Locale.parse('en_US') def handle_request(): """
def clean(self): super(Currency, self).clean() # make sure the code is a valid ISO-4217 currency if self.code not in babel.Locale("en").currency_symbols: raise ValidationError(_('Enter a valid ISO-4217 currency code'))