diff options
author | Douwe Maan <douwe@selenight.nl> | 2017-10-05 13:48:22 +0300 |
---|---|---|
committer | Douwe Maan <douwe@selenight.nl> | 2017-10-05 13:48:22 +0300 |
commit | 0c0c2ecd99cd5c4cb22a0c045f8d45d315203832 (patch) | |
tree | 3900ccdcdff0cd6b8e3ed281ba57a317b801ad08 /app/assets/javascripts/locale/sprintf.js | |
parent | ed3c25e4294bfc75f7167ed63e30561e39ac3a97 (diff) | |
parent | de01353b166018a43ea92b38054b1267c3ba5cea (diff) |
Merge branch 'master' into digitalmoksha/gitlab-ce-feature/verify_secondary_emails
# Conflicts:
# app/controllers/admin/users_controller.rb
# app/controllers/confirmations_controller.rb
# app/controllers/profiles/emails_controller.rb
# app/models/user.rb
# app/services/emails/base_service.rb
# app/services/emails/destroy_service.rb
# app/views/devise/mailer/confirmation_instructions.html.haml
# lib/api/users.rb
# spec/services/emails/destroy_service_spec.rb
Diffstat (limited to 'app/assets/javascripts/locale/sprintf.js')
-rw-r--r-- | app/assets/javascripts/locale/sprintf.js | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/app/assets/javascripts/locale/sprintf.js b/app/assets/javascripts/locale/sprintf.js new file mode 100644 index 00000000000..5f4a053f98e --- /dev/null +++ b/app/assets/javascripts/locale/sprintf.js @@ -0,0 +1,26 @@ +import _ from 'underscore'; + +/** + Very limited implementation of sprintf supporting only named parameters. + + @param input (translated) text with parameters (e.g. '%{num_users} users use us') + @param parameters object mapping parameter names to values (e.g. { num_users: 5 }) + @param escapeParameters whether parameter values should be escaped (see http://underscorejs.org/#escape) + @returns {String} the text with parameters replaces (e.g. '5 users use us') + + @see https://ruby-doc.org/core-2.3.3/Kernel.html#method-i-sprintf + @see https://gitlab.com/gitlab-org/gitlab-ce/issues/37992 +**/ +export default (input, parameters, escapeParameters = true) => { + let output = input; + + if (parameters) { + Object.keys(parameters).forEach((parameterName) => { + const parameterValue = parameters[parameterName]; + const escapedParameterValue = escapeParameters ? _.escape(parameterValue) : parameterValue; + output = output.replace(new RegExp(`%{${parameterName}}`, 'g'), escapedParameterValue); + }); + } + + return output; +}; |