2
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2025-09-04 08:47:37 +08:00

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

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