Skip to content

A platform for community discussion. Free, open, simple.

DEV: Ensure MF locales are checked properly

Loïc Guitautcec8445f1475
diff --git a/lib/i18n/locale_file_checker.rb b/lib/i18n/locale_file_checker.rb
index e0aa83028fa..a66eeff7999 100644
--- a/lib/i18n/locale_file_checker.rb
+++ b/lib/i18n/locale_file_checker.rb
@@ -140,21 +140,15 @@ class LocaleFileChecker
end
def check_message_format
- mf_locale, mf_filename =
- JsLocaleHelper.find_message_format_locale([@locale], fallback_to_english: true)
+ require "messageformat"
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)
+ MessageFormat.compile(@locale, { key: value }, strict: true)
+ rescue MessageFormat::Compiler::CompileError => e
+ add_error(keys, TYPE_INVALID_MESSAGE_FORMAT, e.cause.message, pluralized: false)
end
end
end
diff --git a/spec/lib/js_locale_helper_spec.rb b/spec/lib/js_locale_helper_spec.rb
index 76d77d99e78..4f41d7d470e 100644
--- a/spec/lib/js_locale_helper_spec.rb
+++ b/spec/lib/js_locale_helper_spec.rb
@@ -144,6 +144,10 @@ RSpec.describe JsLocaleHelper do
expect(content).to_not eq("")
end
end
+
+ it "generates valid MF locales for the '#{locale[:value]}' locale" do
+ expect(described_class.output_MF(locale[:value])).not_to match(/Failed to compile/)
+ end
end
describe ".output_MF" do