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

@ -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
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}
med {with a very high like to post ratio}
high {with an extremely high like to post ratio}
@ -3550,7 +3550,6 @@ en:
tomorrow: "Until tomorrow"
custom: "Custom"
# This section is exported to the javascript for i18n in the admin section
admin_js:
type_to_filter: "type to filter..."
@ -4607,7 +4606,7 @@ en:
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
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"
unsilence: "Unsilence"
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_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:",
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']
@ -81,6 +82,7 @@ class LocaleFileValidator
@errors[:invalid_relative_links] = []
@errors[:invalid_relative_image_sources] = []
@errors[:invalid_interpolation_key_format] = []
@errors[:invalid_message_format_one_key] = []
each_translation(yaml) do |key, value|
if value.match?(/href\s*=\s*["']\/[^\/]|\]\(\/[^\/]/i)
@ -94,6 +96,10 @@ class LocaleFileValidator
if value.match?(/{{.+?}}/) && !key.end_with?("_MF")
@errors[:invalid_interpolation_key_format] << key
end
if key.end_with?("_MF") && value.match?(/one {1.*?}/)
@errors[:invalid_message_format_one_key] << key
end
end
end
@ -110,7 +116,7 @@ class LocaleFileValidator
def validate_pluralizations(yaml)
@errors[:wrong_pluralization_keys] = []
@errors[:invald_one_keys] = []
@errors[:invalid_one_keys] = []
each_pluralization(yaml) do |key, hash|
# ignore errors from some ActiveRecord messages
@ -120,7 +126,7 @@ class LocaleFileValidator
one_value = hash['one']
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