Skip to content

Instantly share code, notes, and snippets.

@dirkjanfaber
Created April 28, 2021 14:50
Show Gist options
  • Save dirkjanfaber/e9976ea60209d7beb5d429836116a50e to your computer and use it in GitHub Desktop.
Save dirkjanfaber/e9976ea60209d7beb5d429836116a50e to your computer and use it in GitHub Desktop.
Download and convert the po file for a given language from the poeditor.com file.
#!/usr/bin/env python3
from poeditor import POEditorAPI
import os
import subprocess
import time
import argparse
project_id = "435155"
filename = "victronconnect"
def getGitRoot():
return subprocess.Popen(['git', 'rev-parse', '--show-toplevel'], stdout=subprocess.PIPE).communicate()[0].rstrip().decode('utf-8')
parser = argparse.ArgumentParser()
parser.add_argument('-p', '--project', nargs=1, help='project id (default: '+project_id+')')
parser.add_argument('-f', '--filename', nargs=1, help='filename prefix (default: '+filename+')')
parser.add_argument('-l', '--languages', action='append', help='the code of the language(s) to download (default: all)')
args = parser.parse_args()
if args.project:
project_id=args.project[0]
if args.filename:
filename=args.filename[0]
# we need to get the API token from the environment
try:
os.environ['POEDITOR_TOKEN']
except:
raise SystemExit("### Please set POEDITOR_TOKEN environment variable")
client = POEditorAPI(os.environ.get('POEDITOR_TOKEN', None))
# Fetch the languages for this project
project = client.view_project_details( project_id )
for language in client.list_project_languages( project_id ):
if args.languages and not language["code"] in args.languages:
# skip language
continue
print("Language: %s (%s)" % ( language["name"], language["code"] ) )
# Skip English as that is the source
if language["name"] == 'english':
print("Skipping English as that is the source language")
continue
po_file = "%s/lang/%s_%s.po" % ( getGitRoot(), filename, language["code"] )
print("- exporting po file [%s]" % po_file)
client.export( project_id, language["code"], 'po', local_file=po_file )
# Convert to .ts file. Do note that the .ts file for Chinese uses
# code 'zh', while POEditor uses 'zh-CN', hence the split
ts_file = "%s/lang/%s_%s.ts" % ( getGitRoot(), filename, language["code"].split('-')[0] )
print("- converting po file to ts file [%s]" % ts_file)
try:
subprocess.run(["lconvert", po_file, "-o", ts_file])
except:
raise SystemExit("### Failed to convert the .po file to [%s] file!" % ts_file.split('/')[-1] )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment