lang
This commit is contained in:
@@ -3,6 +3,7 @@ from args import inputAssets, args
|
||||
from processasset import processAsset
|
||||
from processpalette import processPaletteList
|
||||
from processtileset import processTilesetList
|
||||
from processlanguage import processLanguageList
|
||||
from assethelpers import getBuiltAssetsRelativePath
|
||||
import zipfile
|
||||
|
||||
@@ -31,6 +32,7 @@ with zipfile.ZipFile(outputFileName, 'w') as zipf:
|
||||
# Generate additional headers.
|
||||
processPaletteList()
|
||||
processTilesetList()
|
||||
processLanguageList()
|
||||
|
||||
# Finalize build
|
||||
if args.build_type == 'header':
|
||||
|
||||
@@ -4,6 +4,7 @@ from processimage import processImage
|
||||
from processpalette import processPalette
|
||||
from processtileset import processTileset
|
||||
from processmap import processMap
|
||||
from processlanguage import processLanguage
|
||||
|
||||
processedAssets = []
|
||||
|
||||
@@ -22,6 +23,8 @@ def processAsset(asset):
|
||||
return processTileset(asset)
|
||||
elif t == 'map':
|
||||
return processMap(asset)
|
||||
elif t == 'language':
|
||||
return processLanguage(asset)
|
||||
else:
|
||||
print(f"Error: Unknown asset type '{asset['type']}' for path '{asset['path']}'")
|
||||
sys.exit(1)
|
||||
73
tools/assetstool/processlanguage.py
Normal file
73
tools/assetstool/processlanguage.py
Normal file
@@ -0,0 +1,73 @@
|
||||
import sys
|
||||
import os
|
||||
from args import args
|
||||
from assetcache import assetCache, assetGetCache
|
||||
from assethelpers import getAssetRelativePath
|
||||
import polib
|
||||
import re
|
||||
|
||||
LANGUAGE_DATA = {}
|
||||
LANGUAGE_KEYS = []
|
||||
|
||||
def processLanguageList():
|
||||
# Language keys header data
|
||||
headerKeys = "// Auto-generated language keys header file.\n"
|
||||
headerKeys += "#pragma once\n"
|
||||
headerKeys += "#include \"dusk.h\"\n\n"
|
||||
|
||||
keyIndex = 0
|
||||
for key in LANGUAGE_KEYS:
|
||||
headerKeys += f"#define {getLanguageVariableName(key)} {keyIndex}\n"
|
||||
keyIndex += 1
|
||||
|
||||
for lang in LANGUAGE_DATA:
|
||||
if key not in LANGUAGE_DATA[lang]:
|
||||
print(f"Warning: Missing translation for key '{key}' in language '{lang}'")
|
||||
sys.exit(1)
|
||||
|
||||
headerKeys += f"\n#define LANG_KEY_COUNT {len(LANGUAGE_KEYS)}\n"
|
||||
|
||||
# Write out the language keys header file
|
||||
outputFile = os.path.join(args.headers_dir, "locale", "language", "keys.h")
|
||||
os.makedirs(os.path.dirname(outputFile), exist_ok=True)
|
||||
with open(outputFile, "w") as f:
|
||||
f.write(headerKeys)
|
||||
|
||||
def getLanguageVariableName(languageKey):
|
||||
# Take the language key, prepend LANG_, uppercase, replace any non symbols
|
||||
# with _
|
||||
key = languageKey.strip().upper()
|
||||
key = re.sub(r'[^A-Z0-9]', '_', key)
|
||||
return f"LANG_{key}"
|
||||
|
||||
def processLanguage(asset):
|
||||
cache = assetGetCache(asset['path'])
|
||||
if cache is not None:
|
||||
return cache
|
||||
|
||||
# Load PO File
|
||||
po = polib.pofile(asset['path'])
|
||||
|
||||
langName = po.metadata.get('Language')
|
||||
if langName not in LANGUAGE_DATA:
|
||||
LANGUAGE_DATA[langName] = {}
|
||||
|
||||
for entry in po:
|
||||
key = entry.msgid
|
||||
val = entry.msgstr
|
||||
|
||||
if key not in LANGUAGE_KEYS:
|
||||
LANGUAGE_KEYS.append(key)
|
||||
|
||||
if key not in LANGUAGE_DATA[langName]:
|
||||
LANGUAGE_DATA[langName][key] = val
|
||||
else:
|
||||
print(f"Error: Duplicate translation key '{key}' in language '{langName}'")
|
||||
sys.exit(1)
|
||||
|
||||
outLanguageData = {
|
||||
'data': po,
|
||||
'path': asset['path'],
|
||||
'files': []
|
||||
}
|
||||
return assetCache(asset['path'], outLanguageData)
|
||||
Reference in New Issue
Block a user