Class: Gitlab::I18n::PoLinter

Inherits:
Object
  • Object
show all
Includes:
Utils::StrongMemoize
Defined in:
lib/gitlab/i18n/po_linter.rb

Constant Summary collapse

VARIABLE_REGEX =
/%{\w*}|%[a-z]/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(po_path:, locale: I18n.locale.to_s) ⇒ PoLinter

Returns a new instance of PoLinter.



14
15
16
17
# File 'lib/gitlab/i18n/po_linter.rb', line 14

def initialize(po_path:, locale: I18n.locale.to_s)
  @po_path = po_path
  @locale = locale
end

Instance Attribute Details

#localeObject (readonly)

Returns the value of attribute locale.



10
11
12
# File 'lib/gitlab/i18n/po_linter.rb', line 10

def locale
  @locale
end

#metadata_entryObject (readonly)

Returns the value of attribute metadata_entry.



10
11
12
# File 'lib/gitlab/i18n/po_linter.rb', line 10

def 
  @metadata_entry
end

#po_pathObject (readonly)

Returns the value of attribute po_path.



10
11
12
# File 'lib/gitlab/i18n/po_linter.rb', line 10

def po_path
  @po_path
end

#translation_entriesObject (readonly)

Returns the value of attribute translation_entries.



10
11
12
# File 'lib/gitlab/i18n/po_linter.rb', line 10

def translation_entries
  @translation_entries
end

Instance Method Details

#calculate_numbers_covering_all_pluralsObject



262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# File 'lib/gitlab/i18n/po_linter.rb', line 262

def calculate_numbers_covering_all_plurals
  required_numbers = []
  discovered_indexes = []
  counter = 0

  while discovered_indexes.size < .forms_to_test && counter < Gitlab::I18n::MetadataEntry::MAX_FORMS_TO_TEST
    index_for_count = index_for_pluralization(counter)

    unless discovered_indexes.include?(index_for_count)
      discovered_indexes << index_for_count
      required_numbers << counter
    end

    counter += 1
  end

  required_numbers
end

#errorsObject



19
20
21
# File 'lib/gitlab/i18n/po_linter.rb', line 19

def errors
  @errors ||= validate_po
end

#fill_in_variables(variables) ⇒ Object



297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
# File 'lib/gitlab/i18n/po_linter.rb', line 297

def fill_in_variables(variables)
  if variables.empty?
    []
  elsif variables.any? { |variable| unnamed_variable?(variable) }
    variables.map do |variable|
      variable == '%d' ? random_number : random_string
    end
  else
    variables.each_with_object({}) do |variable, hash|
      variable_name = variable[/\w+/]
      # The variable must be a symbol for Ruby string interpolation to work:
      # "Hello, %{world}!" % { world: 'hi' }      # Works correctly
      # "Hello, %{world}!" % { 'world' => 'hi' }  # Fails with KeyError
      hash[variable_name.to_sym] = random_string
    end
  end
end

#index_for_pluralization(counter) ⇒ Object



281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
# File 'lib/gitlab/i18n/po_linter.rb', line 281

def index_for_pluralization(counter)
  # This calls the C function that defines the pluralization rule, it can
  # return a boolean (`false` represents 0, `true` represents 1) or an integer
  # that specifies the plural form to be used for the given number
  pluralization_result = FastGettext.pluralisation_rule.call(counter)

  case pluralization_result
  when false
    0
  when true
    1
  else
    pluralization_result
  end
end

#numbers_covering_all_pluralsObject



258
259
260
# File 'lib/gitlab/i18n/po_linter.rb', line 258

def numbers_covering_all_plurals
  @numbers_covering_all_plurals ||= calculate_numbers_covering_all_plurals
end

#parse_poObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/gitlab/i18n/po_linter.rb', line 33

def parse_po
  entries = SimplePoParser.parse(po_path)

  # The first entry is the metadata entry if there is one.
  # This is an entry when empty `msgid`
  if entries.first[:msgid].empty?
    @metadata_entry = Gitlab::I18n::MetadataEntry.new(entries.shift)
  else
    return 'Missing metadata entry.'
  end

  @translation_entries = entries.map do |entry_data|
    Gitlab::I18n::TranslationEntry.new(
      entry_data: entry_data,
      nplurals: .expected_forms
    )
  end

  nil
rescue SimplePoParser::ParserError => e
  @translation_entries = []
  e.message
end

#random_numberObject



315
316
317
# File 'lib/gitlab/i18n/po_linter.rb', line 315

def random_number
  Random.rand(1000)
end

#random_stringObject



319
320
321
# File 'lib/gitlab/i18n/po_linter.rb', line 319

def random_string
  SecureRandom.alphanumeric(64)
end

#translate_plural(entry) ⇒ Object



242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/gitlab/i18n/po_linter.rb', line 242

def translate_plural(entry)
  numbers_covering_all_plurals.map do |number|
    translation = FastGettext::Translation.n_(entry.msgid, entry.plural_id, number)
    variables = entry.msgid.scan(VARIABLE_REGEX)
    plural_variables = entry.plural_id.scan(VARIABLE_REGEX)
    used_variables = (variables + plural_variables).uniq
    variables = fill_in_variables(used_variables)

    begin
      translation % variables if variables.any?
    rescue KeyError => e
      raise "Failed translation '#{translation}' with variables #{variables.keys}: #{e}"
    end
  end
end

#translate_singular(entry) ⇒ Object



227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/gitlab/i18n/po_linter.rb', line 227

def translate_singular(entry)
  used_variables = entry.msgid.scan(VARIABLE_REGEX)
  variables = fill_in_variables(used_variables)

  translation = if entry.msgid.include?('|')
                  FastGettext::Translation.s_(entry.msgid)
                else
                  FastGettext::Translation._(entry.msgid)
                end

  translation % variables if used_variables.any?
rescue KeyError => e
  raise "Failed translation '#{translation}' with variables #{variables.keys}: #{e}"
end

#unnamed_variable?(variable_name) ⇒ Boolean

Returns:

  • (Boolean)


354
355
356
# File 'lib/gitlab/i18n/po_linter.rb', line 354

def unnamed_variable?(variable_name)
  !variable_name.start_with?('%{')
end

#validate_entriesObject



57
58
59
60
61
62
63
64
65
66
# File 'lib/gitlab/i18n/po_linter.rb', line 57

def validate_entries
  errors = {}

  translation_entries.each do |entry|
    errors_for_entry = validate_entry(entry)
    errors[entry.msgid] = errors_for_entry if errors_for_entry.any?
  end

  errors
end

#validate_entry(entry) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/gitlab/i18n/po_linter.rb', line 68

def validate_entry(entry)
  errors = []

  validate_flags(errors, entry)
  validate_variables(errors, entry)
  validate_newlines(errors, entry)
  validate_number_of_plurals(errors, entry)
  validate_unescaped_chars(errors, entry)
  validate_html(errors, entry)
  validate_translation(errors, entry)
  validate_namespace(errors, entry)
  validate_spaces(errors, entry)

  errors
end

#validate_flags(errors, entry) ⇒ Object



358
359
360
# File 'lib/gitlab/i18n/po_linter.rb', line 358

def validate_flags(errors, entry)
  errors << "is marked #{entry.flag}" if entry.flag
end

#validate_html(errors, entry) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/gitlab/i18n/po_linter.rb', line 119

def validate_html(errors, entry)
  common_message = 'contains < or >. Use variables to include HTML in the string, or the &lt; and &gt; codes ' \
    'for the symbols. For more info see: https://docs.gitlab.com/ee/development/i18n/externalization.html#html'

  if entry.msgid_contains_potential_html?
    errors << common_message
  end

  if entry.plural_id_contains_potential_html?
    errors << ('plural id ' + common_message)
  end

  if entry.translations_contain_potential_html?
    errors << ('translation ' + common_message)
  end
end

#validate_namespace(errors, entry) ⇒ Object



98
99
100
101
102
103
# File 'lib/gitlab/i18n/po_linter.rb', line 98

def validate_namespace(errors, entry)
  if entry.translations_contain_namespace?
    errors << 'contains a namespace. Remove it from the translation. For more information see ' \
              'https://docs.gitlab.com/ee/development/i18n/translation.html#namespaced-strings'
  end
end

#validate_newlines(errors, entry) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/gitlab/i18n/po_linter.rb', line 146

def validate_newlines(errors, entry)
  if entry.msgid_has_multiple_lines?
    errors << 'is defined over multiple lines, this breaks some tooling.'
  end

  if entry.plural_id_has_multiple_lines?
    errors << 'plural is defined over multiple lines, this breaks some tooling.'
  end

  if entry.translations_have_multiple_lines?
    errors << 'has translations defined over multiple lines, this breaks some tooling.'
  end
end

#validate_number_of_plurals(errors, entry) ⇒ Object



136
137
138
139
140
141
142
143
144
# File 'lib/gitlab/i18n/po_linter.rb', line 136

def validate_number_of_plurals(errors, entry)
  return unless &.expected_forms
  return unless entry.translated?

  if entry.has_plural? && entry.all_translations.size != .expected_forms
    errors << "should have #{.expected_forms} "\
              "#{'translations'.pluralize(.expected_forms)}"
  end
end

#validate_poObject



23
24
25
26
27
28
29
30
31
# File 'lib/gitlab/i18n/po_linter.rb', line 23

def validate_po
  if (parse_error = parse_po)
    return 'PO-syntax errors' => [parse_error]
  end

  Gitlab::I18n.with_locale(locale) do
    validate_entries
  end
end

#validate_single_and_plural_variables(errors, entry) ⇒ Object

rubocop: disable Style/AsciiComments – Need for clarity Don’t allow mixing named and positional variables in singular and plural forms for languages such as Japanese. For example:

msgid “GlobalSearch|Showing 1 code result for %term” msgid_plural “GlobalSearch|Showing %resultsTotal code results for %term” msgstr “%termの%resultsTotal個のコード結果を表示しています” variables

Here we see that both term and resultsTotal are needed in the final translation. If we mix named and positional variables in the singular and plural forms, it could be ambiguous as to which variables belong where. rubocop: enable Style/AsciiComments



196
197
198
199
200
201
202
203
# File 'lib/gitlab/i18n/po_linter.rb', line 196

def validate_single_and_plural_variables(errors, entry)
  variables = entry.msgid.scan(VARIABLE_REGEX)
  plural_variables = entry.plural_id.scan(VARIABLE_REGEX)

  all_variables = (variables + plural_variables).uniq
  validate_unnamed_variables(errors, all_variables)
  validate_variables_in_message(errors, entry.plural_id, entry.plural_id)
end

#validate_spaces(errors, entry) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/gitlab/i18n/po_linter.rb', line 84

def validate_spaces(errors, entry)
  if entry.translations_contain_leading_space?
    errors << 'has leading space. Remove it from the translation'
  end

  if entry.translations_contain_trailing_space?
    errors << 'has trailing space. Remove it from the translation'
  end

  if entry.translations_contain_multiple_spaces?
    errors << 'has different sets of consecutive multiple spaces. Make them consistent with source string'
  end
end

#validate_translation(errors, entry) ⇒ Object



205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/gitlab/i18n/po_linter.rb', line 205

def validate_translation(errors, entry)
  if entry.has_plural?
    translate_plural(entry)
  else
    translate_singular(entry)
  end

# `sprintf` could raise an `ArgumentError` when invalid passing something
# other than a Hash when using named variables
#
# `sprintf` could raise `TypeError` when passing a wrong type when using
# unnamed variables
#
# FastGettext::Translation could raise `RuntimeError` (raised as a string),
# or as subclassess `NoTextDomainConfigured` & `InvalidFormat`
#
# `FastGettext::Translation` could raise `ArgumentError` as subclassess
# `InvalidEncoding`, `IllegalSequence` & `InvalidCharacter`
rescue ArgumentError, TypeError, RuntimeError => e
  errors << "Failure translating to #{locale} in #{po_path}: #{e.message}"
end

#validate_unescaped_chars(errors, entry) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/gitlab/i18n/po_linter.rb', line 105

def validate_unescaped_chars(errors, entry)
  if entry.msgid_contains_unescaped_chars?
    errors << 'contains unescaped `%`, escape it using `%%`'
  end

  if entry.plural_id_contains_unescaped_chars?
    errors << 'plural id contains unescaped `%`, escape it using `%%`'
  end

  if entry.translations_contain_unescaped_chars?
    errors << 'translation contains unescaped `%`, escape it using `%%`'
  end
end

#validate_unnamed_variables(errors, variables) ⇒ Object



323
324
325
326
327
328
329
330
331
332
333
# File 'lib/gitlab/i18n/po_linter.rb', line 323

def validate_unnamed_variables(errors, variables)
  unnamed_variables, named_variables = variables.partition { |name| unnamed_variable?(name) }

  if unnamed_variables.any? && named_variables.any?
    errors << 'is combining named variables with unnamed variables'
  end

  if unnamed_variables.size > 1
    errors << 'is combining multiple unnamed variables'
  end
end

#validate_variable_usage(errors, translation, required_variables) ⇒ Object



335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
# File 'lib/gitlab/i18n/po_linter.rb', line 335

def validate_variable_usage(errors, translation, required_variables)
  # We don't need to validate when the message is empty.
  # In this case we fall back to the default, which has all the
  # required variables.
  return if translation.empty?

  found_variables = translation.scan(VARIABLE_REGEX)

  missing_variables = required_variables - found_variables
  if missing_variables.any?
    errors << "<#{translation}> is missing: [#{missing_variables.to_sentence}]"
  end

  unknown_variables = found_variables - required_variables
  if unknown_variables.any?
    errors << "<#{translation}> is using unknown variables: [#{unknown_variables.to_sentence}]"
  end
end

#validate_variables(errors, entry) ⇒ Object



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/gitlab/i18n/po_linter.rb', line 160

def validate_variables(errors, entry)
  if entry.has_singular_translation?
    validate_variables_in_message(errors, entry.msgid, entry.msgid)

    validate_variables_in_message(errors, entry.msgid, entry.singular_translation)
  end

  if entry.has_plural?
    validate_single_and_plural_variables(errors, entry)

    entry.plural_translations.each do |translation|
      validate_variables_in_message(errors, entry.plural_id, translation)
    end
  end
end

#validate_variables_in_message(errors, message_id, message_translation) ⇒ Object



176
177
178
179
180
181
# File 'lib/gitlab/i18n/po_linter.rb', line 176

def validate_variables_in_message(errors, message_id, message_translation)
  required_variables = message_id.scan(VARIABLE_REGEX)

  validate_unnamed_variables(errors, required_variables)
  validate_variable_usage(errors, message_translation, required_variables)
end