From 74d83cc47e93cb8199ba8b874361c52b8175838f Mon Sep 17 00:00:00 2001 From: Umair Khan Date: Fri, 30 Jun 2017 10:07:49 +0500 Subject: [PATCH] lint: Disallow handlebars within {{#tr}}{{/tr}}. This commit adds a new linter which runs from tools/travis/backend. It runs over the translations.json file and checks if any of the translatable string contains handlebars in it. Fixes #5544 --- docs/translating.md | 10 ++++++++++ tools/check-frontend-i18n | 42 +++++++++++++++++++++++++++++++++++++++ tools/travis/backend | 1 + 3 files changed, 53 insertions(+) create mode 100755 tools/check-frontend-i18n diff --git a/docs/translating.md b/docs/translating.md index 11f7921f5a..f42e15f25b 100644 --- a/docs/translating.md +++ b/docs/translating.md @@ -326,6 +326,16 @@ The rules for plurals are same as for JavaScript files. You just have to declare the appropriate keys in the resource file and then include the `count` in the context. +Note: Make sure that you don't use handlebars within the block of text +enclosed by `{{#tr}}{{/tr}}`. The reason is that in most templating +languages, handlebars are used as variable placeholders. Due to this, +translatable the string becomes different when the correct value of the +variable is substituted. The translatable strings are captured by a +preprocessor and doesn't substitude variables with values. We also have +a linter, tools/check-frontend-i18n, to disallow such patterns, so the +Travis build will not pass if the linter finds this pattern. + + ## Transifex config The config file that maps the resources from Zulip to Transifex is diff --git a/tools/check-frontend-i18n b/tools/check-frontend-i18n new file mode 100755 index 0000000000..5b33f823d5 --- /dev/null +++ b/tools/check-frontend-i18n @@ -0,0 +1,42 @@ +#!/usr/bin/env python +from __future__ import absolute_import +from __future__ import print_function + +from typing import List, Text +# check for the venv +from lib import sanity_check +sanity_check.check_venv(__file__) + +import json +import os +import sys + +sys.path.append(os.path.join(os.path.dirname(__file__), '..')) +from scripts.lib.zulip_tools import WARNING, FAIL, ENDC + +def find_handlebars(translatable_strings): + # type: (List[Text]) -> List[Text] + errored = [] + for string in translatable_strings: + if '{{' in string: + errored.append(string) + return errored + +if __name__ == '__main__': + with open('static/locale/en/translations.json') as f: + data = json.load(f) + + found = find_handlebars(list(data.keys())) + if found: + print(WARNING + "Translation strings contain handlebars:" + ENDC) + print('\n'.join(found)) + + print(WARNING + + "See http://zulip.readthedocs.io/en/latest/translating.html#frontend-translations " + "on how you can insert variables in the frontend translatable " + "strings." + + ENDC) + print(FAIL + "Failed!" + ENDC) + sys.exit(1) + else: + sys.exit(0) diff --git a/tools/travis/backend b/tools/travis/backend index d7843df46f..0210a26774 100755 --- a/tools/travis/backend +++ b/tools/travis/backend @@ -20,5 +20,6 @@ set -x ./tools/test-help-documentation.py ./tools/test-api python -W ignore tools/check-capitalization +python -W ignore tools/check-frontend-i18n # Some test suites disabled in CI for being flaky #./tools/test-queue-worker-reload