-
Notifications
You must be signed in to change notification settings - Fork 13.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[WIP] Add translation string support #10995
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
9fe4069
Add tranlsation string support
hamishwillee bffa106
Add debug
hamishwillee 00326a9
Move directory for translations to be relative to source tree
hamishwillee a581b72
Rename the file to emit strings for translation
hamishwillee 195c469
Improve translation keys and feedback fixes
hamishwillee File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import xml.etree.ElementTree as ET | ||
#import os | ||
from os import listdir | ||
from os.path import isfile, join, dirname, realpath, exists | ||
|
||
|
||
def GetTranslations(filename=''): | ||
lang_translations = dict() | ||
# Get path to translation path | ||
|
||
dir_path = dirname(realpath(__file__)) | ||
translation_path = dir_path.rsplit('/',1)[0]+'/translations/' | ||
if not exists(translation_path): | ||
#print("DEBUG: Translation direcory not found: %s" % translation_path) | ||
return None | ||
translation_files = [f for f in listdir(translation_path) if isfile(join(translation_path, f))] | ||
|
||
# Get english dict | ||
for filename in translation_files: | ||
if filename=='parameter_strings.xml': | ||
english_root = ET.parse(translation_path+filename) | ||
lang_dict = dict() | ||
for entry in english_root.findall("./entry"): | ||
lang_dict[entry.attrib['key']]=entry.text | ||
#print("DEBUG: %s " % entry.attrib['key']) | ||
#print("DEBUG: %s " % entry.text) | ||
lang_translations['en'] = lang_dict | ||
break | ||
|
||
# Return if no English version found (need for keys) | ||
if len(lang_translations)==0: | ||
return None | ||
|
||
for filename in translation_files: | ||
#print("DEBUG: %s" % filename) | ||
if filename=='parameter_strings.xml': | ||
continue | ||
else: | ||
root = ET.parse(translation_path+filename) | ||
lang_dict = dict() | ||
lang_key=filename.rsplit('-',1)[-1].rsplit('.',1)[0] | ||
#print('DEBUG:lang_key: %s' % lang_key) | ||
for entry in root.findall("./entry"): | ||
key = entry.attrib['key'] | ||
entry_text = entry.text | ||
# Only add string if it has changed from English | ||
if key in lang_translations['en'] and not lang_translations['en'][key] == entry_text: | ||
lang_dict[key]=entry_text | ||
lang_translations[lang_key] = lang_dict | ||
return lang_translations |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
# | ||
# Export all parameter strings for translation into key/value file. | ||
# | ||
|
||
import xml.etree.ElementTree as ET | ||
import codecs | ||
import os | ||
|
||
def indent(elem, level=0): | ||
i = "\n" + level*" " | ||
if len(elem): | ||
if not elem.text or not elem.text.strip(): | ||
elem.text = i + " " | ||
if not elem.tail or not elem.tail.strip(): | ||
elem.tail = i | ||
for elem in elem: | ||
indent(elem, level+1) | ||
if not elem.tail or not elem.tail.strip(): | ||
elem.tail = i | ||
else: | ||
if level and (not elem.tail or not elem.tail.strip()): | ||
elem.tail = i | ||
|
||
class XMLOutput(): | ||
|
||
def __init__(self, groups, board, inject_xml_file_name): | ||
unique_strings = set() | ||
xml_parameters = ET.Element("properties") | ||
|
||
for group in groups: | ||
group_name=group.GetName() | ||
if group_name not in unique_strings: | ||
xml_entry = ET.SubElement(xml_parameters, "entry") | ||
xml_entry.attrib["key"] = 'group_%s' % group_name | ||
#xml_entry.attrib["type"] = 'group' | ||
xml_entry.text = group_name | ||
unique_strings.add(group_name) | ||
else: | ||
pass | ||
|
||
for param in group.GetParams(): | ||
param_name = param.GetName() | ||
param_default = param.GetDefault() | ||
param_type = param.GetType() | ||
param_volatile = param.GetVolatile() | ||
param_category = param.GetCategory() | ||
#print("DEBUG: param_name: %s" % param_name) | ||
#print("DEBUG: param_default: %s" % param_default) | ||
#print("DEBUG: param type: %s" % param_type) | ||
#print("DEBUG: param_volatile: %s" % param_volatile) | ||
#print("DEBUG: param_category: %s" % param_category) | ||
|
||
#Add param_category (if exists) | ||
if param_category not in unique_strings and param_category: | ||
xml_entry = ET.SubElement(xml_parameters, "entry") | ||
xml_entry.attrib["key"] = 'category_%s' % param_category | ||
#xml_entry.attrib["type"] = 'param_category' | ||
#xml_entry.attrib["param"] = 'param_name' | ||
xml_entry.text = param_category | ||
unique_strings.add(param_category) | ||
else: | ||
pass | ||
|
||
for code in param.GetFieldCodes(): | ||
value = param.GetFieldValue(code) | ||
#print('DEBUG: code: %s, value: %s' % (code, value)) | ||
if code in ['short_desc','long_desc']: #Other fields are not translateable | ||
if value not in unique_strings and value: | ||
xml_entry = ET.SubElement(xml_parameters, "entry") | ||
xml_entry.attrib["key"] = 'param_%s_%s' % (param_name,code) | ||
#xml_entry.attrib["type"] = code | ||
#xml_entry.attrib["param"] = param_name | ||
xml_entry.text = value | ||
unique_strings.add(value) | ||
else: | ||
pass | ||
|
||
|
||
|
||
if len(param.GetEnumCodes()) > 0: | ||
for code in param.GetEnumCodes(): | ||
value = param.GetEnumValue(code) | ||
#print('DEBUG: code: %s, value: %s' % (code, value)) | ||
if value not in unique_strings and value: | ||
xml_entry = ET.SubElement(xml_parameters, "entry") | ||
xml_entry.attrib["key"] = 'enum_%s_val_%s' % (param_name,code) | ||
#xml_entry.attrib["type"] = 'enumvalue' | ||
#xml_entry.attrib["value"] = code | ||
#xml_entry.attrib["param"] = param_name | ||
xml_entry.text = value | ||
unique_strings.add(value) | ||
else: | ||
pass | ||
|
||
if len(param.GetBitmaskList()) > 0: | ||
for index in param.GetBitmaskList(): | ||
value = param.GetBitmaskBit(index) | ||
#print('DEBUG: index: %s, value: %s' % (index, value)) | ||
if value not in unique_strings and value: | ||
xml_entry = ET.SubElement(xml_parameters, "entry") | ||
xml_entry.attrib["key"] = 'bitmask_%s_index_%s' % (param_name,index) | ||
#xml_entry.attrib["type"] = 'bit' | ||
#xml_entry.attrib["value"] = index | ||
#xml_entry.attrib["param"] = param_name | ||
xml_entry.text = value | ||
unique_strings.add(value) | ||
else: | ||
pass | ||
|
||
|
||
|
||
indent(xml_parameters) | ||
self.xml_document = ET.ElementTree(xml_parameters) | ||
|
||
def Save(self, filename): | ||
# Create target directory & all intermediate directories if don't exists | ||
dirname=filename.rsplit('/',1)[0] | ||
if not os.path.exists(dirname): | ||
os.makedirs(dirname) | ||
|
||
self.xml_document.write(filename, encoding="UTF-8") | ||
# Add doctype (Clunky, but not possible to avoid in ElementTree) | ||
f=open(filename, "r") | ||
filetext=f.read() | ||
f.close | ||
filetext=filetext.replace('<properties>','<!DOCTYPE properties SYSTEM "[http://java.sun.com/dtd/properties.dtd](http://java.sun.com/dtd/properties.dtd)">\n<properties>') | ||
with open(filename, "w") as f: | ||
f.write(filetext) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, looks like it is
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed :-)
This is waiting on @dagar - he will review and merge (if appropriate) when it gets near the top of his priority list.