def handle(self, *args, **kwargs): frontend.CommandLineInterface().run([ sys.argv[0], 'compile', '--statistics', '-d', os.path.join(settings.BASE_DIR, 'i18n'), '-D', 'django', ])
def setUp(self): self.datadir = os.path.join(os.path.dirname(__file__), 'data') self.orig_argv = sys.argv self.orig_stdout = sys.stdout self.orig_stderr = sys.stderr sys.argv = ['pybabel'] sys.stdout = StringIO() sys.stderr = StringIO() self.cli = frontend.CommandLineInterface()
def setUp(self): self.datadir = os.path.join(this_dir, 'data') self.orig_working_dir = os.getcwd() self.orig_argv = sys.argv self.orig_stdout = sys.stdout self.orig_stderr = sys.stderr sys.argv = ['pybabel'] sys.stdout = StringIO() sys.stderr = StringIO() os.chdir(self.datadir) self._remove_log_handlers() self.cli = frontend.CommandLineInterface()
def setUp(self): self.datadir = os.path.join(os.path.dirname(__file__), 'data') self.orig_argv = sys.argv self.orig_stdout = sys.stdout self.orig_stderr = sys.stderr sys.argv = ['pybabel'] sys.stdout = StringIO() sys.stderr = StringIO() # Logging handlers will be reused if possible (#227). This breaks the # implicit assumption that our newly created StringIO for sys.stderr # contains the console output. Removing the old handler ensures that a # new handler with our new StringIO instance will be used. log = logging.getLogger('babel') for handler in log.handlers: log.removeHandler(handler) self.cli = frontend.CommandLineInterface()
def main(args=None): parser = argparse.ArgumentParser(description='i18n hello world') parser.add_argument('lang') parsed_args = parser.parse_args(args) domain = 'messages' localedir = os.path.join(os.path.dirname(__file__), 'i18n') print(parsed_args) # If we're provided with a .mo file, just use that. if os.path.exists(parsed_args.lang): if parsed_args.lang.endswith('.mo'): print(f'Using .mo file at {parsed_args.lang}') message_object_file = parsed_args.lang elif parsed_args.lang.endswith('.po'): locale_dir = '.temp-locale' locale = os.path.splitext(os.path.basename(parsed_args.lang))[0] target_path = os.path.join(locale_dir, locale, 'LC_MESSAGES') if not os.path.exists(target_path): os.makedirs(target_path) frontend.CommandLineInterface().run([ "pybabel", "compile", f"--directory={locale_dir}", f"--input-file={parsed_args.lang}", f"--locale={locale}", ]) message_object_file = os.path.join(target_path, f"{locale}.mo") else: raise Exception(f"Unrecognized behavior for {parsed_args.lang}") with open(message_object_file, 'rb') as mo_file: gettext.GNUTranslations(mo_file).install() else: print(f"Loading translation for {parsed_args.lang} from {localedir}") gettext.translation( domain, localedir=localedir, fallback=True, # Use english if lang not found languages=[parsed_args.lang]).install() # I18N-NOTE: This is the standard greeting. print(_("Hello, world!"))
def makelang(*args): """usage: website makelang Compile the language strings used in the project and store them in _locale/website.pot.""" if "WEBSITE_WD" in os.environ and os.path.isdir(os.environ["WEBSITE_WD"]): os.chdir(os.environ["WEBSITE_WD"]) if "_config.py" not in os.listdir("."): logger.error("This seems to be no website project.") exit(1) try: from babel.messages import frontend except ImportError: logger.error("Cannot find pybabel.") exit(2) if not os.path.isdir("_locale"): os.mkdir("_locale") frontend.CommandLineInterface().run(["pybabel", "extract", "-F", os.path.dirname(__file__)+"/_locale/babel.cfg", "-o", "_locale/website.pot", ".", "_articles", "_templates"]) return 0