A platform for community discussion. Free, open, simple.
Adds a check for invalid message formats to rake i18n:check
Gerhard Schlagerf13c34aaedec
diff --git a/lib/i18n/locale_file_checker.rb b/lib/i18n/locale_file_checker.rbindex 3250b075e7f..4d47c06af4c 100644--- a/lib/i18n/locale_file_checker.rb+++ b/lib/i18n/locale_file_checker.rb@@ -5,6 +5,7 @@ class LocaleFileCheckerTYPE_MISSING_INTERPOLATION_KEYS = 1TYPE_UNSUPPORTED_INTERPOLATION_KEYS = 2TYPE_MISSING_PLURAL_KEYS = 3+ TYPE_INVALID_MESSAGE_FORMAT = 4def check(locale)@errors = {}@@ -19,8 +20,7 @@ class LocaleFileCheckercheck_interpolation_keyscheck_plural_keys-- # TODO check MessageFormat+ check_message_formatend@errors@@ -111,6 +111,27 @@ class LocaleFileCheckerendend+ def check_message_format+ mf_locale, mf_filename = JsLocaleHelper.find_message_format_locale([@locale], true)++ traverse_hash(@locale_yaml, []) do |keys, value|+ next unless keys.last.ends_with?("_MF")++ begin+ JsLocaleHelper.with_context do |ctx|+ ctx.load(mf_filename) if File.exist?(mf_filename)+ ctx.eval("mf = new MessageFormat('#{mf_locale}');")+ ctx.eval("mf.precompile(mf.parse(#{value.to_s.inspect}))")+ end+ rescue MiniRacer::EvalError => error+ error_message = error.message.sub(/at undefined[:\d]+/, "").strip+ add_error(keys, TYPE_INVALID_MESSAGE_FORMAT, error_message, pluralized: false)+ end+ end++ JsLocaleHelper.reset_context+ end+def reference_value(keys)value = @reference_yaml[REFERENCE_LOCALE]diff --git a/lib/tasks/i18n.rake b/lib/tasks/i18n.rakeindex 79a0deb2d31..ddd7e25891f 100644--- a/lib/tasks/i18n.rake+++ b/lib/tasks/i18n.rake@@ -37,7 +37,9 @@ task "i18n:check", [:locale] => [:environment] do |_, args|when LocaleFileChecker::TYPE_UNSUPPORTED_INTERPOLATION_KEYS"Unsupported interpolation keys".redwhen LocaleFileChecker::TYPE_MISSING_PLURAL_KEYS- "Missing plural keys".yellow+ "Missing plural keys".magenta+ when LocaleFileChecker::TYPE_INVALID_MESSAGE_FORMAT+ "Invalid message format".yellowenddetails = error[:details] ? ": #{error[:details]}" : ""@@ -49,5 +51,7 @@ task "i18n:check", [:locale] => [:environment] do |_, args|failed_locales.each do |failed_locale|puts "", "Failed to check locale files for #{failed_locale}".redend++ puts ""exit 1 unless failed_locales.empty?end