From cec8445f14754df94cc8df35d809d2b8c314b978 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Guitaut?= Date: Fri, 26 Jul 2024 15:44:46 +0200 Subject: [PATCH] DEV: Ensure MF locales are checked properly This patch fixes the `i18n:check` rake task which has been broken by the `MessageFormat` upgrade. It also adds a spec to ensure we generate valid MF code for all our available locales. --- lib/i18n/locale_file_checker.rb | 14 ++++---------- spec/lib/js_locale_helper_spec.rb | 4 ++++ 2 files changed, 8 insertions(+), 10 deletions(-) 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