Skip to content

Commit

Permalink
Merge pull request #7852 from ckan/faster-js-i18n
Browse files Browse the repository at this point in the history
i18n js: serve faster with LazyJSONObject
  • Loading branch information
pdelboca committed Nov 13, 2023
2 parents 18ead57 + a91bab3 commit 9f2fee9
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 3 deletions.
2 changes: 2 additions & 0 deletions changes/7852.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
serve i18n js faster with LazyJSONObject
generate compact json instead of pretty-printed json to send less data
4 changes: 3 additions & 1 deletion ckan/lib/i18n.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,9 @@ def _build_js_translation(
for _, msgstr in ordered_plural:
plural.append(msgstr)
with open(dest_filename, u'w', encoding='utf-8') as f:
s = json.dumps(result, sort_keys=True, indent=2, ensure_ascii=False)
s = json.dumps(
result, sort_keys=True, ensure_ascii=False, separators=(',', ':')
)
f.write(s)


Expand Down
5 changes: 3 additions & 2 deletions ckan/views/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from ckan.lib.helpers import url_for
from ckan.lib.base import render
from ckan.lib.i18n import get_locales_from_config, get_js_translations_dir
from ckan.lib.lazyjson import LazyJSONObject

from ckan.lib.navl.dictization_functions import DataError
from ckan.logic import get_action, ValidationError, NotFound, NotAuthorized
Expand Down Expand Up @@ -478,8 +479,8 @@ def i18n_js_translations(
source = os.path.join(js_translations_folder, f"{lang}.js")
if not os.path.exists(source):
return "{}"
translations = json.load(io.open(source, "r", encoding="utf-8"))
return _finish_ok(translations)
translations = io.open(source, "r", encoding="utf-8").read()
return _finish_ok(LazyJSONObject(translations))


# Routing
Expand Down

0 comments on commit 9f2fee9

Please sign in to comment.