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
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

View File

@ -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