mirror of
https://github.com/inventree/InvenTree.git
synced 2025-05-07 15:58:49 +00:00
Merge pull request #2358 from matmair/matmair/issue2353
[FR] testing language
This commit is contained in:
commit
97d4107dcc
@ -26,6 +26,7 @@ import moneyed
|
|||||||
import yaml
|
import yaml
|
||||||
from django.utils.translation import gettext_lazy as _
|
from django.utils.translation import gettext_lazy as _
|
||||||
from django.contrib.messages import constants as messages
|
from django.contrib.messages import constants as messages
|
||||||
|
import django.conf.locale
|
||||||
|
|
||||||
|
|
||||||
def _is_true(x):
|
def _is_true(x):
|
||||||
@ -682,6 +683,25 @@ LANGUAGES = [
|
|||||||
('zh-cn', _('Chinese')),
|
('zh-cn', _('Chinese')),
|
||||||
]
|
]
|
||||||
|
|
||||||
|
# Testing interface translations
|
||||||
|
if get_setting('TEST_TRANSLATIONS', False):
|
||||||
|
# Set default language
|
||||||
|
LANGUAGE_CODE = 'xx'
|
||||||
|
|
||||||
|
# Add to language catalog
|
||||||
|
LANGUAGES.append(('xx', 'Test'))
|
||||||
|
|
||||||
|
# Add custom languages not provided by Django
|
||||||
|
EXTRA_LANG_INFO = {
|
||||||
|
'xx': {
|
||||||
|
'code': 'xx',
|
||||||
|
'name': 'Test',
|
||||||
|
'name_local': 'Test'
|
||||||
|
},
|
||||||
|
}
|
||||||
|
LANG_INFO = dict(django.conf.locale.LANG_INFO, **EXTRA_LANG_INFO)
|
||||||
|
django.conf.locale.LANG_INFO = LANG_INFO
|
||||||
|
|
||||||
# Currencies available for use
|
# Currencies available for use
|
||||||
CURRENCIES = CONFIG.get(
|
CURRENCIES = CONFIG.get(
|
||||||
'currencies',
|
'currencies',
|
||||||
|
71
tasks.py
71
tasks.py
@ -3,6 +3,8 @@
|
|||||||
import os
|
import os
|
||||||
import json
|
import json
|
||||||
import sys
|
import sys
|
||||||
|
import pathlib
|
||||||
|
import re
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from invoke import ctask as task
|
from invoke import ctask as task
|
||||||
@ -469,6 +471,75 @@ def server(c, address="127.0.0.1:8000"):
|
|||||||
manage(c, "runserver {address}".format(address=address), pty=True)
|
manage(c, "runserver {address}".format(address=address), pty=True)
|
||||||
|
|
||||||
|
|
||||||
|
@task(post=[translate_stats, static, server])
|
||||||
|
def test_translations(c):
|
||||||
|
"""
|
||||||
|
Add a fictional language to test if each component is ready for translations
|
||||||
|
"""
|
||||||
|
import django
|
||||||
|
from django.conf import settings
|
||||||
|
|
||||||
|
# setup django
|
||||||
|
base_path = os.getcwd()
|
||||||
|
new_base_path = pathlib.Path('InvenTree').absolute()
|
||||||
|
sys.path.append(str(new_base_path))
|
||||||
|
os.chdir(new_base_path)
|
||||||
|
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'InvenTree.settings')
|
||||||
|
django.setup()
|
||||||
|
|
||||||
|
# Add language
|
||||||
|
print("Add dummy language...")
|
||||||
|
print("========================================")
|
||||||
|
manage(c, "makemessages -e py,html,js --no-wrap -l xx")
|
||||||
|
|
||||||
|
# change translation
|
||||||
|
print("Fill in dummy translations...")
|
||||||
|
print("========================================")
|
||||||
|
|
||||||
|
file_path = pathlib.Path(settings.LOCALE_PATHS[0], 'xx', 'LC_MESSAGES', 'django.po')
|
||||||
|
new_file_path = str(file_path) + '_new'
|
||||||
|
|
||||||
|
# complie regex
|
||||||
|
reg = re.compile(
|
||||||
|
r"[a-zA-Z0-9]{1}"+ # match any single letter and number
|
||||||
|
r"(?![^{\(\<]*[}\)\>])"+ # that is not inside curly brackets, brackets or a tag
|
||||||
|
r"(?<![^\%][^\(][)][a-z])"+ # that is not a specially formatted variable with singles
|
||||||
|
r"(?![^\\][\n])" # that is not a newline
|
||||||
|
)
|
||||||
|
last_string = ''
|
||||||
|
|
||||||
|
# loop through input file lines
|
||||||
|
with open(file_path, "rt") as file_org:
|
||||||
|
with open(new_file_path, "wt") as file_new:
|
||||||
|
for line in file_org:
|
||||||
|
if line.startswith('msgstr "'):
|
||||||
|
# write output -> replace regex matches with x in the read in (multi)string
|
||||||
|
file_new.write(f'msgstr "{reg.sub("x", last_string[7:-2])}"\n')
|
||||||
|
last_string = "" # reset (multi)string
|
||||||
|
elif line.startswith('msgid "'):
|
||||||
|
last_string = last_string + line # a new translatable string starts -> start append
|
||||||
|
file_new.write(line)
|
||||||
|
else:
|
||||||
|
if last_string:
|
||||||
|
last_string = last_string + line # a string is beeing read in -> continue appending
|
||||||
|
file_new.write(line)
|
||||||
|
|
||||||
|
# change out translation files
|
||||||
|
os.rename(file_path, str(file_path) + '_old')
|
||||||
|
os.rename(new_file_path, file_path)
|
||||||
|
|
||||||
|
# compile languages
|
||||||
|
print("Compile languages ...")
|
||||||
|
print("========================================")
|
||||||
|
manage(c, "compilemessages")
|
||||||
|
|
||||||
|
# reset cwd
|
||||||
|
os.chdir(base_path)
|
||||||
|
|
||||||
|
# set env flag
|
||||||
|
os.environ['TEST_TRANSLATIONS'] = 'True'
|
||||||
|
|
||||||
|
|
||||||
@task
|
@task
|
||||||
def render_js_files(c):
|
def render_js_files(c):
|
||||||
"""
|
"""
|
||||||
|
Loading…
x
Reference in New Issue
Block a user