def main(): # options parse_command_line() # Locale load_gettext_translations(settings.I18N_PATH, "luoyun") set_default_locale('zh_CN') logging.info("starting torando web server") save_pid(delete_exist=True) if settings.IPV4_ONLY: import socket sockets = bind_sockets(options.port, family=socket.AF_INET) else: sockets = bind_sockets(options.port) if not settings.DEBUG: import tornado.process tornado.process.fork_processes(0) application = Application() server = HTTPServer(application, xheaders=True) server.add_sockets(sockets) save_pid() IOLoop.instance().start()
def application(master, language, debug): if os.environ.get("AS_TLINJECT_SECRET", ""): print("TLInject is enabled for this server.") locale.set_default_locale("en") locale.load_gettext_translations(readonly_app_path("gettext"), "tornado") strings = static_strings() db_coordinator = database.DatabaseCoordinator() application = Application( dispatch.ROUTES, db_coordinator=db_coordinator, master=libcard2.master.MasterData(master), string_access=create_dict_aggregator(master, language), image_server=os.environ.get("AS_IMAGE_SERVER"), tlinject_context=tlinject.TLInjectContext(db_coordinator), news_context=news.NewsDatabase(db_coordinator), card_tracking=card_tracking.CardTrackingDatabase(db_coordinator), event_tracking=event_tracker.EventTrackingDatabase(db_coordinator), template_path=readonly_app_path("webui"), runtime_info=create_runtime_info(), tlinject_secret=os.environ.get("AS_TLINJECT_SECRET", "").encode("utf8"), ui_methods=pageutils.UI_METHODS, static_path=readonly_app_path("static"), static_strings=strings, debug=debug, autoreload=debug, ) return application
def translate(word, lang=None): lang = lang or settings.DEFAULT_LANG language = mapping.get(lang) or lang locale.load_gettext_translations( directory="/Users/sestari/Documents/brainiak_api/locale", domain="brainiak") user_locale = locale.get(language) return user_locale.translate(word)
def setup_locale(self): if not hasattr(settings, 'LOCALE'): return assert 'code' in settings.LOCALE assert 'path' in settings.LOCALE assert 'domain' in settings.LOCALE locale_code = settings.LOCALE['code'] locale.set_default_locale(locale_code) locale.load_gettext_translations(settings.LOCALE['path'], settings.LOCALE['domain']) self.define_current_locale(locale_code)
def setup(): """ Configure the settings (this happens as a side effect of accessing the first setting), configure logging and default locale. """ from anthill.framework.conf import settings from anthill.framework.utils.log import configure_logging from tornado.locale import set_default_locale, load_gettext_translations configure_logging(settings.LOGGING_CONFIG, settings.LOGGING) if settings.USE_I18N: set_default_locale(settings.LOCALE) load_gettext_translations('locale', 'messages')
def __init__(self, config, port): self.config = config self.port = port self.routes = [] self.token_encoder = token.TokenEncoder(self.config) self.writing_queue = queues.Queue() self.db = sqlite.SQLiteDB(self.config.db.sqlite_path, self.token_encoder) logging.info('Opened db {}'.format(self.config.db.sqlite_path)) self.make_app() self.callbacks = [ queue_writer.QueueWriter(self.writing_queue, self.db) ] locale_path = Path(__file__).parent.parent.joinpath('locale') locale.load_gettext_translations(locale_path, 'messages')
def __init__(self, **settings): setdefault(settings, 'cookie_secret', random_string(128)) settings['ui_modules'] = \ self._parse_ui_elements(settings.get('ui_modules', {})) settings['ui_methods'] = \ self._parse_ui_elements(settings.get('ui_methods', {})) settings['transforms'] = \ self._parse_transforms(settings.get('transforms', None)) locale_path = is_folder(settings['locale_path']) if len(os.listdir(locale_path)): locale.load_gettext_translations(locale_path, settings['domain']) handlers = [] if settings.get('handlers', False): handlers.extend(self._parse_handlers_list(settings['handlers'])) not_found_handler = NotFoundHandler if not settings['api'] \ else APINotFoundHandler handlers.append((r'/.*', not_found_handler)) if 'handlers' in settings: del settings['handlers'] super(ServerApplication, self).__init__(handlers, **settings) self._start_time = time.time()
def application(master, language, debug): if os.environ.get("AS_TLINJECT_SECRET", ""): print("TLInject is enabled for this server.") if not os.environ.get("AS_COOKIE_SECRET"): raise ValueError("You need to set AS_COOKIE_SECRET in the environment.") locale.set_default_locale("en") locale.load_gettext_translations(readonly_app_path("gettext"), "tornado") strings = static_strings() db_coordinator = database.DatabaseCoordinator() have_preamble_extra = os.path.exists(readonly_app_path("webui", "t_preamble_extra.html")) have_footer_extra = os.path.exists(readonly_app_path("webui", "t_footer_extra.html")) vi_class = namedtuple("runtime_info_t", ("app_revision", "host_id")) runtime_info = vi_class(os.environ.get("AS_GIT_REVISION"), os.environ.get("AS_HOST_ID")) application = Application( dispatch.ROUTES, db_coordinator=db_coordinator, master=libcard2.master.MasterData(master), more_masters=create_more_masters(), string_access=create_dict_aggregator(master, language), image_server=os.environ.get("AS_IMAGE_SERVER"), template_path=readonly_app_path("webui"), runtime_info=runtime_info, tlinject_secret=os.environ.get("AS_TLINJECT_SECRET", "").encode("utf8"), ui_methods=pageutils.UI_METHODS, static_path=readonly_app_path("static"), static_strings=strings, debug=debug, autoreload=debug, wds_host=os.environ.get("AS_WDS_HOST", "//localhost:5002") if debug else None, have_preamble_extra=have_preamble_extra, have_footer_extra=have_footer_extra, cookie_secret=os.environ.get("AS_COOKIE_SECRET"), feedback_link=os.environ.get("AS_FEEDBACK_URL"), ) return application
def get(self): return self.render('page.html', form=self.form) def post(self): # just mock the form processing # as it will always raise error self.form.process(data={'username': '******'}) self.form.validate() return self.render('page.html', form=self.form) def make_app(): return web.Application([ (r'/', MainHandler), ], template_path=os.path.dirname(__file__), debug=True) if __name__ == '__main__': locale_dir = os.path.join(os.path.dirname(__file__), 'locale') locale_domain = 'test' locale.load_gettext_translations(locale_dir, locale_domain) app = make_app() app.listen(8888) print("Listening on post 8888 ...") ioloop.IOLoop.current().start()
# coding=utf-8 """runtime context package """ from __future__ import absolute_import, division, print_function, with_statement import weakref # 理论上来说,下面这段代码应该写在`tools/locale.py`里 from tornado.locale import Locale, load_gettext_translations, set_default_locale import settings from tools import get_session_class, connection load_gettext_translations(settings.TRANSLATIONS_DIRECTORY, settings.TRANSLATIONS_DOMAIN) set_default_locale(settings.DEFAULT_LOCALE) __author__ = "Chuanchuan Tu" __copyright__ = "Copyright 2016, Kanjian" __credits__ = ["Chuanchuan Tu"] __license__ = "Apache" __version__ = "2.0" __maintainer__ = "Chuanchuan Tu" __email__ = "*****@*****.**" __status__ = "Production" __all__ = ["RuntimeContext"] class RuntimeContext(object): """运行时上下文
def get(self): locale.load_gettext_translations(directory="locale", domain="brainiak") user_locale = self.get_browser_locale() _ = user_locale.translate self.finalize(_("WORKING"))
from tornado.locale import get_supported_locales, load_gettext_translations from babel import Locale from tobot.helpers import pgettext, Emoji load_gettext_translations('./locales', 'boterator') DEFAULT_SLAVE_SETTINGS = { 'delay': 15, 'votes': 2, 'vote_timeout': 24, 'text_min': 50, 'text_max': 1000, 'start': pgettext('Default start message', "Just enter your message, and we're ready."), 'hello': pgettext( 'Default channel-hello message', 'Hi there, guys! Now it is possible to publish messages in this channel by ' 'any of you. All you need to do - is to write a message to me (bot named ' '@{bot_username}), and it will be published after verification by our team.' ), 'public_vote': True, 'power': False,