def configure_babel(app): # Load additional languages into the babel # cache. We do this so we can add Haitian Creole # using french as a model. babel_patched_load('cpf') # flask-babel babel = Babel(app) # Wire Babel into the settings # and the settings code into Babel settings_views.set_babel(babel) babel.localeselector(settings_views.current_locale) return babel
def i18n_init(app): """ initialize Flask-Babel """ babel = Babel(app) babel.localeselector(get_locale) babel.timezoneselector(get_timezone)
def test_human_name_is_i18n(self): app = self._get_flask() babel = Babel(app) with app.test_request_context('/'): babel.localeselector(lambda: 'fr') self.assertEquals(u'Météo', unicode(self._weather_app.human_name))
def make_app(): babel = Babel() security = Security() social = Social() user_datastore = SQLAlchemyUserDatastore(mdl.DB, mdl.User, mdl.Role) app = Flask(__name__) @login_failed.connect_via(app) def auto_add_user(sender, provider, oauth_response): connection_values = get_connection_values_from_oauth_response( provider, oauth_response) email = connection_values['email'] if not email or not email.strip(): email = '' if provider.name.lower() == 'facebook': fname = connection_values['full_name'] email = fb.get_email(oauth_response) elif provider.name.lower() == 'twitter': fname = connection_values['display_name'][1:] # cut off leading @ else: fname = connection_values['display_name'] user = user_datastore.create_user( email=email, name=fname, active=True, confirmed_at=datetime.now(), ) role_query = mdl.DB.session.query(mdl.Role).filter_by( name='authenticated') try: role = role_query.one() except NoResultFound: role = mdl.Role(name='authenticated') user.roles.append(role) user_datastore.commit() connection_values['user_id'] = user.id connect_handler(connection_values, provider) login_user(user) mdl.DB.session.commit() flash(gettext( 'Successfully linked login with {}. ' 'Ignore the message saying: "{}"').format( provider.name, '... account not associated with an existing user'), 'info') return redirect(url_for('root.profile')) app.user_datastore = user_datastore app.localconf = lconf = Config('mamerwiselen', 'lost-tracker', version='2.0', require_load=True) app.config['SECRET_KEY'] = lconf.get('app', 'secret_key') app.config['SQLALCHEMY_DATABASE_URI'] = lconf.get('db', 'dsn') mdl.DB.init_app(app) # Social connections social_connections = {} _add_social_params(social_connections, 'facebook', app.localconf) _add_social_params(social_connections, 'twitter', app.localconf) _add_social_params(social_connections, 'google', app.localconf) if len(social_connections) < 1: LOG.error('No Social/OAuth providers defined! Users will not be ' 'able to log-in!') app.config.update(social_connections) social.init_app(app, SQLAlchemyConnectionDatastore(mdl.DB, mdl.Connection)) security.init_app(app, user_datastore) app.register_blueprint(COMMENT, url_prefix=COMMENT_PREFIX) app.register_blueprint(GROUP, url_prefix=GROUP_PREFIX) app.register_blueprint(PHOTO, url_prefix=PHOTO_PREFIX) app.register_blueprint(QR, url_prefix=QR_PREFIX) app.register_blueprint(REGISTRATION, url_prefix=REGISTRATION_PREFIX) app.register_blueprint(ROOT) app.register_blueprint(STATION, url_prefix=STATION_PREFIX) app.register_blueprint(TABULAR, url_prefix=TABULAR_PREFIX) app.register_blueprint(USER, url_prefix=USER_PREFIX) babel.init_app(app) babel.localeselector(get_locale) return app
app.config.from_object('application.settings.Development') elif os.getenv('FLASK_CONF') == 'TEST': app.config.from_object('application.settings.Testing') else: app.config.from_object('application.settings.Production') # Enable jinja2 loop controls extension app.jinja_env.add_extension('jinja2.ext.loopcontrols') def localeselector(): return request.accept_languages.best_match(['en', 'ko', 'ja', 'zh']) babel.localeselector(localeselector) def get_context(): return { 'debug': False, 'service.name': app.config['SERVICE_NAME'], 'service.slogan': gettext(u'쉽고 빠른 파일 공유'), 'menu.home': gettext(u'처음으로'), 'menu.upload': gettext(u'업로드'), 'menu.about': gettext(u'소개'), 'menu.usecase': gettext(u'활용 방법'), 'menu.contact': gettext(u'연락하기'), 'file.add': gettext(u'파일 추가'), 'file.draghere': gettext(u'여기로 파일을 드래그 해주세요.'),
def init_app(app): babel = Babel(app) babel.localeselector(get_locale)
app.debug = config.DEBUG app.secret_key = config.SECRET_KEY app.config['SQLALCHEMY_DATABASE_URI'] = config.DB_CONNECTION app.config['UPLOAD_FOLDER'] = os.path.join(DIR, config.UPLOAD_FOLDER) app.config['BABEL_DEFAULT_LOCALE'] = 'en' if not os.path.isdir(app.config['UPLOAD_FOLDER']): os.makedirs(app.config['UPLOAD_FOLDER']) db = SQLAlchemy(app) babel = Babel(app) def get_locale(): return request.environ['LANG'] babel.localeselector(get_locale) DEBUG = config.DEBUG default_url_for = app.jinja_env.globals['url_for'] _truncate = app.jinja_env.filters['truncate'] def truncate(s, length=255, killwords=False, end='...'): if not s: return '' return _truncate(s, length=length, killwords=killwords, end=end) app.jinja_env.filters.update({'truncate': truncate}) lang_dir = {} for code, name, dir_, active in language_list:
def create_app(self): app = Flask(__name__) babel = Babel(app, default_locale='fr', default_timezone=USER_TZ) babel.localeselector(en_locale) babel.timezoneselector(user_tz) return app