Commit 2a8d26b3 authored by Mark Chao's avatar Mark Chao

Merge branch 'pl-declarative-enums-translations' into 'master'

Translate descriptions for declarative_enum dynamically

See merge request gitlab-org/gitlab!74311
parents f4f44d89 174808d4
......@@ -95,8 +95,7 @@ class ApplicationRecord < ActiveRecord::Base
end
def self.declarative_enum(enum_mod)
values = enum_mod.definition.transform_values { |v| v[:value] }
enum(enum_mod.key => values)
enum(enum_mod.key => enum_mod.values)
end
def self.cached_column_list
......
......@@ -9,11 +9,11 @@ module Vulnerabilities
description 'The dismissal reason of the Vulnerability'
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.')
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.')
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.')
used_in_tests value: 3, description: _('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.')
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: 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: 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: N_('The finding is not a vulnerability because it is part of a test or is test data.')
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
......@@ -32,7 +32,7 @@ module VulnerabilitiesHelper
end
def dismissal_descriptions
Vulnerabilities::DismissalReasonEnum.definition.transform_values { |v| v[:description] }
Vulnerabilities::DismissalReasonEnum.translated_descriptions
end
def new_issue_url_for(vulnerability)
......
......@@ -175,8 +175,21 @@ RSpec.describe VulnerabilitiesHelper do
}
end
it 'incldues dismissal descriptions' do
expect(subject[:dismissal_descriptions]).to eq(expected_descriptions)
let(:translated_descriptions) do
# 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
......
......@@ -15,9 +15,9 @@
# TEXT
#
# define do
# acceptable_risk value: 0, description: '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.'
# used_in_tests value: 2, description: 'The finding is not a vulnerability because it is part of a test or is test data.'
# acceptable_risk value: 0, description: N_('The vulnerability is known but is considered to be an acceptable business risk.')
# 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: N_('The finding is not a vulnerability because it is part of a test or is test data.')
# end
#
# Then we can use this module to register enums for our Active Record models like so,
......@@ -63,6 +63,19 @@ module DeclarativeEnum
@description
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)
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