def setup_i18n(languages=None): if not languages: languages = pylons.request.accept_language.best_matches() if languages: for lang in languages[:]: try: add_fallback(lang) except LanguageError: # if there is no resource bundle for this language # remove the language from the list languages.remove(lang) log.debug("Skip language %s: not supported", lang) # if any language is left, set the best match as a default if languages: try: set_lang(languages[0]) except LanguageError: log.debug("Language %s: not supported", languages[0]) else: log.debug("Set request language to %s", languages[0]) try: set_formencode_translation(languages) except LanguageError: log.debug("Language %s: not supported by FormEncode", languages[0]) else: log.debug("Set request language for FormEncode to %s", languages[0])
def user_language(user, fallbacks=[]): # find out the locale locale = None if user and user.locale: locale = user.locale if locale is None: locale = Locale.parse(Locale.negotiate(fallbacks, LOCALE_STRINGS_DASH, sep='-', aliases=A2_LOCALE_ALIASES)) \ or get_default_locale() # determinate from which path we load the translations translations_config = { 'pylons.paths': { 'root': _get_translations_root() }, 'pylons.package': config.get('pylons.package') } # set language and fallback set_lang(str(locale), pylons_config=translations_config) add_fallback(str(get_default_locale()), pylons_config=translations_config) formencode.api.set_stdtranslation(domain="FormEncode", languages=[locale.language]) return locale
def user_language(user, fallbacks=[]): # find out the locale locale = None if user and user.locale: locale = user.locale if locale is None: locales = map(str, LOCALES) locale = Locale.parse(Locale.negotiate(fallbacks, locales)) \ or get_default_locale() # determinate from which path we load the translations translations_module = config.get('adhocracy.translations', 'adhocracy') translations_module_loader = pkgutil.get_loader(translations_module) if translations_module_loader is None: raise ValueError(('Cannot import the module "%s" configured for ' '"adhocracy.translations". Make sure it is an ' 'importable module (and contains the ' 'translation files in a subdirectory ' '"i18n"') % translations_module) translations_root = translations_module_loader.filename translations_config = {'pylons.paths': {'root': translations_root}, 'pylons.package': config.get('pylons.package')} # set language and fallback set_lang(locale.language, pylons_config=translations_config) add_fallback(get_default_locale().language, pylons_config=translations_config) formencode.api.set_stdtranslation(domain="FormEncode", languages=[locale.language]) return locale
def __init__(self): lang = request.params.get('lang') if lang: if is_valid_string(lang): try: set_lang(os.path.basename(lang)) except LanguageError: pass else: for lang in request.languages: if is_valid_string(lang): try: set_lang(lang) except LanguageError: continue try: add_fallback('en') except LanguageError: pass self.PREFIXES = { 'mime': "m:", 'group': "g:", 'category': "c:", 'license': "l:", 'useflag': "u:", 'library': "so:", 'provided_library': "sop:", }
def __call__(self, environ, start_response): """Invoke the Controller.""" # set the language fallback to english add_fallback("en") # define the language based on browser preference user_agent_language = request.languages[0][0:2] set_lang(user_agent_language) formencode.api.set_stdtranslation(user_agent_language) # common values mostly inherited from config file c.version = __version__ # TODO move this into the development.ini file c.site_full_name = config["site_full_name"] # TODO move this into the development.ini file c.site_short_name = config["site_short_name"] # TODO move this into the development.ini file # controler and action named for use in templates #c.controller = request.environ['pylons.routes_dict']['controller'] #c.action = request.environ['pylons.routes_dict']['action'] # WSGIController.__call__ dispatches to the Controller method # the request is routed to. This routing information is # available in environ['pylons.routes_dict'] try: return WSGIController.__call__(self, environ, start_response) finally: meta.Session.remove()
def setup_i18n(): from pylons.i18n import add_fallback, set_lang, LanguageError languages = pylons.request.accept_language.best_matches() if languages: for lang in languages[:]: try: add_fallback(lang) except LanguageError: # if there is no resource bundle for this language # remove the language from the list languages.remove(lang) log.debug("Skip language %s: not supported", lang) # if any language is left, set the best match as a default if languages: set_lang(languages[0]) log.info("Set request language to %s", languages[0])
def __init__(self): lang = request.params.get('lang') if lang: if is_valid_string(lang): try: set_lang(os.path.basename(lang)) except LanguageError: pass else: for lang in request.languages: if is_valid_string(lang): try: set_lang(lang) except LanguageError: continue try: add_fallback('en') except LanguageError: pass
def user_language(user, fallbacks=[]): # find out the locale locale = None if user and user.locale: locale = user.locale if locale is None: locale = Locale.parse(Locale.negotiate(fallbacks, LOCALE_STRINGS)) \ or get_default_locale() # determinate from which path we load the translations translations_config = {'pylons.paths': {'root': _get_translations_root()}, 'pylons.package': config.get('pylons.package')} # set language and fallback set_lang(str(locale), pylons_config=translations_config) add_fallback(get_default_locale().language, pylons_config=translations_config) formencode.api.set_stdtranslation(domain="FormEncode", languages=[locale.language]) return locale
def __before__(self, action=None): if 'lang' in session: set_lang(session['lang']) #log.debug(request.languages) try: for lang in request.languages: if lang.lower() in ['zh-cn', 'zh']: add_fallback('zh') elif lang in ['en']: add_fallback(lang) except: pass ## Show exception and traceback info if getattr(g, 'catch_e', None): return redirect(url(controller='template', action='show_e')) else: g.catch_e = [] if isinstance(self.requires_auth, bool) and not self.requires_auth: pass elif isinstance(self.requires_auth, (list, tuple)) and \ not action in self.requires_auth: pass else: if 'user' not in session: session['path_before_login'] = request.path_info session.save() return redirect(url('login')) if hasattr(self, 'authz'): diff = self.authz.differ() if diff: c.global_message = _('Some one maybe you, has modified the svn authz file by hands. Please %(begin)ssave once%(end)s to fix possible config error.') % { 'begin': '<big><strong><a href="' + url(controller=self.__class__.__name__.lower()[0:-10], action="standardize") + '">', 'end': '</a></strong></big>' } c.global_message += "<blockquote>" + "<br>".join(diff.splitlines()) + "</blockquote>"