def runTest(self): """Test for regression of http://trac.edgewall.org/ticket/11337 The language select will be disabled if Babel is not installed and a hint will be shown. The text of the hint is dependent on whether the user has TRAC_ADMIN and the message catalogs have been compiled. """ from trac.util.translation import has_babel, get_available_locales babel_hint = "Install Babel for extended language support." catalog_hint = "Message catalogs have not been compiled." nonadmin_hint = r"Please contact your\s+Trac administrator\s+" \ r"to enable existing translations." language_select = '<select id="language" name="language">' disabled_language_select = \ '<select id="language" name="language" disabled="disabled" ' \ 'title="Translations are currently unavailable">' self._tester.go_to_preferences("Localization") if has_babel: tc.notfind(babel_hint) if get_available_locales(): tc.find(language_select) tc.notfind(babel_hint) tc.notfind(catalog_hint) else: tc.find(disabled_language_select) tc.find(catalog_hint) tc.notfind(babel_hint) else: tc.find(disabled_language_select) tc.find(babel_hint) tc.notfind(catalog_hint) tc.notfind(nonadmin_hint) # For users without TRAC_ADMIN, the Language tab should only be # present when Babel is installed self._tester.logout() self._tester.go_to_preferences("Localization") try: if has_babel and get_available_locales(): tc.find(language_select) tc.notfind(nonadmin_hint) elif has_babel: tc.find(disabled_language_select) tc.find(nonadmin_hint) else: tc.find(disabled_language_select) tc.find(nonadmin_hint) tc.notfind(catalog_hint) tc.notfind(babel_hint) finally: self._tester.login('admin')
def render_preference_panel(self, req, panel): if req.method == 'POST': if 'restore' in req.args: self._do_load(req) else: self._do_save(req) req.redirect(req.href.prefs(panel or None)) data = { 'settings': { 'session': req.session, 'session_id': req.session.sid }, 'timezones': all_timezones, 'timezone': get_timezone, 'localtz': localtz, 'has_babel': False } if Locale: locale_ids = get_available_locales() locales = [Locale.parse(locale) for locale in locale_ids] # use locale identifiers from get_available_locales() instead # of str(locale) to prevent storing expanded locale identifier # to session, e.g. zh_Hans_CN and zh_Hant_TW, since Babel 1.0. # see #11258. languages = sorted((id, locale.display_name) for id, locale in zip(locale_ids, locales)) data['locales'] = locales data['languages'] = languages data['has_babel'] = True return 'prefs_%s.html' % (panel or 'general'), data
def render_preference_panel(self, req, panel): if req.method == 'POST': if Locale and \ req.args.get('language') != req.session.get('language'): # reactivate translations with new language setting # when changed del req.locale # for re-negotiating locale deactivate() make_activable(lambda: req.locale, self.env.path) _do_save(req, panel, self._form_fields) data = { 'timezones': all_timezones, 'timezone': get_timezone, 'localtz': localtz, 'has_babel': False, } if Locale: locale_ids = get_available_locales() locales = [Locale.parse(locale) for locale in locale_ids] # use locale identifiers from get_available_locales() instead # of str(locale) to prevent storing expanded locale identifier # to session, e.g. zh_Hans_CN and zh_Hant_TW, since Babel 1.0. # see #11258. languages = sorted((id_, locale.display_name) for id_, locale in zip(locale_ids, locales)) data['locales'] = locales data['languages'] = languages data['has_babel'] = True return 'prefs_localization.html', data
def render_preference_panel(self, req, panel): if req.method == 'POST': if 'restore' in req.args: self._do_load(req) else: self._do_save(req) req.redirect(req.href.prefs(panel or None)) data = { 'settings': { 'session': req.session, 'session_id': req.session.sid }, 'timezones': all_timezones, 'timezone': get_timezone, 'localtz': localtz } if Locale: locales = [ Locale.parse(locale) for locale in get_available_locales() ] languages = sorted( (str(locale), locale.display_name) for locale in locales) data['locales'] = locales data['languages'] = languages return 'prefs_%s.html' % (panel or 'general'), data
def runTest(self): """Test for regression of http://trac.edgewall.org/ticket/11515 Show a notice message with new language setting after it is changed. """ from trac.util.translation import has_babel, get_available_locales from pkg_resources import resource_exists, resource_filename if not has_babel: return if not resource_exists("trac", "locale"): return locale_dir = resource_filename("trac", "locale") from babel.support import Translations string = "Your preferences have been saved." translated = None for second_locale in get_available_locales(): tx = Translations.load(locale_dir, second_locale) translated = tx.dgettext("messages", string) if string != translated: break # the locale has a translation else: return try: self._tester.go_to_preferences("Language") tc.formvalue("userprefs", "language", second_locale) tc.submit() tc.find(re.escape(translated)) finally: tc.formvalue("userprefs", "language", "") # revert to default tc.submit() tc.find("Your preferences have been saved")
def runTest(self): """Test for regression of http://trac.edgewall.org/ticket/11515 Show a notice message with new language setting after it is changed. """ from trac.util.translation import Locale, has_babel, \ get_available_locales from pkg_resources import resource_exists, resource_filename if not has_babel: return if not resource_exists('trac', 'locale'): return locale_dir = resource_filename('trac', 'locale') from babel.support import Translations string = 'Your preferences have been saved.' translated = None for second_locale_id in get_available_locales(): tx = Translations.load(locale_dir, second_locale_id) translated = tx.dgettext('messages', string) if string != translated: break # the locale has a translation else: return try: self._tester.go_to_preferences('Localization') tc.formvalue('userprefs', 'language', second_locale_id) tc.submit() tc.find(re.escape(translated)) tc.find('<option selected="selected" value="%s">' % second_locale_id) finally: tc.formvalue('userprefs', 'language', '') # revert to default tc.submit() tc.find('Your preferences have been saved')
def render_preference_panel(self, req, panel): if req.method == 'POST': if 'restore' in req.args: self._do_load(req) else: self._do_save(req) req.redirect(req.href.prefs(panel or None)) data = { 'settings': {'session': req.session, 'session_id': req.session.sid}, 'timezones': all_timezones, 'timezone': get_timezone, 'localtz': localtz, 'has_babel': False } if Locale: locale_ids = get_available_locales() locales = [Locale.parse(locale) for locale in locale_ids] # use locale identifiers from get_available_locales() instead # of str(locale) to prevent storing expanded locale identifier # to session, e.g. zh_Hans_CN and zh_Hant_TW, since Babel 1.0. # see #11258. languages = sorted((id, locale.display_name) for id, locale in zip(locale_ids, locales)) data['locales'] = locales data['languages'] = languages data['has_babel'] = True return 'prefs_%s.html' % (panel or 'general'), data
def runTest(self): from trac.util.translation import get_available_locales, has_babel babel_hint_lang = "Install Babel for extended language support." babel_hint_date = "Install Babel for localized date formats." catalog_hint = "Message catalogs have not been compiled." language_select = '<select name="default_language">' disabled_language_select = \ '<select name="default_language" disabled="disabled" ' \ 'title="Translations are currently unavailable">' self._tester.go_to_admin("Basic Settings") if has_babel: tc.notfind(babel_hint_lang) tc.notfind(babel_hint_date) if get_available_locales(): tc.find(language_select) tc.notfind(catalog_hint) else: tc.find(disabled_language_select) tc.find(catalog_hint) else: tc.find(disabled_language_select) tc.find(babel_hint_lang) tc.find(babel_hint_date) tc.notfind(catalog_hint)
def test_activate_unavailable_locale(self): unavailables = sorted(set(locale_identifiers()) - set(translation.get_available_locales())) or \ ('en_US',) locale_dir = self._get_locale_dir() translation.add_domain('catalog1', self.env.path, locale_dir) translation.add_domain('catalog2', self.env.path, locale_dir) translation.activate(unavailables[0], self.env.path)
def render_admin_panel(self, req, cat, page, path_info): valid_default_handlers = [ handler.__class__.__name__ for handler in self.request_handlers if is_valid_default_handler(handler) ] if Locale: locale_ids = get_available_locales() locales = [Locale.parse(locale) for locale in locale_ids] # don't use str(locale) to prevent storing expanded locale # identifier, see #11258 languages = sorted((id, locale.display_name) for id, locale in zip(locale_ids, locales)) else: locale_ids, locales, languages = [], [], [] if req.method == 'POST': for option in ('name', 'url', 'descr'): self.config.set('project', option, req.args.get(option)) default_handler = req.args.get('default_handler') self.config.set('trac', 'default_handler', default_handler) default_timezone = req.args.get('default_timezone') if default_timezone not in all_timezones: default_timezone = '' self.config.set('trac', 'default_timezone', default_timezone) default_language = req.args.get('default_language') if default_language not in locale_ids: default_language = '' self.config.set('trac', 'default_language', default_language) default_date_format = req.args.get('default_date_format') if default_date_format != 'iso8601': default_date_format = '' self.config.set('trac', 'default_date_format', default_date_format) _save_config(self.config, req, self.log) req.redirect(req.href.admin(cat, page)) default_handler = self.config.get('trac', 'default_handler') default_timezone = self.config.get('trac', 'default_timezone') default_language = self.config.get('trac', 'default_language') default_date_format = self.config.get('trac', 'default_date_format') data = { 'default_handler': default_handler, 'valid_default_handlers': sorted(valid_default_handlers), 'default_timezone': default_timezone, 'timezones': all_timezones, 'has_pytz': pytz is not None, 'default_language': default_language.replace('-', '_'), 'languages': languages, 'default_date_format': default_date_format, 'has_babel': Locale is not None, } Chrome(self.env).add_textarea_grips(req) return 'admin_basics.html', data
def _test_get_console_locale_with_babel(self): locales = get_available_locales() en_US = Locale.parse('en_US') de = Locale.parse('de') def unset_locale_envs(): for name in ('LANGUAGE', 'LC_ALL', 'LC_MESSAGES', 'LANG'): if name in os.environ: del os.environ[name] if 'de' in locales: unset_locale_envs() self.assertIsNone(get_console_locale(None, None)) self.assertEqual(de, get_console_locale(None, 'de_DE.UTF8')) self.env.config.set('trac', 'default_language', 'de') self.assertEqual(de, get_console_locale(self.env, None)) self.assertEqual(de, get_console_locale(self.env, 'C')) self.env.config.set('trac', 'default_language', 'en_US') self.assertEqual(en_US, get_console_locale(self.env, None)) self.assertEqual(en_US, get_console_locale(self.env, 'C')) self.assertEqual(de, get_console_locale(self.env, 'de_DE.UTF8')) self.env.config.set('trac', 'default_language', 'de') os.environ['LANG'] = 'POSIX' # unavailable locale in Trac self.assertIsNone(get_console_locale()) self.assertEqual(de, get_console_locale(self.env)) os.environ['LANG'] = '****' # invalid locale self.assertIsNone(get_console_locale()) self.assertEqual(de, get_console_locale(self.env)) os.environ['LANG'] = 'en_US.utf-8' self.assertEqual(en_US, get_console_locale()) self.assertEqual(en_US, get_console_locale(self.env)) os.environ['LC_MESSAGES'] = 'de_DE.utf-8' self.assertEqual(de, get_console_locale()) self.assertEqual(de, get_console_locale(self.env)) os.environ['LC_ALL'] = 'en_US.utf-8' self.assertEqual(en_US, get_console_locale()) self.assertEqual(en_US, get_console_locale(self.env)) os.environ['LANGUAGE'] = 'de_DE:en_US:en' self.assertEqual(de, get_console_locale()) self.assertEqual(de, get_console_locale(self.env)) if not locales: # compiled catalog is missing unset_locale_envs() self.assertIsNone(get_console_locale(None, 'de_DE.UTF8')) self.env.config.set('trac', 'default_language', 'de') self.assertIsNone(get_console_locale(self.env, None)) self.assertIsNone(get_console_locale(self.env, 'C')) self.assertIsNone(get_console_locale(self.env, 'de_DE.UTF8')) os.environ['LANG'] = 'en_US.utf-8' os.environ['LC_MESSAGES'] = 'de_DE.utf-8' os.environ['LC_ALL'] = 'en_US.utf-8' os.environ['LANGUAGE'] = 'de_DE:en_US' self.assertEqual(en_US, get_console_locale()) self.assertEqual(en_US, get_console_locale(self.env))
def render_admin_panel(self, req, cat, page, path_info): if Locale: locale_ids = get_available_locales() locales = [Locale.parse(locale) for locale in locale_ids] # don't use str(locale) to prevent storing expanded locale # identifier, see #11258 languages = sorted((id, locale.display_name) for id, locale in zip(locale_ids, locales)) else: locale_ids, locales, languages = [], [], [] if req.method == 'POST': for option in ('name', 'url', 'descr'): self.config.set('project', option, req.args.get(option)) default_timezone = req.args.get('default_timezone') if default_timezone not in all_timezones: default_timezone = '' self.config.set('trac', 'default_timezone', default_timezone) default_language = req.args.get('default_language') if default_language not in locale_ids: default_language = '' self.config.set('trac', 'default_language', default_language) default_date_format = req.args.get('default_date_format') if default_date_format != 'iso8601': default_date_format = '' self.config.set('trac', 'default_date_format', default_date_format) default_dateinfo_format = req.args.get('default_dateinfo_format') if default_dateinfo_format not in ('relative', 'absolute'): default_dateinfo_format = 'relative' self.config.set('trac', 'default_dateinfo_format', default_dateinfo_format) _save_config(self.config, req, self.log) req.redirect(req.href.admin(cat, page)) default_timezone = self.config.get('trac', 'default_timezone') default_language = self.config.get('trac', 'default_language') default_date_format = self.config.get('trac', 'default_date_format') default_dateinfo_format = self.config.get('trac', 'default_dateinfo_format') data = { 'default_timezone': default_timezone, 'timezones': all_timezones, 'has_pytz': pytz is not None, 'default_language': default_language.replace('-', '_'), 'languages': languages, 'default_date_format': default_date_format, 'default_dateinfo_format': default_dateinfo_format, 'has_babel': Locale is not None, } Chrome(self.env).add_textarea_grips(req) return 'admin_basics.html', data
def filter_stream(self, req, method, filename, stream, data): if not self._TimerIsStillAlive(): # copied from main.py:310ff available = [locale_id.replace('_', '-') for locale_id in translation.get_available_locales()] preferred = req.session.get('language', req.languages) if not isinstance(preferred, list): preferred = [preferred] self._locale_string = Locale.negotiate(preferred, available, sep='-') self.log.debug("Negotiated locale: %s -> %s", preferred, self._locale_string) return stream
def render_admin_panel(self, req, cat, page, path_info): req.perm.require('TRAC_ADMIN') if Locale: locales = [ Locale.parse(locale) for locale in get_available_locales() ] languages = sorted( (str(locale), locale.display_name) for locale in locales) else: locales, languages = [], [] if req.method == 'POST': for option in ('name', 'url', 'descr'): self.config.set('project', option, req.args.get(option)) default_timezone = req.args.get('default_timezone') if default_timezone not in all_timezones: default_timezone = '' self.config.set('trac', 'default_timezone', default_timezone) default_language = req.args.get('default_language') if default_language not in locales: default_language = '' self.config.set('trac', 'default_language', default_language) default_date_format = req.args.get('default_date_format') if default_date_format != 'iso8601': default_date_format = '' self.config.set('trac', 'default_date_format', default_date_format) _save_config(self.config, req, self.log) req.redirect(req.href.admin(cat, page)) default_timezone = self.config.get('trac', 'default_timezone') default_language = self.config.get('trac', 'default_language') default_date_format = self.config.get('trac', 'default_date_format') data = { 'default_timezone': default_timezone, 'timezones': all_timezones, 'has_pytz': pytz is not None, 'default_language': default_language.replace('-', '_'), 'languages': languages, 'default_date_format': default_date_format, } Chrome(self.env).add_textarea_grips(req) return 'admin_basics.html', data
def render_admin_panel(self, req, cat, page, path_info): req.perm.require('TRAC_ADMIN') if Locale: locales = [Locale.parse(locale) for locale in get_available_locales()] languages = sorted((str(locale), locale.display_name) for locale in locales) else: locales, languages = [], [] if req.method == 'POST': for option in ('name', 'url', 'descr'): self.config.set('project', option, req.args.get(option)) default_timezone = req.args.get('default_timezone') if default_timezone not in all_timezones: default_timezone = '' self.config.set('trac', 'default_timezone', default_timezone) default_language = req.args.get('default_language') if default_language not in locales: default_language = '' self.config.set('trac', 'default_language', default_language) default_date_format = req.args.get('default_date_format') if default_date_format != 'iso8601': default_date_format = '' self.config.set('trac', 'default_date_format', default_date_format) _save_config(self.config, req, self.log) req.redirect(req.href.admin(cat, page)) default_timezone = self.config.get('trac', 'default_timezone') default_language = self.config.get('trac', 'default_language') default_date_format = self.config.get('trac', 'default_date_format') data = { 'default_timezone': default_timezone, 'timezones': all_timezones, 'has_pytz': pytz is not None, 'default_language': default_language.replace('-', '_'), 'languages': languages, 'default_date_format': default_date_format, 'has_babel': Locale is not None, } Chrome(self.env).add_textarea_grips(req) return 'admin_basics.html', data
def get_system_info(self): import genshi info = get_pkginfo(genshi).get('version') if hasattr(genshi, '_speedups'): info += ' (with speedups)' else: info += ' (without speedups)' yield 'Genshi', info try: import babel except ImportError: babel = None if babel is not None: info = get_pkginfo(babel).get('version') if not get_available_locales(): info += " (translations unavailable)" # No i18n on purpose self.log.warning("Locale data is missing") yield 'Babel', info
def runTest(self): """Test for regression of http://trac.edgewall.org/ticket/11337 The preferences panel will only be visible when Babel is installed or for a user that has `TRAC_ADMIN`. """ from trac.util.translation import has_babel, get_available_locales babel_hint = "Install Babel for extended language support." catalog_hint = "Message catalogs have not been compiled." language_select = '<select id="language" name="language">' disabled_language_select = ( '<select id="language" name="language" disabled="disabled" ' 'title="Translations are currently unavailable">' ) self._tester.go_to_preferences("Language") if has_babel: tc.notfind(babel_hint) if get_available_locales(): tc.find(language_select) tc.notfind(catalog_hint) else: tc.find(disabled_language_select) tc.find(catalog_hint) else: tc.find(babel_hint) tc.find(disabled_language_select) tc.notfind(catalog_hint) # For users without TRAC_ADMIN, the Language tab should only be # present when Babel is installed self._tester.go_to_preferences() language_tab = '<li id="tab_language">' try: self._tester.logout() if has_babel: tc.find(language_tab) tc.notfind(catalog_hint) else: tc.notfind(language_tab) finally: self._tester.login("admin")
def runTest(self): """Test for regression of http://trac.edgewall.org/ticket/11337 The preferences panel will only be visible when Babel is installed or for a user that has `TRAC_ADMIN`. """ from trac.util.translation import has_babel, get_available_locales babel_hint = "Install Babel for extended language support." catalog_hint = "Message catalogs have not been compiled." language_select = '<select name="language">' disabled_language_select = \ '<select name="language" disabled="disabled" ' \ 'title="Translations are currently unavailable">' self._tester.go_to_preferences("Language") if has_babel: tc.notfind(babel_hint) if get_available_locales(): tc.find(language_select) tc.notfind(catalog_hint) else: tc.find(disabled_language_select) tc.find(catalog_hint) else: tc.find(babel_hint) tc.find(disabled_language_select) tc.notfind(catalog_hint) # For users without TRAC_ADMIN, the Language tab should only be # present when Babel is installed self._tester.go_to_preferences() language_tab = '<li id="tab_language">' try: self._tester.logout() if has_babel: tc.find(language_tab) tc.notfind(catalog_hint) else: tc.notfind(language_tab) finally: self._tester.login('admin')
def render_preference_panel(self, req, panel): if req.method == "POST": if "restore" in req.args: self._do_load(req) else: self._do_save(req) req.redirect(req.href.prefs(panel or None)) data = { "settings": {"session": req.session, "session_id": req.session.sid}, "timezones": all_timezones, "timezone": get_timezone, "localtz": localtz, } if Locale: locales = map(Locale.parse, get_available_locales()) languages = sorted([(str(locale).replace("_", "-"), locale.display_name) for locale in locales]) data["locales"] = locales data["languages"] = languages return "prefs_%s.html" % (panel or "general"), data
def render_preference_panel(self, req, panel): if req.method == 'POST': if 'restore' in req.args: self._do_load(req) else: self._do_save(req) req.redirect(req.href.prefs(panel or None)) data = { 'settings': {'session': req.session, 'session_id': req.session.sid}, 'timezones': all_timezones, 'timezone': get_timezone, 'localtz': localtz } if Locale: locales = map(Locale.parse, get_available_locales()) languages = sorted([(str(locale).replace('_','-'), locale.display_name) for locale in locales]) data['locales'] = locales data['languages'] = languages return 'prefs_%s.html' % (panel or 'general'), data
def runTest(self): """Test for regression of http://trac.edgewall.org/ticket/11515 Show a notice message with new language setting after it is changed. """ from trac.util.translation import Locale, has_babel, \ get_available_locales from pkg_resources import resource_exists, resource_filename if not has_babel: print("SKIP: RegressionTestTicket11515 (Babel not installed)") return if not resource_exists('trac', 'locale'): return locale_dir = resource_filename('trac', 'locale') from babel.support import Translations string = 'Your preferences have been saved.' translated = None for second_locale_id in get_available_locales(): tx = Translations.load(locale_dir, second_locale_id) translated = tx.dgettext('messages', string) if string != translated: break # the locale has a translation else: print("SKIP: RegressionTestTicket11515 " "(Message catalogs not compiled)") return try: self._tester.go_to_preferences('Localization') tc.formvalue('userprefs', 'language', second_locale_id) tc.submit() tc.find(re.escape(translated)) tc.find('<option selected="selected" value="%s">' % second_locale_id) finally: tc.formvalue('userprefs', 'language', '') # revert to default tc.submit() tc.find('Your preferences have been saved')
def runTest(self): """Test for regression of http://trac.edgewall.org/ticket/11515 Show a notice message with new language setting after it is changed. """ from trac.util.translation import has_babel, get_available_locales if not has_babel: return for second_locale in (locale for locale in get_available_locales() if not locale.startswith('en_')): break else: return try: self._tester.go_to_preferences('Language') tc.formvalue('userprefs', 'language', second_locale) tc.submit() tc.notfind('Your preferences have been saved') finally: tc.formvalue('userprefs', 'language', '') # revert to default tc.submit() tc.find('Your preferences have been saved')
def _i18n_parse_date_patterns(): if not babel: return {} format_keys = { 'y': ('y', 'Y'), 'M': ('M',), 'd': ('d',), 'h': ('h', 'H'), 'm': ('m',), 's': ('s',), } patterns = {} for locale in get_available_locales(): regexp = [r'[0-9]+'] date_format = get_date_format('medium', locale=locale) time_format = get_time_format('medium', locale=locale) datetime_format = get_datetime_format('medium', locale=locale) formats = ( datetime_format.replace('{0}', time_format.format) \ .replace('{1}', date_format.format), date_format.format) orders = [] for format in formats: order = [] for key, chars in format_keys.iteritems(): for char in chars: idx = format.find('%(' + char) if idx != -1: order.append((idx, key)) break order.sort() order = dict((key, idx) for idx, (_, key) in enumerate(order)) orders.append(order) month_names = { 'jan': 1, 'feb': 2, 'mar': 3, 'apr': 4, 'may': 5, 'jun': 6, 'jul': 7, 'aug': 8, 'sep': 9, 'oct': 10, 'nov': 11, 'dec': 12, } if formats[0].find('%(MMM)s') != -1: for width in ('wide', 'abbreviated'): names = get_month_names(width, locale=locale) for num, name in names.iteritems(): name = name.lower() month_names[name] = num regexp.extend(month_names.iterkeys()) period_names = {'am': 'am', 'pm': 'pm'} if formats[0].find('%(a)s') != -1: names = get_period_names(locale=locale) for period, name in names.iteritems(): name = name.lower() period_names[name] = period regexp.extend(period_names.iterkeys()) patterns[locale] = { 'orders': orders, 'regexp': re.compile('(%s)' % '|'.join(regexp), re.IGNORECASE | re.UNICODE), 'month_names': month_names, 'period_names': period_names, } return patterns
def _get_available_locales(self): return sorted(locale for locale in translation.get_available_locales() if resource_exists('trac', 'locale/%s/LC_MESSAGES/messages.mo' % locale))
def _i18n_parse_date_patterns(): if not babel: return {} format_keys = { 'y': ('y', 'Y'), 'M': ('M', ), 'd': ('d', ), 'h': ('h', 'H'), 'm': ('m', ), 's': ('s', ), } patterns = {} for locale in get_available_locales(): regexp = [r'[0-9]+'] date_format = get_date_format('medium', locale=locale) time_format = get_time_format('medium', locale=locale) datetime_format = get_datetime_format('medium', locale=locale) formats = ( datetime_format.replace('{0}', time_format.format) \ .replace('{1}', date_format.format), date_format.format) orders = [] for format in formats: order = [] for key, chars in format_keys.iteritems(): for char in chars: idx = format.find('%(' + char) if idx != -1: order.append((idx, key)) break order.sort() order = dict((key, idx) for idx, (_, key) in enumerate(order)) orders.append(order) month_names = { 'jan': 1, 'feb': 2, 'mar': 3, 'apr': 4, 'may': 5, 'jun': 6, 'jul': 7, 'aug': 8, 'sep': 9, 'oct': 10, 'nov': 11, 'dec': 12, } if formats[0].find('%(MMM)s') != -1: for width in ('wide', 'abbreviated'): names = get_month_names(width, locale=locale) for num, name in names.iteritems(): name = name.lower() month_names[name] = num regexp.extend(month_names.iterkeys()) period_names = {'am': 'am', 'pm': 'pm'} if formats[0].find('%(a)s') != -1: names = get_period_names(locale=locale) for period, name in names.iteritems(): name = name.lower() period_names[name] = period regexp.extend(period_names.iterkeys()) patterns[locale] = { 'orders': orders, 'regexp': re.compile('(%s)' % '|'.join(regexp), re.IGNORECASE | re.UNICODE), 'month_names': month_names, 'period_names': period_names, } return patterns
names = get_period_names(locale=locale) for period, name in names.iteritems(): name = name.lower() period_names[name] = period regexp.extend(period_names.iterkeys()) return { 'orders': orders, 'regexp': re.compile('(%s)' % '|'.join(regexp), re.IGNORECASE | re.UNICODE), 'month_names': month_names, 'period_names': period_names, } _I18N_PARSE_DATE_PATTERNS = dict(map(lambda l: (l, False), get_available_locales())) def _i18n_parse_date(text, tzinfo, locale): locale = Locale.parse(locale) key = str(locale) pattern = _I18N_PARSE_DATE_PATTERNS.get(key) if pattern is False: pattern = _i18n_parse_date_pattern(locale) _I18N_PARSE_DATE_PATTERNS[key] = pattern if pattern is None: return None regexp = pattern['regexp'] period_names = pattern['period_names'] month_names = pattern['month_names'] text = text.lower()
def _get_available_locales(self): return sorted( locale for locale in translation.get_available_locales() if resource_exists('trac', 'locale/%s/LC_MESSAGES/messages.mo' % locale))
for period, name in names.iteritems(): name = name.lower() period_names[name] = period regexp.extend(period_names.iterkeys()) return { 'orders': orders, 'regexp': re.compile('(%s)' % '|'.join(regexp), re.IGNORECASE | re.UNICODE), 'month_names': month_names, 'period_names': period_names, } _I18N_PARSE_DATE_PATTERNS = dict( map(lambda l: (l, False), get_available_locales())) def _i18n_parse_date(text, tzinfo, locale): locale = Locale.parse(locale) key = str(locale) pattern = _I18N_PARSE_DATE_PATTERNS.get(key) if pattern is False: pattern = _i18n_parse_date_pattern(locale) _I18N_PARSE_DATE_PATTERNS[key] = pattern if pattern is None: return None regexp = pattern['regexp'] period_names = pattern['period_names'] month_names = pattern['month_names']