i18next: Don't allow empty string as valid translation.

Previously we used to mark a key as unstranlated if its value was equal
to it in translations.json. This had an issue because it didn't allow
otherwise valid cases where key was equal to the value.

This commit solves the problem by disallowing an empty string as a valid
translation and then using the empty string as the value for all the
unstranslated keys.

Fixes #5261
This commit is contained in:
Umair Khan 2017-10-04 13:09:24 +05:00 committed by Tim Abbott
parent b464fd7643
commit 326a6f6b4f
3 changed files with 11 additions and 5 deletions

View File

@ -39,6 +39,7 @@ i18next.use(XHR)
detection: detectionOptions,
cache: cacheOptions,
fallbackLng: 'en',
returnEmptyString: false, // Empty string is not a valid translation.
}, function () {
var i;
initialized = true;

View File

@ -115,7 +115,7 @@ class Command(compilemessages.Command):
with open(self.get_json_filename(locale_path, locale)) as reader:
for key, value in ujson.load(reader).items():
total += 1
if key == value:
if value == '':
not_translated += 1
return (total - not_translated) * 100 // total

View File

@ -217,8 +217,8 @@ class Command(makemessages.Command):
yield os.path.join(path, self.get_namespace())
def get_new_strings(self, old_strings, translation_strings):
# type: (Mapping[str, str], List[str]) -> Dict[str, str]
def get_new_strings(self, old_strings, translation_strings, locale):
# type: (Mapping[str, str], List[str], str) -> Dict[str, str]
"""
Missing strings are removed, new strings are added and already
translated strings are not touched.
@ -226,7 +226,11 @@ class Command(makemessages.Command):
new_strings = {} # Dict[str, str]
for k in translation_strings:
k = k.replace('\\n', '\n')
new_strings[k] = old_strings.get(k, k)
if locale == 'en':
# For English language, translation is equal to the key.
new_strings[k] = old_strings.get(k, k)
else:
new_strings[k] = old_strings.get(k, "")
plurals = {k: v for k, v in old_strings.items() if k.endswith('_plural')}
for plural_key, value in plurals.items():
@ -250,7 +254,8 @@ class Command(makemessages.Command):
new_strings = {
force_text(k): v
for k, v in self.get_new_strings(old_strings,
translation_strings).items()
translation_strings,
locale).items()
}
with open(output_path, 'w') as writer:
json.dump(new_strings, writer, indent=2, sort_keys=True)