mirror of
https://github.com/discourse/discourse.git
synced 2025-06-04 11:11:13 +08:00
DEV: Output failing MF keys when compilation fails
Currently, when the MessageFormat compiler fails on some translations, we just have the raw output from the compiler in the logs and that’s not always very helpful. Now, when there is an error, we iterate over the translation keys and try to compile them one by one. When we detect one that is failing, it’s added to a list that is now outputted in the logs. That way, it’s easier to know which keys are not properly translated, and the problems can be addressed quicker. --- The previous implementation of this patch had a bug: it wasn’t handling locales with country/region code properly. So instead of iterating over the problematic keys, it was raising an error.
This commit is contained in:

committed by
Loïc Guitaut

parent
f45d5dc268
commit
281570226b
@ -139,9 +139,9 @@ module JsLocaleHelper
|
||||
|
||||
message_formats =
|
||||
I18n.fallbacks[locale]
|
||||
.each_with_object({}) do |l, hash|
|
||||
.each_with_object(HashWithIndifferentAccess.new) do |l, hash|
|
||||
translations = translations_for(l, no_fallback: true)
|
||||
hash[l.to_s.dasherize] = remove_message_formats!(translations, l).merge(
|
||||
hash[l] = remove_message_formats!(translations, l).merge(
|
||||
TranslationOverride
|
||||
.mf_locales(l)
|
||||
.pluck(:translation_key, :value)
|
||||
@ -150,7 +150,8 @@ module JsLocaleHelper
|
||||
)
|
||||
end
|
||||
.compact_blank
|
||||
compiled = MessageFormat.compile(message_formats.keys, message_formats, strict: false)
|
||||
js_message_formats = message_formats.transform_keys(&:dasherize)
|
||||
compiled = MessageFormat.compile(js_message_formats.keys, js_message_formats, strict: false)
|
||||
transpiled = DiscourseJsProcessor.transpile(<<~JS, "", "discourse-mf")
|
||||
import Messages from '@messageformat/runtime/messages';
|
||||
#{compiled.sub("export default", "const msgData =")};
|
||||
@ -163,7 +164,18 @@ module JsLocaleHelper
|
||||
require("discourse-mf");
|
||||
JS
|
||||
rescue => e
|
||||
Rails.logger.error("Failed to compile message formats for #{locale} '#{e}'")
|
||||
js_locale = locale.to_s.dasherize
|
||||
message_formats[locale]
|
||||
.filter_map do |key, value|
|
||||
next if MessageFormat.compile(js_locale, value, strict: false)
|
||||
rescue StandardError
|
||||
key
|
||||
end
|
||||
.then do |strings|
|
||||
Rails.logger.error(
|
||||
"Failed to compile message formats for #{locale}.\n\nBroken strings are: #{strings.join(", ")}\n\nError: #{e}",
|
||||
)
|
||||
end
|
||||
<<~JS
|
||||
console.error("Failed to compile message formats for #{locale}. Some translation strings will be missing.");
|
||||
JS
|
||||
|
Reference in New Issue
Block a user