DEV: Lint MessageFormat strings to prevent usage of "one {1 foo}" (#11605)

This commit is contained in:
Gerhard Schlager 2020-12-29 21:42:47 +01:00 committed by GitHub
parent 8af6e72675
commit 6b53f26fc0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 8 deletions

View File

@ -2753,8 +2753,8 @@ en:
one: "View this post and its reply" one: "View this post and its reply"
other: "View this post and its %{count} replies" other: "View this post and its %{count} replies"
filtered_replies_viewing: filtered_replies_viewing:
one: "Viewing %{count} reply to" one: "Viewing %{count} reply to"
other: "Viewing %{count} replies to" other: "Viewing %{count} replies to"
in_reply_to: "Load parent post" in_reply_to: "Load parent post"
@ -3174,7 +3174,7 @@ en:
# keys ending with _MF use message format, see https://meta.discourse.org/t/message-format-support-for-localization/7035 for details # keys ending with _MF use message format, see https://meta.discourse.org/t/message-format-support-for-localization/7035 for details
posts_likes_MF: | posts_likes_MF: |
This topic has {count, plural, one {1 reply} other {# replies}} {ratio, select, This topic has {count, plural, one {# reply} other {# replies}} {ratio, select,
low {with a high like to post ratio} low {with a high like to post ratio}
med {with a very high like to post ratio} med {with a very high like to post ratio}
high {with an extremely high like to post ratio} high {with an extremely high like to post ratio}
@ -3550,7 +3550,6 @@ en:
tomorrow: "Until tomorrow" tomorrow: "Until tomorrow"
custom: "Custom" custom: "Custom"
# This section is exported to the javascript for i18n in the admin section # This section is exported to the javascript for i18n in the admin section
admin_js: admin_js:
type_to_filter: "type to filter..." type_to_filter: "type to filter..."
@ -4607,7 +4606,7 @@ en:
description: "users with penalties cannot reach TL3" description: "users with penalties cannot reach TL3"
# keys ending with _MF use message format, see https://meta.discourse.org/t/message-format-support-for-localization/7035 for details # keys ending with _MF use message format, see https://meta.discourse.org/t/message-format-support-for-localization/7035 for details
delete_all_posts_confirm_MF: "You are about to delete {POSTS, plural, one {1 post} other {# posts}} and {TOPICS, plural, one {1 topic} other {# topics}}. Are you sure?" delete_all_posts_confirm_MF: "You are about to delete {POSTS, plural, one {# post} other {# posts}} and {TOPICS, plural, one {# topic} other {# topics}}. Are you sure?"
silence: "Silence" silence: "Silence"
unsilence: "Unsilence" unsilence: "Unsilence"
silenced: "Silenced?" silenced: "Silenced?"

View File

@ -31,7 +31,8 @@ class LocaleFileValidator
invalid_relative_image_sources: "The following keys have relative image sources, but do not start with %{base_url} or %{base_path}:", invalid_relative_image_sources: "The following keys have relative image sources, but do not start with %{base_url} or %{base_path}:",
invalid_interpolation_key_format: "The following keys use {{key}} instead of %{key} for interpolation keys:", invalid_interpolation_key_format: "The following keys use {{key}} instead of %{key} for interpolation keys:",
wrong_pluralization_keys: "Pluralized strings must have only the sub-keys 'one' and 'other'.\nThe following keys have missing or additional keys:", wrong_pluralization_keys: "Pluralized strings must have only the sub-keys 'one' and 'other'.\nThe following keys have missing or additional keys:",
invald_one_keys: "The following keys contain the number 1 instead of the interpolation key %{count}:" invalid_one_keys: "The following keys contain the number 1 instead of the interpolation key %{count}:",
invalid_message_format_one_key: "The following keys use 'one {1 foo}' instead of the generic 'one {# foo}':",
} }
PLURALIZATION_KEYS = ['zero', 'one', 'two', 'few', 'many', 'other'] PLURALIZATION_KEYS = ['zero', 'one', 'two', 'few', 'many', 'other']
@ -81,6 +82,7 @@ class LocaleFileValidator
@errors[:invalid_relative_links] = [] @errors[:invalid_relative_links] = []
@errors[:invalid_relative_image_sources] = [] @errors[:invalid_relative_image_sources] = []
@errors[:invalid_interpolation_key_format] = [] @errors[:invalid_interpolation_key_format] = []
@errors[:invalid_message_format_one_key] = []
each_translation(yaml) do |key, value| each_translation(yaml) do |key, value|
if value.match?(/href\s*=\s*["']\/[^\/]|\]\(\/[^\/]/i) if value.match?(/href\s*=\s*["']\/[^\/]|\]\(\/[^\/]/i)
@ -94,6 +96,10 @@ class LocaleFileValidator
if value.match?(/{{.+?}}/) && !key.end_with?("_MF") if value.match?(/{{.+?}}/) && !key.end_with?("_MF")
@errors[:invalid_interpolation_key_format] << key @errors[:invalid_interpolation_key_format] << key
end end
if key.end_with?("_MF") && value.match?(/one {1.*?}/)
@errors[:invalid_message_format_one_key] << key
end
end end
end end
@ -110,7 +116,7 @@ class LocaleFileValidator
def validate_pluralizations(yaml) def validate_pluralizations(yaml)
@errors[:wrong_pluralization_keys] = [] @errors[:wrong_pluralization_keys] = []
@errors[:invald_one_keys] = [] @errors[:invalid_one_keys] = []
each_pluralization(yaml) do |key, hash| each_pluralization(yaml) do |key, hash|
# ignore errors from some ActiveRecord messages # ignore errors from some ActiveRecord messages
@ -120,7 +126,7 @@ class LocaleFileValidator
one_value = hash['one'] one_value = hash['one']
if one_value && one_value.include?('1') && !one_value.match?(/%{count}|{{count}}/) if one_value && one_value.include?('1') && !one_value.match?(/%{count}|{{count}}/)
@errors[:invald_one_keys] << key @errors[:invalid_one_keys] << key
end end
end end
end end