def load_meta(path, locale): meta = {} meta_path = os.path.join(path, "meta.yaml") if os.path.isfile(meta_path): try: meta = yaml.load_from_file(path=meta_path) except Exception: logging.getLogger(__name__).exception( "Could not load %s", meta_path) pass else: import datetime if "last_update" in meta and isinstance( meta["last_update"], datetime.datetime): meta["last_update"] = ( meta["last_update"] - datetime.datetime(1970, 1, 1)).total_seconds() loc = Locale.parse(locale) meta["locale"] = locale meta["locale_display"] = loc.display_name meta["locale_english"] = loc.english_name return meta
def load_meta(path, locale): meta = dict() meta_path = os.path.join(path, "meta.yaml") if os.path.isfile(meta_path): import yaml try: with io.open(meta_path, 'rt', encoding='utf-8') as f: meta = yaml.safe_load(f) except Exception: logging.getLogger(__name__).exception( "Could not load %s", meta_path) pass else: import datetime if "last_update" in meta and isinstance( meta["last_update"], datetime.datetime): meta["last_update"] = ( meta["last_update"] - datetime.datetime(1970, 1, 1)).total_seconds() l = Locale.parse(locale) meta["locale"] = locale meta["locale_display"] = l.display_name meta["locale_english"] = l.english_name return meta
def _build_language_list(self): default_locale = Locale.parse(self.babel.app.config['BABEL_DEFAULT_LOCALE']) session_locale = get_locale() first_choice = (session_locale.language, session_locale.language_name) choices = set(((translation.language, translation.language_name) for translation in self.babel.list_translations())) choices.add(first_choice) choices.add((default_locale.language, default_locale.language_name)) choices.remove(first_choice) return [first_choice] + list(choices)
def get_locale(): # if a user is logged in, use the locale from the user settings user = getattr(g, 'user', None) if user is not None and hasattr(user, "locale"): if user.name != 'Guest': # if the account is the guest account bypass the config lang settings return user.locale preferred = list() if request.accept_languages: for x in request.accept_languages.values(): try: preferred.append(str(Locale.parse(x.replace('-', '_')))) except (UnknownLocaleError, ValueError) as e: log.debug('Could not parse locale "%s": %s', x, e) return negotiate_locale(preferred or ['en'], get_available_translations())
def change_language(): """ Changes the session language locale and refreshes the Flask app context """ locale = request.form.get("locale") session["language"] = locale ractl.locale = session["language"] file_cmds.locale = session["language"] refresh() language = Locale.parse(locale) language_name = language.get_language_name(locale) flash( _("Changed Web Interface language to %(locale)s", locale=language_name)) return redirect(url_for("index"))
def load_meta(path, locale): meta = dict() meta_path = os.path.join(path, "meta.yaml") if os.path.isfile(meta_path): import yaml try: with open(meta_path) as f: meta = yaml.safe_load(f) except: pass else: import datetime if "last_update" in meta and isinstance(meta["last_update"], datetime.datetime): meta["last_update"] = (meta["last_update"] - datetime.datetime(1970,1,1)).total_seconds() l = Locale.parse(locale) meta["locale"] = locale meta["locale_display"] = l.display_name meta["locale_english"] = l.english_name return meta
def init_app(app): babel = Babel(app) app.config['LANGUAGES'] = {} for language in app.config['SUPPORTED_LANGUAGES']: app.config['LANGUAGES'][language] = Locale.parse( language).language_name @app.after_request def call_after_request_callbacks(response): # pylint: disable=unused-variable for callback in getattr(g, 'after_request_callbacks', ()): callback(response) return response def after_this_request(f): if not hasattr(g, 'after_request_callbacks'): g.after_request_callbacks = [] g.after_request_callbacks.append(f) return f @babel.localeselector def get_locale(): # pylint: disable=unused-variable supported_languages = app.config['SUPPORTED_LANGUAGES'] language_arg = request.args.get('l') if language_arg is not None: if language_arg in supported_languages: @after_this_request def remember_language(response): # pylint: disable=unused-variable response.set_cookie('language', language_arg) return language_arg else: language_cookie = request.cookies.get('language') if language_cookie in supported_languages: return language_cookie return request.accept_languages.best_match(supported_languages)
def init_app(app): babel = Babel(app) app.config['LANGUAGES'] = {} for language in app.config['SUPPORTED_LANGUAGES']: app.config['LANGUAGES'][ language] = Locale.parse(language).language_name @app.after_request def call_after_request_callbacks(response): for callback in getattr(g, 'after_request_callbacks', ()): callback(response) return response def after_this_request(f): if not hasattr(g, 'after_request_callbacks'): g.after_request_callbacks = [] g.after_request_callbacks.append(f) return f @babel.localeselector def get_locale(): supported_languages = app.config['SUPPORTED_LANGUAGES'] language_arg = request.args.get('l') if language_arg is not None: if language_arg in supported_languages: @after_this_request def remember_language(response): response.set_cookie('language', language_arg) return language_arg else: language_cookie = request.cookies.get('language') if language_cookie in supported_languages: return language_cookie return request.accept_languages.best_match(supported_languages)
def load_meta(path, locale): meta = dict() meta_path = os.path.join(path, "meta.yaml") if os.path.isfile(meta_path): import yaml try: with open(meta_path) as f: meta = yaml.safe_load(f) except: pass else: import datetime if "last_update" in meta and isinstance( meta["last_update"], datetime.datetime): meta["last_update"] = ( meta["last_update"] - datetime.datetime(1970, 1, 1)).total_seconds() l = Locale.parse(locale) meta["locale"] = locale meta["locale_display"] = l.display_name meta["locale_english"] = l.english_name return meta
def get_user_locale_language(user_language): return Locale.parse(user_language).get_language_name(get_locale())