Exemple #1
0
def cmdline(argv):
    logging.basicConfig(level=logging.INFO)
    ap = argparse.ArgumentParser()
    ap.add_argument("config")
    args = ap.parse_args()

    try:
        import requests_cache
        requests_cache.install_cache(
            os.path.realpath('./mklocale.%s.cache' % hashlib.md5(args.config).hexdigest()),
            expire_after=86400
        )
    except ImportError:
        pass
    os.chdir(os.path.dirname(args.config))
    with open(args.config, "r") as infp:
        config = yaml.safe_load(infp)
    catalogs = []

    for tx_config in listify(config.get("transifex")):
        catalogs.extend(transifex.read_catalogs(tx_config))

    for local_config in listify(config.get("local")):
        catalogs.extend(local.read_catalogs(local_config))

    for merged_catalog in merge_by_language(catalogs):
        targets = [
            t.format(lang=merged_catalog.locale)
            for t
            in listify(config["target"])
        ]
        for target_file in targets:
            write_catalog(target_file, merged_catalog)
Exemple #2
0
def read_catalogs(tx_config):
    if isinstance(tx_config, six.string_types):
        tx_config = {"project": tx_config}
    project_slug = tx_config["project"]
    sess = requests.Session()
    resource_data = sess.get(
        "http://www.transifex.com/api/2/project/%s/resources/" % project_slug, auth=get_tx_auth()
    ).json()
    languages = listify(tx_config.get("languages"))
    resources = set(listify(tx_config.get("resources")))
    for resource_datum in resource_data:
        res_slug = resource_datum["slug"]
        if resources and res_slug not in resources:
            continue
        for lang_code in languages:
            log.info("Processing %s.%s (%s)" % (project_slug, res_slug, lang_code))
            xlate_resp = sess.get(
                "http://www.transifex.com/api/2/project/%s/resource/%s/translation/%s/"
                % (project_slug, res_slug, lang_code),
                params={"mode": "onlytranslated"},
                auth=get_tx_auth(),
            ).json()
            sio = six.StringIO(xlate_resp["content"])
            yield read_po(sio, domain=res_slug, locale=lang_code, charset="UTF-8")