FIX: Allow invalid plural keys in MF translations

We can get translations with invalid plural keys from Crowdin
or from custom overrides. Currently, this will raise an error and the
locales won’t be outputted at all.

This patch addresses this issue by using the new `strict: false` option
of our `messageformat-wrapper` gem, allowing to generate locales even if
there are invalid plural keys present.
This commit is contained in:
Loïc Guitaut
2024-07-24 18:05:17 +02:00
committed by Loïc Guitaut
parent 5dca68dc1d
commit c4845acf5e
3 changed files with 40 additions and 8 deletions

View File

@ -236,7 +236,7 @@ GEM
memory_profiler (1.0.2) memory_profiler (1.0.2)
message_bus (4.3.8) message_bus (4.3.8)
rack (>= 1.1.3) rack (>= 1.1.3)
messageformat-wrapper (1.0.0) messageformat-wrapper (1.1.0)
mini_racer (>= 0.6.3) mini_racer (>= 0.6.3)
method_source (1.1.0) method_source (1.1.0)
mini_mime (1.1.5) mini_mime (1.1.5)

View File

@ -150,7 +150,7 @@ module JsLocaleHelper
) )
end end
.compact_blank .compact_blank
compiled = MessageFormat.compile(message_formats.keys, message_formats) compiled = MessageFormat.compile(message_formats.keys, message_formats, strict: false)
transpiled = DiscourseJsProcessor.transpile(<<~JS, "", "discourse-mf") transpiled = DiscourseJsProcessor.transpile(<<~JS, "", "discourse-mf")
import Messages from '@messageformat/runtime/messages'; import Messages from '@messageformat/runtime/messages';
#{compiled.sub("export default", "const msgData =")}; #{compiled.sub("export default", "const msgData =")};

View File

@ -147,18 +147,34 @@ RSpec.describe JsLocaleHelper do
end end
describe ".output_MF" do describe ".output_MF" do
let(:output) { described_class.output_MF(locale).gsub(/^import.*$/, "") } fab!(:overriden_translation_en) do
let(:generated_locales) { v8_ctx.eval("Object.keys(I18n._mfMessages._data)") }
let(:translated_message) do
v8_ctx.eval("I18n._mfMessages.get('posts_likes_MF', {count: 3, ratio: 'med'})")
end
let!(:overriden_translation) do
Fabricate( Fabricate(
:translation_override, :translation_override,
translation_key: "admin_js.admin.user.penalty_history_MF", translation_key: "admin_js.admin.user.penalty_history_MF",
value: "OVERRIDEN", value: "OVERRIDEN",
) )
end end
fab!(:overriden_translation_ja) do
Fabricate(
:translation_override,
locale: "ja",
translation_key: "js.posts_likes_MF",
value: "{ count, plural, one {返信 # 件、} other {返信 # 件、} }",
)
end
fab!(:overriden_translation_he) do
Fabricate(
:translation_override,
locale: "he",
translation_key: "js.posts_likes_MF",
value: "{ count, plural, ",
)
end
let(:output) { described_class.output_MF(locale).gsub(/^import.*$/, "") }
let(:generated_locales) { v8_ctx.eval("Object.keys(I18n._mfMessages._data)") }
let(:translated_message) do
v8_ctx.eval("I18n._mfMessages.get('posts_likes_MF', {count: 3, ratio: 'med'})")
end
before { v8_ctx.eval(output) } before { v8_ctx.eval(output) }
@ -227,5 +243,21 @@ RSpec.describe JsLocaleHelper do
end end
end end
end end
context "when locale contains invalid plural keys" do
let(:locale) { "ja" }
it "does not raise an error" do
expect(generated_locales).to match(%w[ja en])
end
end
context "when locale contains malformed messages" do
let(:locale) { "he" }
it "raises an error" do
expect(output).to match(/Failed to compile message formats/)
end
end
end end
end end