- Notifications
You must be signed in to change notification settings - Fork5.5k
Work on loading UI translations#2969
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes from1 commit
Commits
Show all changes
8 commits Select commitHold shift + click to select a range
9dd5d5c Load translations for Javascript in page template
takluyvercc01b45 Normalise language codes to gettext format with underscores
takluyver1495f22 .mo files need to be under LC_MESSAGES as well
takluyverf8265aa remove unused JS code
takluyvere5f1f88 Normalise result in test
takluyver31599b5 Fix for opening files on Py 2
takluyver4a6bd1d Fix location of I18N directory
takluyverc075b56 Add translation files to package_data
takluyverFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
NextNext commit
Load translations for Javascript in page template
- Loading branch information
Uh oh!
There was an error while loading.Please reload this page.
commit9dd5d5c1f81e5a08013f83f831deca8b82203f8b
There are no files selected for viewing
3 changes: 3 additions & 0 deletionsnotebook/base/handlers.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletionsnotebook/i18n/__init__.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| """Server functions for loading translations | ||
| """ | ||
| from collections import defaultdict | ||
| import errno | ||
| import json | ||
| from os.path import dirname, join as pjoin | ||
| import re | ||
| I18N_DIR = pjoin(dirname(dirname(__file__)), 'i18n') | ||
| # Cache structure: | ||
| # {'nbjs': { # Domain | ||
| # 'zh-CN': { # Language code | ||
| # <english string>: <translated string> | ||
| # ... | ||
| # } | ||
| # }} | ||
| TRANSLATIONS_CACHE = {'nbjs': {}} | ||
| _accept_lang_re = re.compile(r''' | ||
| (?P<lang>[a-zA-Z]{1,8}(-[a-zA-Z]{1,8})?) | ||
| (\s*;\s*q\s*=\s* | ||
| (?P<qvalue>[01](.\d+)?) | ||
| )?''', re.VERBOSE) | ||
| def parse_accept_lang_header(accept_lang): | ||
| """Parses the 'Accept-Language' HTTP header. | ||
| Returns a list of language codes in *ascending* order of preference | ||
| (with the most preferred language last). | ||
| """ | ||
| by_q = defaultdict(list) | ||
| for part in accept_lang.split(','): | ||
| m = _accept_lang_re.match(part.strip()) | ||
| if not m: | ||
| continue | ||
| lang, qvalue = m.group('lang', 'qvalue') | ||
| if qvalue is None: | ||
| qvalue = 1. | ||
| else: | ||
| qvalue = float(qvalue) | ||
| if qvalue == 0: | ||
| continue # 0 means not accepted | ||
| by_q[qvalue].append(lang) | ||
| res = [] | ||
| for qvalue, langs in sorted(by_q.items()): | ||
| res.extend(sorted(langs)) | ||
| return res | ||
| def load(language, domain='nbjs'): | ||
| """Load translations from an nbjs.json file""" | ||
| try: | ||
| f = open(pjoin(I18N_DIR, language, 'LC_MESSAGES', 'nbjs.json'), encoding='utf-8') | ||
| except OSError as e: | ||
| if e.errno != errno.ENOENT: | ||
| raise | ||
| return {} | ||
| with f: | ||
| data = json.load(f) | ||
| return data["locale_data"][domain] | ||
| def cached_load(language, domain='nbjs'): | ||
| """Load translations for one language, using in-memory cache if available""" | ||
| domain_cache = TRANSLATIONS_CACHE[domain] | ||
| try: | ||
| return domain_cache[language] | ||
| except KeyError: | ||
| data = load(language, domain) | ||
| domain_cache[language] = data | ||
| return data | ||
| def combine_translations(accept_language, domain='nbjs'): | ||
| """Combine translations for multiple accepted languages. | ||
| Returns data re-packaged in jed1.x format. | ||
| """ | ||
| lang_codes = parse_accept_lang_header(accept_language) | ||
| combined = {} | ||
| for language in lang_codes: | ||
| if language == 'en': | ||
| # en is default, all translations are in frontend. | ||
| combined.clear() | ||
| else: | ||
| combined.update(cached_load(language, domain)) | ||
| combined[''] = {"domain":"nbjs"} | ||
| return { | ||
| "domain": domain, | ||
| "locale_data": { | ||
| domain: combined | ||
| } | ||
| } |
37 changes: 3 additions & 34 deletionsnotebook/static/base/js/i18n.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
26 changes: 0 additions & 26 deletionsnotebook/static/base/js/i18nload.js
This file was deleted.
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
2 changes: 2 additions & 0 deletionsnotebook/templates/page.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -97,6 +97,8 @@ | ||
| return {}; | ||
| } | ||
| }) | ||
| document.nbjs_translations = {{ nbjs_translations|safe }}; | ||
| </script> | ||
| {% block meta %} | ||
10 changes: 10 additions & 0 deletionsnotebook/tests/test_i18n.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| import nose.tools as nt | ||
| from notebook import i18n | ||
| def test_parse_accept_lang_header(): | ||
| palh = i18n.parse_accept_lang_header | ||
| nt.assert_equal(palh(''), []) | ||
| nt.assert_equal(palh('zh-CN,en-GB;q=0.7,en;q=0.3'), | ||
| ['en', 'en-GB', 'zh-CN']) | ||
| nt.assert_equal(palh('nl,fr;q=0'), ['nl']) |
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.