"""Freeze the upstream dictionaries as data; no JS executes in either desktop app.
Native additions intentionally fall back to English when a locale has no entry.
Run --rewrite-xaml once when adding literal XAML text; normal runs only build data.
"""
import argparse
import hashlib
import html
import json
import re
from pathlib import Path
root = Path(__file__).resolve().parent.parent
args = argparse.ArgumentParser()
args.add_argument('--rewrite-xaml', action='store_true')
options = args.parse_args()
folder = root / 'Client/SharedUI/Localization'
reference = root / 'artifacts/openwf-frontend-audit-20260926/SpaceNinjaServer/static/webui/translations'
pattern = re.compile(r'(\w+)\s*:\s*`((?:\\.|[^`])*)`', re.S)
def plain(text):
return html.unescape(re.sub(r'<[^>]+>', '', text)).replace('\\`', '`').replace('\\n', '\n').strip()
catalog = {p.stem: {key: plain(value) for key, value in pattern.findall(p.read_text(encoding='utf-8-sig'))} for p in reference.glob('*.js')}
assert set(catalog) == {'en','de','es','fr','pl','ru','uk','zh'}
assert len(catalog['en']) > 300
sources = {}
for line in (folder / 'native-en.tsv').read_text(encoding='utf-8').splitlines():
if line and not line.startswith('#'):
source, english = line.split('\t', 1)
source, english = source.replace('\\n', '\n'), english.replace('\\n', '\n')
assert source not in sources, source
sources[source] = english
# Chinese and English literals, schema labels and original keys all resolve through
# the same dictionary. Preserve untouched key/value protocol data in API responses.
for key, chinese in list(catalog['zh'].items()):
for language, data in catalog.items():
if key in data:
data.setdefault(chinese, data[key])
if key in catalog['en']: data.setdefault(catalog['en'][key], data[key])
for source, english in sources.items():
catalog['en'][source] = english
catalog['zh'][source] = source
for line in (folder / 'native-languages.tsv').read_text(encoding='utf-8').splitlines():
if not line or line.startswith('#'): continue
row = line.split('\t'); assert len(row) == 7, row
for lang, text in zip(['de','es','fr','pl','ru','uk'], row[1:]): catalog[lang][row[0]] = text
for source in sources:
key = 'ui_' + hashlib.sha256(source.encode()).hexdigest()[:16]
for data in catalog.values():
if source in data: data[key] = data[source]
for base in ['Client/XFEWarframe.Player','Admin/XFEWarframe.Admin']:
for path in (root / base).rglob('*.xaml'):
if 'obj' in path.parts or 'bin' in path.parts: continue
text = path.read_text(encoding='utf-8-sig')
if not options.rewrite_xaml: continue
used = False
def replace(match):
global used
source = html.unescape(match[2])
if source.startswith('{') or not re.search('[\u4e00-\u9fff]',source): return match[0]
assert source in sources, (path,source)
used = True
key = 'ui_' + hashlib.sha256(source.encode()).hexdigest()[:16]
return f'{match[1]}="{{loc:Translate Key={key}}}"'
text = re.sub(r'(Text|Header|Content|PlaceholderText|Subtitle|OnContent|OffContent|Title|PrimaryButtonText|SecondaryButtonText|CloseButtonText)="([^"\n]*)"', replace, text)
if used and 'xmlns:loc=' not in text:
text = re.sub(r'(<\w+(?:\.\w+)?\b)', r'\1 xmlns:loc="using:XFEWarframe.Desktop.Editing"', text, count=1)
if used: path.write_text(text, encoding='utf-8')
(folder / 'translations.json').write_text(json.dumps(catalog,ensure_ascii=False,indent=2)+'\n',encoding='utf-8')
print(json.dumps({'nativeKeys':len(sources),'languages':{key:len(value) for key,value in catalog.items()}},ensure_ascii=False))
"""Freeze the upstream dictionaries as data; no JS executes in either desktop app.
Native additions intentionally fall back to English when a locale has no entry.
Run --rewrite-xaml once when adding literal XAML text; normal runs only build data.
"""
import argparse
import hashlib
import html
import json
import re
from pathlib import Path
root = Path(__file__).resolve().parent.parent
args = argparse.ArgumentParser()
args.add_argument('--rewrite-xaml', action='store_true')
options = args.parse_args()
folder = root / 'Client/SharedUI/Localization'
reference = root / 'artifacts/openwf-frontend-audit-20260926/SpaceNinjaServer/static/webui/translations'
pattern = re.compile(r'(\w+)\s*:\s*`((?:\\.|[^`])*)`', re.S)
def plain(text):
return html.unescape(re.sub(r'<[^>]+>', '', text)).replace('\\`', '`').replace('\\n', '\n').strip()
catalog = {p.stem: {key: plain(value) for key, value in pattern.findall(p.read_text(encoding='utf-8-sig'))} for p in reference.glob('*.js')}
assert set(catalog) == {'en','de','es','fr','pl','ru','uk','zh'}
assert len(catalog['en']) > 300
sources = {}
for line in (folder / 'native-en.tsv').read_text(encoding='utf-8').splitlines():
if line and not line.startswith('#'):
source, english = line.split('\t', 1)
source, english = source.replace('\\n', '\n'), english.replace('\\n', '\n')
assert source not in sources, source
sources[source] = english
# Chinese and English literals, schema labels and original keys all resolve through
# the same dictionary. Preserve untouched key/value protocol data in API responses.
for key, chinese in list(catalog['zh'].items()):
for language, data in catalog.items():
if key in data:
data.setdefault(chinese, data[key])
if key in catalog['en']: data.setdefault(catalog['en'][key], data[key])
for source, english in sources.items():
catalog['en'][source] = english
catalog['zh'][source] = source
for line in (folder / 'native-languages.tsv').read_text(encoding='utf-8').splitlines():
if not line or line.startswith('#'): continue
row = line.split('\t'); assert len(row) == 7, row
for lang, text in zip(['de','es','fr','pl','ru','uk'], row[1:]): catalog[lang][row[0]] = text
for source in sources:
key = 'ui_' + hashlib.sha256(source.encode()).hexdigest()[:16]
for data in catalog.values():
if source in data: data[key] = data[source]
for base in ['Client/XFEWarframe.Player','Admin/XFEWarframe.Admin']:
for path in (root / base).rglob('*.xaml'):
if 'obj' in path.parts or 'bin' in path.parts: continue
text = path.read_text(encoding='utf-8-sig')
if not options.rewrite_xaml: continue
used = False
def replace(match):
global used
source = html.unescape(match[2])
if source.startswith('{') or not re.search('[\u4e00-\u9fff]',source): return match[0]
assert source in sources, (path,source)
used = True
key = 'ui_' + hashlib.sha256(source.encode()).hexdigest()[:16]
return f'{match[1]}="{{loc:Translate Key={key}}}"'
text = re.sub(r'(Text|Header|Content|PlaceholderText|Subtitle|OnContent|OffContent|Title|PrimaryButtonText|SecondaryButtonText|CloseButtonText)="([^"\n]*)"', replace, text)
if used and 'xmlns:loc=' not in text:
text = re.sub(r'(<\w+(?:\.\w+)?\b)', r'\1 xmlns:loc="using:XFEWarframe.Desktop.Editing"', text, count=1)
if used: path.write_text(text, encoding='utf-8')
(folder / 'translations.json').write_text(json.dumps(catalog,ensure_ascii=False,indent=2)+'\n',encoding='utf-8')
print(json.dumps({'nativeKeys':len(sources),'languages':{key:len(value) for key,value in catalog.items()}},ensure_ascii=False))