Commit 174808d4 authored by Peter Leitzen's avatar Peter Leitzen

Fix translations for vulnerabilities dismissal reason

Translate descriptions for declarative_enum dynamically using N_(...)
combined with `_(var)`.

Prior this change the translated strings were loaded in class scope so
switching the locale had no effect on them anymore. The descriptions
were always displayed in English.
parent 33d23d98
...@@ -91,8 +91,7 @@ class ApplicationRecord < ActiveRecord::Base ...@@ -91,8 +91,7 @@ class ApplicationRecord < ActiveRecord::Base
end end
def self.declarative_enum(enum_mod) def self.declarative_enum(enum_mod)
values = enum_mod.definition.transform_values { |v| v[:value] } enum(enum_mod.key => enum_mod.values)
enum(enum_mod.key => values)
end end
def self.cached_column_list def self.cached_column_list
......
...@@ -9,11 +9,11 @@ module Vulnerabilities ...@@ -9,11 +9,11 @@ module Vulnerabilities
description 'The dismissal reason of the Vulnerability' description 'The dismissal reason of the Vulnerability'
define do define do
acceptable_risk value: 0, description: _('The vulnerability is known, and has not been remediated or mitigated, but is considered to be an acceptable business risk.') acceptable_risk value: 0, description: N_('The vulnerability is known, and has not been remediated or mitigated, but is considered to be an acceptable business risk.')
false_positive value: 1, description: _('An error in reporting in which a test result incorrectly indicates the presence of a vulnerability in a system when the vulnerability is not present.') false_positive value: 1, description: N_('An error in reporting in which a test result incorrectly indicates the presence of a vulnerability in a system when the vulnerability is not present.')
mitigating_control value: 2, description: _('A management, operational, or technical control (that is, safeguard or countermeasure) employed by an organization that provides equivalent or comparable protection for an information system.') mitigating_control value: 2, description: N_('A management, operational, or technical control (that is, safeguard or countermeasure) employed by an organization that provides equivalent or comparable protection for an information system.')
used_in_tests value: 3, description: _('The finding is not a vulnerability because it is part of a test or is test data.') used_in_tests value: 3, description: N_('The finding is not a vulnerability because it is part of a test or is test data.')
not_applicable value: 4, description: _('The vulnerability is known, and has not been remediated or mitigated, but is considered to be in a part of the application that will not be updated.') not_applicable value: 4, description: N_('The vulnerability is known, and has not been remediated or mitigated, but is considered to be in a part of the application that will not be updated.')
end end
end end
end end
...@@ -32,7 +32,7 @@ module VulnerabilitiesHelper ...@@ -32,7 +32,7 @@ module VulnerabilitiesHelper
end end
def dismissal_descriptions def dismissal_descriptions
Vulnerabilities::DismissalReasonEnum.definition.transform_values { |v| v[:description] } Vulnerabilities::DismissalReasonEnum.translated_descriptions
end end
def new_issue_url_for(vulnerability) def new_issue_url_for(vulnerability)
......
...@@ -175,8 +175,21 @@ RSpec.describe VulnerabilitiesHelper do ...@@ -175,8 +175,21 @@ RSpec.describe VulnerabilitiesHelper do
} }
end end
it 'incldues dismissal descriptions' do let(:translated_descriptions) do
expect(subject[:dismissal_descriptions]).to eq(expected_descriptions) # Use dynamic translations via N_(...)
expected_descriptions.values.map { |description| _(description) }
end
it 'includes translated dismissal descriptions' do
Gitlab::I18n.with_locale(:en) do
# Force loading of the class and configured translations
Vulnerabilities::DismissalReasonEnum.translated_descriptions
end
Gitlab::I18n.with_locale(:zh_CN) do
expect(subject[:dismissal_descriptions].keys).to eq(expected_descriptions.keys)
expect(subject[:dismissal_descriptions].values).to eq(translated_descriptions)
end
end end
end end
end end
......
...@@ -15,9 +15,9 @@ ...@@ -15,9 +15,9 @@
# TEXT # TEXT
# #
# define do # define do
# acceptable_risk value: 0, description: 'The vulnerability is known but is considered to be an acceptable business risk.' # acceptable_risk value: 0, description: N_('The vulnerability is known but is considered to be an acceptable business risk.')
# false_positive value: 1, description: 'An error in reporting the presence of a vulnerability in a system when the vulnerability is not present.' # false_positive value: 1, description: N_('An error in reporting the presence of a vulnerability in a system when the vulnerability is not present.')
# used_in_tests value: 2, description: 'The finding is not a vulnerability because it is part of a test or is test data.' # used_in_tests value: 2, description: N_('The finding is not a vulnerability because it is part of a test or is test data.')
# end # end
# #
# Then we can use this module to register enums for our Active Record models like so, # Then we can use this module to register enums for our Active Record models like so,
...@@ -63,6 +63,19 @@ module DeclarativeEnum ...@@ -63,6 +63,19 @@ module DeclarativeEnum
@description @description
end end
def values
definition.transform_values { |definition| definition[:value] }
end
# Return list of dynamically translated descriptions.
#
# It is required to define descriptions with `N_(...)`.
#
# See https://github.com/grosser/fast_gettext#n_-and-nn_-make-dynamic-translations-available-to-the-parser
def translated_descriptions
definition.transform_values { |definition| _(definition[:description]) }
end
def define(&block) def define(&block)
raise LocalJumpError, 'No block given' unless block raise LocalJumpError, 'No block given' unless block
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment