From d4dfeb57fd316434cb96c6c8a1814cd760df7262 Mon Sep 17 00:00:00 2001 From: Tim Abbott Date: Wed, 27 May 2020 14:03:16 -0700 Subject: [PATCH] lint: Add i18n linter rule for invalid i18n.t tags. After seeing yet another contributor accidentally try to add i18n tags that don't work using this pattern, it's time for a lint rule. --- frontend_tests/node_tests/compose.js | 2 +- static/js/alert_words_ui.js | 5 +++-- static/js/components.js | 2 +- static/js/compose.js | 9 +++------ static/js/settings_bots.js | 3 +-- tools/linter_lib/custom_check.py | 3 +++ 6 files changed, 12 insertions(+), 12 deletions(-) diff --git a/frontend_tests/node_tests/compose.js b/frontend_tests/node_tests/compose.js index 93d320c908..746258f121 100644 --- a/frontend_tests/node_tests/compose.js +++ b/frontend_tests/node_tests/compose.js @@ -225,7 +225,7 @@ run_test('validate', () => { reminder.is_deferred_delivery = () => true; compose.validate(); - assert.equal($('#sending-indicator').html(), 'translated: Scheduling...'); + assert.equal($('#sending-indicator').text(), 'translated: Scheduling...'); reminder.is_deferred_delivery = noop; add_content_to_compose_box(); diff --git a/static/js/alert_words_ui.js b/static/js/alert_words_ui.js index 9e2ab92734..0f62276358 100644 --- a/static/js/alert_words_ui.js +++ b/static/js/alert_words_ui.js @@ -45,8 +45,9 @@ function add_alert_word(alert_word) { url: '/json/users/me/alert_words', data: {alert_words: JSON.stringify(words_to_be_added)}, success: function () { - const message = "Alert word \"" + words_to_be_added + "\" added successfully!"; - update_alert_word_status(i18n.t(message), false); + const message = i18n.t('Alert word "__word__" added successfully!', + {word: words_to_be_added[0]}); + update_alert_word_status(message, false); $('#create_alert_word_name').val(''); }, error: function () { diff --git a/static/js/components.js b/static/js/components.js index 6575de2cd4..cc7e6858b2 100644 --- a/static/js/components.js +++ b/static/js/components.js @@ -2,7 +2,7 @@ Toggle x = components.toggle({ selected: Integer selected_index, values: Array [ - { label: i18n.t(String title) } + { label: i18n.t("String title") } ], callback: function () { // .. on value change. diff --git a/static/js/compose.js b/static/js/compose.js index eab0484779..2c14da368d 100644 --- a/static/js/compose.js +++ b/static/js/compose.js @@ -75,10 +75,7 @@ exports.clear_all_everyone_warnings = function () { }; function show_sending_indicator(whats_happening) { - if (whats_happening === undefined) { - whats_happening = 'Sending...'; - } - $("#sending-indicator").html(i18n.t(whats_happening)); + $("#sending-indicator").text(whats_happening); $("#sending-indicator").show(); } @@ -647,9 +644,9 @@ exports.validate = function () { $("#compose-send-button").attr('disabled', 'disabled').blur(); const message_content = compose_state.message_content(); if (reminder.is_deferred_delivery(message_content)) { - show_sending_indicator('Scheduling...'); + show_sending_indicator(i18n.t('Scheduling...')); } else { - show_sending_indicator(); + show_sending_indicator(i18n.t('Sending...')); } if (/^\s*$/.test(message_content)) { diff --git a/static/js/settings_bots.js b/static/js/settings_bots.js index ea71dcd90b..c09e0ef4d7 100644 --- a/static/js/settings_bots.js +++ b/static/js/settings_bots.js @@ -64,8 +64,7 @@ function is_local_part(value, element) { } exports.type_id_to_string = function (type_id) { - const name = page_params.bot_types.find(bot_type => bot_type.type_id === type_id).name; - return i18n.t(name); + return page_params.bot_types.find(bot_type => bot_type.type_id === type_id).name; }; exports.render_bots = function () { diff --git a/tools/linter_lib/custom_check.py b/tools/linter_lib/custom_check.py index 949751cd45..c2a8539e1b 100644 --- a/tools/linter_lib/custom_check.py +++ b/tools/linter_lib/custom_check.py @@ -125,6 +125,9 @@ js_rules = RuleList( 'description': 'i18n string should not be a multiline string'}, {'pattern': r'''i18n\.t\(['"].+?['"]\s*\+''', 'description': 'Do not concatenate arguments within i18n.t()'}, + {'pattern': r'''i18n\.t\([a-zA-Z]''', + 'exclude': {'static/js/templates.js'}, + 'description': 'Do not pass a variable into i18n.t; it will not be exported to Transifex for translation.'}, {'pattern': r'i18n\.t\(.+\).*\+', 'description': 'Do not concatenate i18n strings'}, {'pattern': r'\+.*i18n\.t\(.+\)',