Translation Fixes (#4433)

* Change 'zh-cn' to 'zh-hans'

* Enforce consistent naming scheme when rendering translated javascript files

* More intelligent lookup of translated javascript files

- Ensure that the language code is lower-case (to match translated files)
- Provide fallback if specified langauge code is not found

* Replace underscore characters

* remove debug hook
This commit is contained in:
Oliver 2023-03-02 08:24:31 +11:00 committed by GitHub
parent fdfc3e5e7e
commit ec66e5351b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 35 additions and 5 deletions

View File

@ -12,8 +12,15 @@ from django.utils.translation import override as lang_over
def render_file(file_name, source, target, locales, ctx):
"""Renders a file into all provided locales."""
for locale in locales:
# Enforce lower-case for locale names
locale = locale.lower()
locale = locale.replace('_', '-')
target_file = os.path.join(target, locale + '.' + file_name)
with open(target_file, 'w') as localised_file:
with lang_over(locale):
renderd = render_to_string(os.path.join(source, file_name), ctx)

View File

@ -718,7 +718,7 @@ LANGUAGES = [
('th', _('Thai')),
('tr', _('Turkish')),
('vi', _('Vietnamese')),
('zh-cn', _('Chinese')),
('zh-hans', _('Chinese')),
]
# Testing interface translations

View File

@ -2,7 +2,7 @@ msgid ""
msgstr ""
"Project-Id-Version: inventree\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2023-02-28 21:17+0000\n"
"POT-Creation-Date: 2023-03-01 10:01+0000\n"
"PO-Revision-Date: 2023-02-28 22:38\n"
"Last-Translator: \n"
"Language-Team: Chinese Simplified\n"
@ -11550,7 +11550,8 @@ msgstr ""
#: templates/socialaccount/signup.html:10
#, python-format
msgid "You are about to use your %(provider_name)s account to login to\n"
msgid ""
"You are about to use your %(provider_name)s account to login to\n"
"%(site_name)s.<br>As a final step, please complete the following form:"
msgstr ""
@ -11737,4 +11738,3 @@ msgstr "编辑项目权限"
#: users/models.py:231
msgid "Permission to delete items"
msgstr "删除项目权限"

View File

@ -570,7 +570,30 @@ class I18nStaticNode(StaticNode):
self.original = self.path.var
if hasattr(context, 'request'):
self.path.var = self.original.format(lng=context.request.LANGUAGE_CODE)
# Convert the "requested" language code to a standard format
language_code = context.request.LANGUAGE_CODE.lower().strip()
language_code = language_code.replace('_', '-')
# Find the first "best" match:
# - First, try the original requested code, e.g. 'pt-br'
# - Next, try a simpler version of the code e.g. 'pt'
# - Finally, fall back to english
options = [
language_code,
language_code.split('-')[0],
'en',
]
for lng in options:
lng_file = os.path.join(
djangosettings.STATIC_ROOT,
self.original.format(lng=lng)
)
if os.path.exists(lng_file):
self.path.var = self.original.format(lng=lng)
break
ret = super().render(context)