summaryrefslogtreecommitdiffstats
path: root/chrome/browser/resources/shared
diff options
context:
space:
mode:
authorcsilv@chromium.org <csilv@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-07-16 20:30:34 +0000
committercsilv@chromium.org <csilv@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-07-16 20:30:34 +0000
commit73e9e831c4ee8026cd0a71e648bd06cc3b6be3b5 (patch)
tree1023e56bef21b778707acf784b8d61cd027adc97 /chrome/browser/resources/shared
parent39b6184b82a4531abbe3597a68703fb30954a22e (diff)
downloadchromium_src-73e9e831c4ee8026cd0a71e648bd06cc3b6be3b5.zip
chromium_src-73e9e831c4ee8026cd0a71e648bd06cc3b6be3b5.tar.gz
chromium_src-73e9e831c4ee8026cd0a71e648bd06cc3b6be3b5.tar.bz2
Add methods to LocalStrings to fetch strings without Windows-style accelerators.
BUG=48715 TEST=View options window using --enable-tabbed-options, verify that advanced panel shows Browse button without ampersand. Review URL: http://codereview.chromium.org/2832056 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@52728 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/resources/shared')
-rw-r--r--chrome/browser/resources/shared/js/local_strings.js59
-rw-r--r--chrome/browser/resources/shared/js/local_strings_test.html85
2 files changed, 137 insertions, 7 deletions
diff --git a/chrome/browser/resources/shared/js/local_strings.js b/chrome/browser/resources/shared/js/local_strings.js
index ace7035..ce8f3d0 100644
--- a/chrome/browser/resources/shared/js/local_strings.js
+++ b/chrome/browser/resources/shared/js/local_strings.js
@@ -13,6 +13,33 @@
function LocalStrings() {
}
+// Start of anonymous namespace.
+(function() {
+
+/**
+ * Returns a formatted string where $1 to $9 are replaced by the second to the
+ * tenth argument.
+ * @param {string} s The format string.
+ * @param {...string} The extra values to include in the formatted output.
+ * @return {string} The string after format substitution.
+ */
+function replaceArgs(s, args) {
+ return s.replace(/\$[$1-9]/g, function(m) {
+ return (m == '$$') ? '$' : args[m[1]];
+ });
+}
+
+/**
+ * Returns a string after removing Windows-style accelerators.
+ * @param {string} s The input string that may contain accelerators.
+ * @return {string} The resulting string with accelerators removed.
+ */
+function trimAccelerators(s) {
+ return s.replace(/&{1,2}/g, function(m) {
+ return (m == '&&') ? '&' : '';
+ });
+}
+
LocalStrings.prototype = {
/**
* The template data object.
@@ -38,12 +65,30 @@ LocalStrings.prototype = {
* @return {string} The formatted string.
*/
getStringF: function(id, var_args) {
- var s = this.getString(id);
- var args = arguments;
- return s.replace(/\$[$1-9]/g, function(m) {
- if (m == '$$')
- return '$';
- return args[m[1]];
- });
+ return replaceArgs(this.getString(id), arguments);
+ },
+
+ /**
+ * Gets a localized string (stripped of Windows-style accelerators) by its id.
+ * @param {string} s The ID of the string we want.
+ * @return {string} The localized string.
+ */
+ getStringWithoutAccelerator: function(id) {
+ return trimAccelerators(this.getString(id));
+ },
+
+ /**
+ * Returns a formatted localized string (stripped of Windows-style
+ * accelerators) where $1 to $9 are replaced by the second to the tenth
+ * argument.
+ * @param {string} id The ID of the string we want.
+ * @param {...string} The extra values to include in the formatted output.
+ * @return {string} The formatted string.
+ */
+ getStringWithoutAcceleratorF: function(id, var_args) {
+ return replaceArgs(this.getStringWithoutAccelerator(id), arguments);
}
};
+
+// End of anonymous namespace.
+})();
diff --git a/chrome/browser/resources/shared/js/local_strings_test.html b/chrome/browser/resources/shared/js/local_strings_test.html
new file mode 100644
index 0000000..488a3ca
--- /dev/null
+++ b/chrome/browser/resources/shared/js/local_strings_test.html
@@ -0,0 +1,85 @@
+<!DOCTYPE html>
+<html>
+<head>
+<title>LocalStrings Tests</title>
+<script src="http://closure-library.googlecode.com/svn/trunk/closure/goog/base.js"></script>
+<script src="local_strings.js"></script>
+<script>
+goog.require('goog.testing.jsunit');
+</script>
+</head>
+<body>
+<script>
+
+function testGetString() {
+ var localStrings = new LocalStrings;
+ localStrings.templateData = {
+ 'a': 'apple',
+ 'b': 'banana'
+ };
+ assertEquals(localStrings.getStringWithoutAccelerator('a'), 'apple');
+ assertEquals(localStrings.getStringWithoutAccelerator('b'), 'banana');
+ assertEquals(localStrings.getStringWithoutAccelerator('c'), '');
+}
+
+function testGetStringF() {
+ var localStrings = new LocalStrings;
+ localStrings.templateData = {
+ 'a': '$1',
+ 'b': '$1$2',
+ 'c': '$1$2$3',
+ 'd': '$1$2$3$4$5$6$7$8$9'
+ };
+ assertEquals(localStrings.getStringWithoutAcceleratorF('a', 'A'), 'A');
+ assertEquals(localStrings.getStringWithoutAcceleratorF('b', 'A', 'B'), 'AB');
+ assertEquals(localStrings.getStringWithoutAcceleratorF('b', 'A'),
+ 'Aundefined');
+ assertEquals(localStrings.getStringWithoutAcceleratorF('c', 'A', 'B', 'C'),
+ 'ABC');
+ assertEquals(localStrings.getStringWithoutAcceleratorF('c', 'A'),
+ 'Aundefinedundefined');
+ assertEquals(localStrings.getStringWithoutAcceleratorF('d', 'A', 'B', 'C',
+ 'D', 'E', 'F', 'G', 'H', 'I'), 'ABCDEFGHI');
+ assertEquals(localStrings.getStringWithoutAcceleratorF('e', 'A'), '');
+}
+
+function testGetStringWithoutAccelerator() {
+ var localStrings = new LocalStrings;
+ localStrings.templateData = {
+ 'a': 'ab',
+ 'b': 'a&b',
+ 'c': 'a&&b',
+ 'd': 'a&&&b',
+ 'e': 'a&&&&b',
+ 'f': 'a&b&c&d'
+ };
+ assertEquals(localStrings.getStringWithoutAccelerator('a'), 'ab');
+ assertEquals(localStrings.getStringWithoutAccelerator('b'), 'ab');
+ assertEquals(localStrings.getStringWithoutAccelerator('c'), 'a&b');
+ assertEquals(localStrings.getStringWithoutAccelerator('d'), 'a&b');
+ assertEquals(localStrings.getStringWithoutAccelerator('e'), 'a&&b');
+ assertEquals(localStrings.getStringWithoutAccelerator('f'), 'abcd');
+}
+
+function testGetStringWithoutAcceleratorF() {
+ var localStrings = new LocalStrings;
+ localStrings.templateData = {
+ 'a': 'ab',
+ 'b': 'a&b&c',
+ 'c': 'a$1b$2c',
+ 'd': 'a&b$1c',
+ 'e': 'a$1b&c'
+ };
+ assertEquals(localStrings.getStringWithoutAcceleratorF('a'), 'ab');
+ assertEquals(localStrings.getStringWithoutAcceleratorF('b'), 'abc');
+ assertEquals(localStrings.getStringWithoutAcceleratorF('c', 'A', 'B'),
+ 'aAbBc');
+ assertEquals(localStrings.getStringWithoutAcceleratorF('c', 'A&B', 'C&D'),
+ 'aA&BbC&Dc');
+ assertEquals(localStrings.getStringWithoutAcceleratorF('d', 'A'), 'abAc');
+ assertEquals(localStrings.getStringWithoutAcceleratorF('e', 'A'), 'aAbc');
+}
+
+</script>
+</body>
+</html>