Hello:
  
   I have code a tool: po-export,  and think it is useful, suggest merge.


# HG changeset patch
# User Feng Shu <tuma...@163.com>
# Date 1688551344 -28800
#      Wed Jul 05 18:02:24 2023 +0800
# Node ID 3e48363e9b8259a860d1f17c1e51a074fee28f8e
# Parent  543fc8aa87b3393e5fdcbe254396019d41ba2408
Add po-export tool to tryton/script directory

diff -r 543fc8aa87b3 -r 3e48363e9b82 tryton/scripts/po-export/po-export.py
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tryton/scripts/po-export/po-export.py	Wed Jul 05 18:02:24 2023 +0800
@@ -0,0 +1,108 @@
+#!/usr/bin/env python3
+
+import sys
+import os
+from optparse import OptionParser
+from proteus import config, Model, Wizard
+
+def main(options):
+    database = options.db
+    user = options.user
+    language = options.lang
+    connect_health_server(database, user)
+    extract_en_translations()
+    cleanup_translations()
+    update_translations_from_en(language)
+    delete_useless_translations(language)
+    export_all_translations(language)
+
+def connect_health_server(database, user):
+    print("Connecting to database '{}' with '{}' ...".format(database, user))
+    config.set_trytond(database=database, user=user)
+
+def extract_en_translations():
+    print("Extracting en translations from models, views, reports ...")
+    translation_set = Wizard('ir.translation.set')
+    translation_set.execute('set_')
+
+def cleanup_translations():
+    print("Cleaning up translations ...")
+    translation_clean = Wizard('ir.translation.clean')
+    translation_clean.execute('clean')
+
+def update_translations_from_en(language):
+    print("Syncing {0} translations with en translations.".format(language))
+    Lang = Model.get('ir.lang')
+    translation_update = Wizard('ir.translation.update')
+    translation_update.form.language, = Lang.find([('code', '=', language)])
+    translation_update.execute('update')
+
+def delete_useless_translations(language):
+    for lang in ['en', language]:
+        useless_translations = [
+            # Module            Field                          Source
+            ('health_caldav',  'calendar.event,vevent',       'vevent'),
+            ('health_caldav',  'calendar.event.alarm,valarm', 'valarm'),
+            ('health_%',       '%',                           'LibreOffice/%'),
+            ('health',         'patient.medication',          'iVBORw0KGgoAAAANSU%')]
+        Translation = Model.get('ir.translation')
+        for module, field, source in useless_translations:
+            translations = Translation.find([
+                ('lang', '=', lang),
+                ('module', 'ilike', module),
+                ('name', 'ilike', field),
+                ('src', 'ilike', source)])
+            for translation in translations:
+                print("Deleting {} translation of '{}' in '{}' ...".format(lang, source, translation.name))
+                translation.delete()
+ 
+def export_all_translations(language):
+    print("Starting export {0} translations of gnuhealth modules ...".format(language))
+    for module in get_all_health_module_names():
+        po_file = get_po_file_path(module, language)
+        print("## Exporting to '{0}'".format(po_file))
+        export_translation(language, module, po_file)
+    print("Finish to export!")
+
+def get_all_health_module_names():
+    Module = Model.get('ir.module')
+    modules = Module.find([('name', 'like', "health%"),
+                           ('state', 'in', ['activated'])])
+    return [module.name for module in modules]
+    
+def get_po_file_path(module_name, language):
+    script_dir = os.path.abspath(os.path.dirname(__file__))
+    path = script_dir + "/../../" + module_name + "/locale/" + language + ".po"
+    return path
+
+def export_translation(lang, module, po_file):
+    Lang = Model.get('ir.lang')
+    Module = Model.get('ir.module')
+    translation_export = Wizard('ir.translation.export')
+    translation_export.form.language, = Lang.find([('code', '=', lang)])
+    translation_export.form.module, = Module.find([('name', '=', module)])
+    translation_export.execute('export')
+    data = translation_export.form.file
+    if data is None:
+        print("   WARN: Module {0} have no translation for languate {1}.\n".format(module, lang))
+    else:
+        os.makedirs(os.path.dirname(po_file), exist_ok=True)
+        with open(po_file, 'wb') as binary_file:
+            binary_file.write(translation_export.form.file)
+    translation_export.execute('end')
+
+if __name__ == '__main__':
+    parser = OptionParser("%prog [options]")
+    parser.add_option('-d', '--database', dest='db')
+    parser.add_option('-u', '--user', dest='user')
+    parser.add_option('-l', '--language', dest='lang')
+    parser.set_defaults(user='admin', db='', lang='')
+
+    options, module_path = parser.parse_args()
+    if not options.db:
+        parser.error('You must define a database')
+    if not options.lang:
+        parser.error('You must set a language, for example: zh_CN')
+
+    main(options)
+
diff -r 543fc8aa87b3 -r 3e48363e9b82 tryton/scripts/po-export/po-export.sh
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tryton/scripts/po-export/po-export.sh	Wed Jul 05 18:02:24 2023 +0800
@@ -0,0 +1,58 @@
+#!/bin/bash
+source $HOME/.gnuhealthrc
+
+help()
+{
+    cat << EOF
+
+GNU Health HMIS po files export tool
+
+usage: `basename $0` LANG
+
+    Example:
+    $ bash ./po-export.sh zh_CN
+
+EOF
+    exit 0
+}
+
+if [ $# -eq 0 ]; then
+    help
+fi
+
+LANGUAGE=$1
+TRYTON_DATABASE="po-export-db"
+TRYTON_SERVER_DIR=${GNUHEALTH_DIR}/tryton/server
+TRYTOND_ADMIN_CMD="${TRYTON_SERVER_DIR}/trytond-${TRYTON_VERSION}/bin/trytond-admin --email admin -d ${TRYTON_DATABASE} --all"
+
+echo ""
+echo "+--------------------------------------------+"
+echo "|    GNU Health HMIS po files export tool    |"
+echo "+--------------------------------------------+"
+echo ""
+
+echo "## Creating database '${TRYTON_DATABASE}' ..."
+dropdb --if-exists ${TRYTON_DATABASE} >/dev/null
+createdb ${TRYTON_DATABASE}
+
+echo "## Creating admin password file ..."
+password_file=$(mktemp -t gnuhealth-tempfile.XXXXXX)
+echo "gnuhealth" > "${password_file}"
+export TRYTONPASSFILE="${password_file}"
+
+echo "## Running trytond-admin command to update DB (1. Init setup) ..."
+${TRYTOND_ADMIN_CMD}
+
+modules_ignored="('health_icd10pcs','health_icd11')"
+echo "## Active all modules with psql command,except $modules_ignored ..."
+psql -q -c "UPDATE ir_module SET state = 'to activate' WHERE name NOT IN $modules_ignored" ${TRYTON_DATABASE}
+
+echo "## Running trytond-admin command to update DB (2. Active modules) ..."
+${TRYTOND_ADMIN_CMD}
+psql -q -c "UPDATE ir_translation SET value = ''" ${TRYTON_DATABASE}
+
+echo "## Running trytond-admin command to update DB (3. Active language) ..."
+${TRYTOND_ADMIN_CMD} --language ${LANGUAGE}
+
+echo "## Export po files ..."
+python3 po-export.py --user admin --database ${TRYTON_DATABASE} --language ${LANGUAGE}

-- 

Reply via email to