Spaces:
Runtime error
Runtime error
File size: 958 Bytes
49226f5 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
import os
import json
class i18nTranslator:
def __init__(self):
self.translations = {}
def init(self, path):
for filename in os.listdir(path):
if filename.endswith('.json'):
language = filename.split('.')[0]
with open(os.path.join(path, filename), 'r') as file:
self.translations[language] = json.load(file)
def trans(self, key, lng):
if lng in self.translations and key in self.translations[lng]:
return self.translations[lng][key]
elif 'en' in self.translations and key in self.translations['en']:
return self.translations['en'][key]
else:
return key
# Usage example
if __name__ == '__main__':
translator = i18nTranslator()
translator.init(path='locales')
print(translator.trans(key="Enter your prompt", lng="zh-CN")) # Output: "输入你的提示词(你希望生成的图片的描述)"
|