Skip to content

Commit

Permalink
extends localized string export options to group by language
Browse files Browse the repository at this point in the history
- also adds default templates: `robots.txt`, `data.json`
- adds `json_file_download` view for easily rendering a downloadable JSON file response
- allows `jsondumps` template filter to take in an arg for `indent`
  • Loading branch information
jontsai committed Apr 8, 2024
1 parent 9c99e29 commit 4861521
Show file tree
Hide file tree
Showing 6 changed files with 237 additions and 74 deletions.
8 changes: 5 additions & 3 deletions apps/i18n/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,9 @@ class Meta:
)

def __str__(self):
value = '{} - {}'.format(
self.localizable_string.key, self.language_code
)
value = '{} - {}'.format(self.key, self.language_code)
return value

@property
def key(self):
return self.localizable_string.key
103 changes: 92 additions & 11 deletions apps/i18n/utils/data.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Python Standard Library Imports
import json
import os
from functools import lru_cache

# HTK Imports
from htk.utils import (
Expand All @@ -9,23 +10,103 @@
)


def retrieve_all_strings():
LocalizableString = resolve_model_dynamically(
htk_setting('HTK_LOCALIZABLE_STRING_MODEL')
@lru_cache
def look_up_supported_languages():
"""Looks up which have languages have translations.
Returns a list of language codes.
"""
LocalizedString = resolve_model_dynamically(
htk_setting('HTK_LOCALIZED_STRING_MODEL')
)
language_codes = (
LocalizedString.objects.values_list('language_code', flat=True)
.order_by('language_code')
.distinct()
)
localizable_strings = LocalizableString.objects.order_by('key')
data = {
localizable_string.key: localizable_string.json_encode(
include_key=False
)['translations']
for localizable_string in localizable_strings
return language_codes


def retrieve_all_strings(by_language=False, language_codes=None):
"""Returns all translated strings for every possible
`LocalizableString` in every available language (`LocalizedString`)
When `by_language` is `False` (default):
- All `LocalizableString`s will be included, including those which
have no translations available.
- The strings will be ordered by `key`s
The use case for including untranslated strings is to be able to send
a batch of strings to translation vendors in bulk.
Example structure:
{
'bad': {
'en-US': 'Bad',
'zh-CN': '壞',
},
'good': {
'en-US': 'Good',
'zh-CN': '好',
},
}
When `by_language` is `True`:
- The strings will be ordered by `language_code`
- Additionally, if `language_codes` is supplied, only the included languages
will be output
Example structure:
{
'en-US': {
'bad': 'Bad',
'good': 'Good',
},
'zh-CN': {
'bad': '壞',
'good': '好',
},
}
"""
if by_language:
if language_codes is None:
language_codes = look_up_supported_languages()

LocalizedString = resolve_model_dynamically(
htk_setting('HTK_LOCALIZED_STRING_MODEL')
)

data = {
language_code: {
localized_string.key: localized_string.value
for localized_string in LocalizedString.objects.filter(
language_code=language_code
).order_by('localizable_string__key')
}
for language_code in language_codes
}
else:
LocalizableString = resolve_model_dynamically(
htk_setting('HTK_LOCALIZABLE_STRING_MODEL')
)
localizable_strings = LocalizableString.objects.order_by('key')
data = {
localizable_string.key: localizable_string.json_encode(
include_key=False
)['translations']
for localizable_string in localizable_strings
}

return data


def dump_strings(file_path, indent=4):
data = retrieve_all_strings()
def dump_strings(file_path, indent=4, by_language=False, language_codes=None):
data = retrieve_all_strings(
by_language=False, language_codes=language_codes
)

dir_name = os.path.dirname(file_path)
os.makedirs(dir_name, exist_ok=True)
Expand Down
1 change: 1 addition & 0 deletions templates/data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{% load htk_tags %}{{ json|jsondumps:4 }}
13 changes: 13 additions & 0 deletions templates/robots.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{% spaceless %}
{% if request.host == constants.canonical_domain %}
User-agent: *
Disallow: /admin

Crawl-delay: 10
Sitemap: https://{{ request.host }}/sitemap.xml
Host: {{ request.host }}
{% else %}
User-agent: *
Disallow: /
{% endif %}
{% endspaceless %}
Loading

0 comments on commit 4861521

Please sign in to comment.