--- a/svghmi/i18n.py Tue Feb 16 11:35:56 2021 +0100
+++ b/svghmi/i18n.py Tue Feb 16 11:47:52 2021 +0100
@@ -64,9 +64,10 @@
w.write(POT_file)
def ReadTranslations(dirpath):
- """ Read all PO files from a directory and return a list of (lang, translation_dict) tuples """
+ """ Read all PO files from a directory and return a list of (langcode, translation_dict) tuples """
po_files = [fname for fname in os.listdir(dirpath) if fname.endswith(".po")]
+ po_files.sort()
translations = []
for po_fname in po_files:
@@ -86,34 +87,35 @@
broken_lang = set()
for msgid,label,svgid in messages:
translated_message = []
- for lang,translation in translations:
+ for langcode,translation in translations:
msg = translation.pop(msgid, None)
if msg is None:
- broken_lang.add(lang)
+ broken_lang.add(langcode)
errcallback(_('{}: Missing translation for "{}" (label:{}, id:{})\n').format(lang,msgid,label,svgid))
translated_message.append(msgid)
else:
translated_message.append(msg)
translated_messages.append((msgid,translated_message))
langs = []
- for lang,translation in translations:
+ for langcode,translation in translations:
try:
- l,c = lang.split("_")
+ l,c = langcode.split("_")
language_name = pycountry.languages.get(alpha_2 = l).name
country_name = pycountry.countries.get(alpha_2 = c).name
- langs.append("{} ({})".format(language_name, country_name))
+ langname = "{} ({})".format(language_name, country_name)
except:
try:
- language_name = pycountry.languages.get(alpha_2 = lang).name
- langs.append(language_name)
+ langname = pycountry.languages.get(alpha_2 = langcode).name
except:
- langs.append(lang)
+ langname = langcode
+
+ langs.append((langname,langcode))
broken = False
for msgid, msg in translation.iteritems():
broken = True
- errcallback(_('{}: Unused translation "{}":"{}"\n').format(lang,msgid,msg))
- if broken or lang in broken_lang:
+ errcallback(_('{}: Unused translation "{}":"{}"\n').format(langcode,msgid,msg))
+ if broken or langcode in broken_lang:
errcallback(_('Translation for {} is outdated, please edit {}.po, click "Catalog -> Update from POT File..." and select messages.pot.\n').format(lang,lang))
@@ -125,9 +127,9 @@
result = etree.Element("translations")
langsroot = etree.SubElement(result, "langs")
- for lang in langs:
- langel = etree.SubElement(langsroot, "lang")
- langel.text = lang
+ for name, code in langs:
+ langel = etree.SubElement(langsroot, "lang", {"code":code})
+ langel.text = name
msgsroot = etree.SubElement(result, "messages")
for msgid, msgs in translated_messages:
--- a/svghmi/i18n.ysl2 Tue Feb 16 11:35:56 2021 +0100
+++ b/svghmi/i18n.ysl2 Tue Feb 16 11:47:52 2021 +0100
@@ -20,9 +20,9 @@
emit "preamble:i18n" {
const "translations", "ns:GetTranslations($translatable_strings)";
- > var langs = [ "default",
+ > var langs = [ ["Default", "C"],
foreach "$translations/langs/lang" {
- > "«.»"
+ > ["«.»","«@code»"]
if "position()!=last()" > ,
}
| ];
--- a/svghmi/svghmi.js Tue Feb 16 11:35:56 2021 +0100
+++ b/svghmi/svghmi.js Tue Feb 16 11:47:52 2021 +0100
@@ -212,37 +212,48 @@
}
function switch_langnum(langnum) {
- if(langnum == current_lang) {
- return;
- }
+ langnum = Math.max(0, Math.min(langs.length - 1, langnum));
for (let translation of translations) {
- let [objs, msgs, orig] = translation;
- let msg = langnum == 0 ? orig : msgs[langnum - 1];
+ let [objs, msgs] = translation;
+ let msg = msgs[langnum];
for (let obj of objs) {
multiline_to_svg_text(obj, msg);
obj.setAttribute("lang",langnum);
}
}
- current_lang = langnum;
+ return langnum;
}
// backup original texts
for (let translation of translations) {
- let [objs] = translation;
- translation.push(svg_text_to_multiline(objs[0]));
+ let [objs, msgs] = translation;
+ msgs.unshift(svg_text_to_multiline(objs[0]));
}
var lang_local_index = hmi_local_index("lang");
+var langcode_local_index = hmi_local_index("lang_code");
+var langname_local_index = hmi_local_index("lang_name");
subscribers(lang_local_index).add({
indexes: [lang_local_index],
new_hmi_value: function(index, value, oldval) {
- switch_langnum(value);
+ let current_lang = switch_langnum(value);
+ let [langname,langcode] = langs[current_lang];
+ apply_hmi_value(langcode_local_index, langcode);
+ apply_hmi_value(langname_local_index, langname);
switch_page();
}
});
-var current_lang = 0;
-switch_langnum(cache[lang_local_index]);
+
+function setup_lang(){
+ let current_lang = cache[lang_local_index];
+ let new_lang = switch_langnum(current_lang);
+ if(current_lang != new_lang){
+ apply_hmi_value(lang_local_index, new_lang);
+ }
+}
+
+setup_lang();
function update_subscriptions() {
let delta = [];