我如何在 Django 中翻译来自 JSon 个文件的静态数据?
How could I translate static data coming from JSon files in Django?
我有一个包含一长串地理位置的 json 文件
[
{
"id": 1,
"name": "Afghanistan",
"iso3": "AFG",
"iso2": "AF",
"phone_code": "93",
"capital": "Kabul",
"currency": "AFN",
"currency_symbol": "؋",
"tld": ".af",
"native": "افغانستان",
"region": "Asia",
"subregion": "Southern Asia",
"timezones": [
{
"zoneName": "Asia\/Kabul",
"gmtOffset": 16200,
"gmtOffsetName": "UTC+04:30",
"abbreviation": "AFT",
"tzName": "Afghanistan Time"
}
],
"latitude": "33.00000000",
"longitude": "65.00000000",
"emoji": "",
"emojiU": "U+1F1E6 U+1F1EB",
"states": [
{
"id": 3901,
"name": "Badakhshan",
"state_code": "BDS",
"cities": [
{
"id": 52,
"name": "Ashkāsham",
"latitude": "36.68333000",
"longitude": "71.53333000"
},
.......
/* very long list */
当需要 country/state/city 个下拉列表时,此文件以 Django 形式加载。
问题是我想至少将国家名称翻译成其他语言。
即使我被允许在我的 JSon 文件中使用 {% trans "Country_name" %} 也不会很实用。有更快的方法吗?例如,在 forms.py 中做这个是行不通的:
from django.utils.translation import gettext_lazy as _
# ...
def get_country():
filepath = 'myproj/static/data/countries_states_cities.json'
all_data = readJson(filepath)
all_countries = [('----', _("--- Select a Country ---"))]
for x in all_data:
y = (x['name'], _(x['name']))
all_countries.append(y)
return all_countries
"--- Select a Country ---" 将被翻译但 x['name'] 不会,因为看起来
django-admin makemessages -l fr
不执行循环也不查找数组。
简而言之,如果我必须翻译来自数据库的数据,我会遇到同样的问题。
对于数据库django_modeltranslation 将是一个解决方案。但是我如何翻译来自 json 文件的数据而不必手动编辑 json 文件,这非常繁琐且容易出错?
您可以customize the makemessages
command预处理.json
个文件。
# mysite/myapp/management/commands/makemessages.py
import os
import subprocess
from django.core.management.commands import makemessages
def templatize(path):
return subprocess.check_output(["sed", "-E", f's/"name": "(.*)"/"name": _("\1")/g', path]).decode()
class BuildFile(makemessages.BuildFile):
def preprocess(self):
if not self.is_templatized:
return
file_ext = os.path.splitext(self.translatable.file)[1]
if file_ext == '.json':
content = templatize(self.path)
with open(self.work_path, 'w', encoding='utf-8') as fp:
fp.write(content)
return
super().preprocess()
class Command(makemessages.Command):
build_file_class = BuildFile
用法:
python manage.py makemessages -l fr -e html,txt,py,json
我有一个包含一长串地理位置的 json 文件
[
{
"id": 1,
"name": "Afghanistan",
"iso3": "AFG",
"iso2": "AF",
"phone_code": "93",
"capital": "Kabul",
"currency": "AFN",
"currency_symbol": "؋",
"tld": ".af",
"native": "افغانستان",
"region": "Asia",
"subregion": "Southern Asia",
"timezones": [
{
"zoneName": "Asia\/Kabul",
"gmtOffset": 16200,
"gmtOffsetName": "UTC+04:30",
"abbreviation": "AFT",
"tzName": "Afghanistan Time"
}
],
"latitude": "33.00000000",
"longitude": "65.00000000",
"emoji": "",
"emojiU": "U+1F1E6 U+1F1EB",
"states": [
{
"id": 3901,
"name": "Badakhshan",
"state_code": "BDS",
"cities": [
{
"id": 52,
"name": "Ashkāsham",
"latitude": "36.68333000",
"longitude": "71.53333000"
},
.......
/* very long list */
当需要 country/state/city 个下拉列表时,此文件以 Django 形式加载。
问题是我想至少将国家名称翻译成其他语言。
即使我被允许在我的 JSon 文件中使用 {% trans "Country_name" %} 也不会很实用。有更快的方法吗?例如,在 forms.py 中做这个是行不通的:
from django.utils.translation import gettext_lazy as _
# ...
def get_country():
filepath = 'myproj/static/data/countries_states_cities.json'
all_data = readJson(filepath)
all_countries = [('----', _("--- Select a Country ---"))]
for x in all_data:
y = (x['name'], _(x['name']))
all_countries.append(y)
return all_countries
"--- Select a Country ---" 将被翻译但 x['name'] 不会,因为看起来
django-admin makemessages -l fr
不执行循环也不查找数组。
简而言之,如果我必须翻译来自数据库的数据,我会遇到同样的问题。
对于数据库django_modeltranslation 将是一个解决方案。但是我如何翻译来自 json 文件的数据而不必手动编辑 json 文件,这非常繁琐且容易出错?
您可以customize the makemessages
command预处理.json
个文件。
# mysite/myapp/management/commands/makemessages.py
import os
import subprocess
from django.core.management.commands import makemessages
def templatize(path):
return subprocess.check_output(["sed", "-E", f's/"name": "(.*)"/"name": _("\1")/g', path]).decode()
class BuildFile(makemessages.BuildFile):
def preprocess(self):
if not self.is_templatized:
return
file_ext = os.path.splitext(self.translatable.file)[1]
if file_ext == '.json':
content = templatize(self.path)
with open(self.work_path, 'w', encoding='utf-8') as fp:
fp.write(content)
return
super().preprocess()
class Command(makemessages.Command):
build_file_class = BuildFile
用法:
python manage.py makemessages -l fr -e html,txt,py,json