Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTim Zallmann <tzallmann@gitlab.com>2017-10-03 18:56:40 +0300
committerTim Zallmann <tzallmann@gitlab.com>2017-10-03 18:56:40 +0300
commit23e6b17b3e7fab3fc1668234133dcf339dedc649 (patch)
treea4b5099d988447ceece848d63ad759bb6a67bfdd /app/assets/javascripts/locale
parent66dd045e6af591c370143c21ae98f36d439cd87e (diff)
parentb509588a28d0a102f4ad4b97d91c5b5944946834 (diff)
Merge branch 'winh-sprintf' into 'master'
Add basic sprintf implementation to JavaScript See merge request gitlab-org/gitlab-ce!14506
Diffstat (limited to 'app/assets/javascripts/locale')
-rw-r--r--app/assets/javascripts/locale/index.js3
-rw-r--r--app/assets/javascripts/locale/sprintf.js26
2 files changed, 29 insertions, 0 deletions
diff --git a/app/assets/javascripts/locale/index.js b/app/assets/javascripts/locale/index.js
index 6a5084efeb8..af718e894cf 100644
--- a/app/assets/javascripts/locale/index.js
+++ b/app/assets/javascripts/locale/index.js
@@ -1,5 +1,7 @@
import Jed from 'jed';
+import sprintf from './sprintf';
+
/**
This is required to require all the translation folders in the current directory
this saves us having to do this manually & keep up to date with new languages
@@ -66,4 +68,5 @@ export { lang };
export { gettext as __ };
export { ngettext as n__ };
export { pgettext as s__ };
+export { sprintf };
export default locale;
diff --git a/app/assets/javascripts/locale/sprintf.js b/app/assets/javascripts/locale/sprintf.js
new file mode 100644
index 00000000000..003cbe820d6
--- /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;
+}