def test_locales_with_no_plural_rules_have_default(): from py3babel import Locale aa_plural = Locale.parse("aa").plural_form assert aa_plural(1) == "other" assert aa_plural(2) == "other" assert aa_plural(15) == "other"
def run(self, argv=sys.argv): """Main entry point of the command-line interface. :param argv: list of arguments passed on the command-line """ self.parser = OptionParser(usage=self.usage % ("command", "[args]"), version=self.version) self.parser.disable_interspersed_args() self.parser.print_help = self._help self.parser.add_option( "--list-locales", dest="list_locales", action="store_true", help="print all known locales and exit" ) self.parser.add_option( "-v", "--verbose", action="store_const", dest="loglevel", const=logging.DEBUG, help="print as much as possible", ) self.parser.add_option( "-q", "--quiet", action="store_const", dest="loglevel", const=logging.ERROR, help="print as little as possible", ) self.parser.set_defaults(list_locales=False, loglevel=logging.INFO) options, args = self.parser.parse_args(argv[1:]) self._configure_logging(options.loglevel) if options.list_locales: identifiers = localedata.locale_identifiers() longest = max([len(identifier) for identifier in identifiers]) identifiers.sort() format = u"%%-%ds %%s" % (longest + 1) for identifier in identifiers: locale = Locale.parse(identifier) output = format % (identifier, locale.english_name) print(output.encode(sys.stdout.encoding or getpreferredencoding() or "ascii", "replace")) return 0 if not args: self.parser.error("no valid command or option passed. " "Try the -h/--help option for more information.") cmdname = args[0] if cmdname not in self.commands: self.parser.error('unknown command "%s"' % cmdname) return getattr(self, cmdname)(args[1:])
def test_parse_likely_subtags(self): l = Locale.parse('zh-TW', sep='-') assert l.language == 'zh' assert l.territory == 'TW' assert l.script == 'Hant' l = Locale.parse('zh_CN') assert l.language == 'zh' assert l.territory == 'CN' assert l.script == 'Hans' l = Locale.parse('zh_SG') assert l.language == 'zh' assert l.territory == 'SG' assert l.script == 'Hans' l = Locale.parse('und_AT') assert l.language == 'de' assert l.territory == 'AT' l = Locale.parse('und_UK') assert l.language == 'en' assert l.territory == 'GB' assert l.script is None
def finalize_options(self): if not self.input_file: raise DistutilsOptionError("you must specify the input file") if not self.locale: raise DistutilsOptionError("you must provide a locale for the " "new catalog") try: self._locale = Locale.parse(self.locale) except UnknownLocaleError as e: raise DistutilsOptionError(e) if not self.output_file and not self.output_dir: raise DistutilsOptionError("you must specify the output directory") if not self.output_file: self.output_file = os.path.join(self.output_dir, self.locale, "LC_MESSAGES", self.domain + ".po") if not os.path.exists(os.path.dirname(self.output_file)): os.makedirs(os.path.dirname(self.output_file)) if self.no_wrap and self.width: raise DistutilsOptionError("'--no-wrap' and '--width' are mutually " "exclusive") if not self.no_wrap and not self.width: self.width = 76 elif self.width is not None: self.width = int(self.width)
def test_parse(self): l = Locale.parse('de-DE', sep='-') assert l.display_name == 'Deutsch (Deutschland)' de_DE = Locale.parse(l) assert (de_DE.language, de_DE.territory) == ('de', 'DE')
def test_datetime_format_get_week_number(): format = dates.DateTimeFormat(date(2006, 1, 8), Locale.parse("de_DE")) assert format.get_week_number(6) == 1 format = dates.DateTimeFormat(date(2006, 1, 8), Locale.parse("en_US")) assert format.get_week_number(6) == 2
def init(self, argv): """Subcommand for creating new message catalogs from a template. :param argv: the command arguments """ parser = OptionParser(usage=self.usage % ("init", ""), description=self.commands["init"]) parser.add_option("--domain", "-D", dest="domain", help="domain of PO file (default '%default')") parser.add_option("--input-file", "-i", dest="input_file", metavar="FILE", help="name of the input file") parser.add_option("--output-dir", "-d", dest="output_dir", metavar="DIR", help="path to output directory") parser.add_option( "--output-file", "-o", dest="output_file", metavar="FILE", help="name of the output file (default " "'<output_dir>/<locale>/LC_MESSAGES/" "<domain>.po')", ) parser.add_option( "--locale", "-l", dest="locale", metavar="LOCALE", help="locale for the new localized catalog" ) parser.add_option("-w", "--width", dest="width", type="int", help="set output line width (default 76)") parser.add_option( "--no-wrap", dest="no_wrap", action="store_true", help="do not break long message lines, longer than " "the output line width, into several lines", ) parser.set_defaults(domain="messages") options, args = parser.parse_args(argv) if not options.locale: parser.error("you must provide a locale for the new catalog") try: locale = Locale.parse(options.locale) except UnknownLocaleError as e: parser.error(e) if not options.input_file: parser.error("you must specify the input file") if not options.output_file and not options.output_dir: parser.error("you must specify the output file or directory") if not options.output_file: options.output_file = os.path.join( options.output_dir, options.locale, "LC_MESSAGES", options.domain + ".po" ) if not os.path.exists(os.path.dirname(options.output_file)): os.makedirs(os.path.dirname(options.output_file)) if options.width and options.no_wrap: parser.error("'--no-wrap' and '--width' are mutually exclusive.") elif not options.width and not options.no_wrap: options.width = 76 infile = open(options.input_file, "r") try: # Although reading from the catalog template, read_po must be fed # the locale in order to correctly calculate plurals catalog = read_po(infile, locale=options.locale) finally: infile.close() catalog.locale = locale catalog.revision_date = datetime.now(LOCALTZ) self.log.info("creating catalog %r based on %r", options.output_file, options.input_file) outfile = open(options.output_file, "wb") try: write_po(outfile, catalog, width=options.width) finally: outfile.close()