Skip to content

Commit d6d271a

Browse files
committed
Add translateString method
This allows to translate a string of text programmatically, using the string translation provided.
1 parent 6109a8a commit d6d271a

File tree

3 files changed

+115
-1
lines changed

3 files changed

+115
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ labelTemplate | string | `{{country}} {{(language)}}` | a template string to int
7070

7171
Method | Argument | Description
7272
------ | -------- | -----------
73-
translate | string (languageCode) | translates the text in the given language programmatically, if no language code is specified, the default (initial) translation is used.
73+
translateString | string (textToTranslate), string (languageCode) | translates the given textToTranslate in the given languageCode. Note, this method just translates without creating side effects, returning the translated string. |
74+
translate | string (languageCode) | translates the text in the DOM to the given language programmatically, if no language code is specified, the default (initial) translation is used.
7475
destroy | None | destroys the localization tool
7576

7677
#### JSDoc

src/jquery.localizationTool.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -805,6 +805,42 @@
805805

806806
return $this;
807807
},
808+
/**
809+
* Translates a given string.
810+
*
811+
* @param {string} textToTranslate
812+
* A string of text to be translated. Requires the string is defined
813+
* during plugin initialization.
814+
* @param {string} languageCode
815+
* A known language code like 'de_DE'
816+
*
817+
* @returns {string}
818+
* The translated string. The method throws an error if it was not
819+
* possible to translate the string.
820+
*/
821+
'translateString' : function (textToTranslate, languageCode) {
822+
var $this = this;
823+
var settings = $this.data('settings');
824+
var stringsObj = settings.strings;
825+
826+
if (!settings.languages.hasOwnProperty(languageCode)) {
827+
$.error('The language code ' + languageCode + ' is not known');
828+
return;
829+
}
830+
831+
if (!stringsObj.hasOwnProperty(textToTranslate)) {
832+
$.error('The string \'' + textToTranslate + '\' was not translated in any language.');
833+
return;
834+
}
835+
836+
var oTranslations = stringsObj[textToTranslate] || {};
837+
if (!oTranslations.hasOwnProperty(languageCode)) {
838+
$.error('A translation for the string \'' + textToTranslate + '\' was not defined for language ' + languageCode + ". Defined languages are: " + Object.keys(oTranslations).join(", "));
839+
return;
840+
}
841+
842+
return oTranslations[languageCode];
843+
},
808844
/**
809845
* Destroys the dropdown widget.
810846
*

test/localizationTool_test.js

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1024,4 +1024,81 @@
10241024
}, 'out of left bound of array');
10251025
});
10261026

1027+
1028+
//////////////////////////////////////////////////////////////////////////////
1029+
Q.module('translateString', { beforeEach: function () {
1030+
addDropdownWidgetFunc();
1031+
}});
1032+
Q.test('translates a string with provided translation', function (assert) {
1033+
var $dropdown = $('#dropdown').localizationTool({
1034+
strings: {
1035+
'this is some text' : {
1036+
'it_IT' : 'this is some translation 1',
1037+
'jp_JP' : 'this is some translation 2',
1038+
'fr_FR' : 'this is some translation 3'
1039+
}
1040+
}
1041+
});
1042+
1043+
assert.equal($dropdown.localizationTool('translateString',
1044+
'this is some text',
1045+
'it_IT'
1046+
), 'this is some translation 1', 'got the expected string');
1047+
1048+
});
1049+
1050+
Q.test('throws an error when translation is made on an unknown language', function (assert) {
1051+
var $dropdown = $('#dropdown').localizationTool({
1052+
strings: {
1053+
'this is some text' : {
1054+
'it_IT' : 'this is some translation 1',
1055+
'jp_JP' : 'this is some translation 2',
1056+
'fr_FR' : 'this is some translation 3'
1057+
}
1058+
}
1059+
});
1060+
assert.throws(function () {
1061+
$dropdown.localizationTool('translateString',
1062+
'this is some text',
1063+
'fooLanguage' // this language is not knwon
1064+
);
1065+
}, /The language code fooLanguage is not known/);
1066+
});
1067+
1068+
Q.test('throws an error when translation is not defined for the given language', function (assert) {
1069+
var $dropdown = $('#dropdown').localizationTool({
1070+
strings: {
1071+
'this is some text' : {
1072+
'it_IT' : 'this is some translation 1',
1073+
'jp_JP' : 'this is some translation 2',
1074+
'fr_FR' : 'this is some translation 3'
1075+
}
1076+
}
1077+
});
1078+
assert.throws(function () {
1079+
$dropdown.localizationTool('translateString',
1080+
'this is some text',
1081+
'de_DE'
1082+
);
1083+
}, /A translation for the string 'this is some text' was not defined for language de_DE. Defined languages are: it_IT, jp_JP, fr_FR$/);
1084+
});
1085+
1086+
Q.test('throws an error when string is not translated in any language', function (assert) {
1087+
var $dropdown = $('#dropdown').localizationTool({
1088+
strings: {
1089+
'this is some text' : {
1090+
'it_IT' : 'this is some translation 1',
1091+
'jp_JP' : 'this is some translation 2',
1092+
'fr_FR' : 'this is some translation 3'
1093+
}
1094+
}
1095+
});
1096+
assert.throws(function () {
1097+
$dropdown.localizationTool('translateString',
1098+
'this is some other text',
1099+
'de_DE' // this language is not knwon
1100+
);
1101+
}, /The string 'this is some other text' was not translated in any language./);
1102+
});
1103+
10271104
}(jQuery));

0 commit comments

Comments
 (0)