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.
This commit is contained in:
Loïc Guitaut 2024-07-26 15:44:46 +02:00 committed by Loïc Guitaut
parent 6f2f34f786
commit cec8445f14
2 changed files with 8 additions and 10 deletions

View File

@ -140,21 +140,15 @@ class LocaleFileChecker
end end
def check_message_format def check_message_format
mf_locale, mf_filename = require "messageformat"
JsLocaleHelper.find_message_format_locale([@locale], fallback_to_english: true)
traverse_hash(@locale_yaml, []) do |keys, value| traverse_hash(@locale_yaml, []) do |keys, value|
next unless keys.last.ends_with?("_MF") next unless keys.last.ends_with?("_MF")
begin begin
JsLocaleHelper.with_context do |ctx| MessageFormat.compile(@locale, { key: value }, strict: true)
ctx.load(mf_filename) if File.exist?(mf_filename) rescue MessageFormat::Compiler::CompileError => e
ctx.eval("mf = new MessageFormat('#{mf_locale}');") add_error(keys, TYPE_INVALID_MESSAGE_FORMAT, e.cause.message, pluralized: false)
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
end end
end end

View File

@ -144,6 +144,10 @@ RSpec.describe JsLocaleHelper do
expect(content).to_not eq("") expect(content).to_not eq("")
end end
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 end
describe ".output_MF" do describe ".output_MF" do