Skip to content

Commit 2566293

Browse files
committed
Reverted unit test changes and reverted _plural changes. Added custom getSuffixMethod()
1 parent 4700957 commit 2566293

File tree

4 files changed

+161
-127
lines changed

4 files changed

+161
-127
lines changed

README.markdown

Lines changed: 64 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -15,33 +15,33 @@ Depends on jQuery 1.3.2+ (uses $.ajax, $.each, $.extend)
1515
Usage example
1616
=============
1717

18-
$.jsperanto.init(function(t){
19-
t('project.name'); //-> "jsperanto"
20-
$.t('project.store'); //-> "JSON"
21-
22-
$.t('can_speak',{count:1}); //-> "I can only speak one language"
23-
$.t('can_speak',{count:3}); //-> "I can speak 3 languages"
24-
$.t('can_speak',{count:'any'}); //-> "I can speak any languages"
25-
26-
$.t('project.size.source',{value:4,unit:"kb"}); //-> "jsperanto is 4 kb"
27-
$.t('project.size.min',{value:1727,unit:"bytes"}) //-> "jsperanto is 1727 bytes when minified"
28-
$.t('project.size.gzip',{value:833,unit:"bytes"}) //-> "jsperanto is 833 bytes when minified and gzipped"
29-
});
30-
31-
//given this dictionary
32-
{
33-
"project" : {
34-
"name" : "jsperanto",
35-
"store" : "JSON",
36-
"size" : {
37-
"source" : "$t(project.name) is __value__ __unit__",
38-
"min" : "$t(project.size.source) when minified",
39-
"gzip" : "$t(project.size.min) and gzipped"
40-
}
41-
},
42-
"can_speak" : "I can only speak one language",
43-
"can_speak_aboveOne" : "I can speak __count__ languages"
44-
}
18+
$.jsperanto.init(function(t){
19+
t('project.name'); //-> "jsperanto"
20+
$.t('project.store'); //-> "JSON"
21+
22+
$.t('can_speak',{count:1}); //-> "I can only speak one language"
23+
$.t('can_speak',{count:3}); //-> "I can speak 3 languages"
24+
$.t('can_speak_plural',{count:'any'}); //-> "I can speak any languages"
25+
26+
$.t('project.size.source',{value:4,unit:"kb"}); //-> "jsperanto is 4 kb"
27+
$.t('project.size.min',{value:1727,unit:"bytes"}) //-> "jsperanto is 1727 bytes when minified"
28+
$.t('project.size.gzip',{value:833,unit:"bytes"}) //-> "jsperanto is 833 bytes when minified and gzipped"
29+
});
30+
31+
//given this dictionary
32+
{
33+
"project" : {
34+
"name" : "jsperanto",
35+
"store" : "JSON",
36+
"size" : {
37+
"source" : "$t(project.name) is __value__ __unit__",
38+
"min" : "$t(project.size.source) when minified",
39+
"gzip" : "$t(project.size.min) and gzipped"
40+
}
41+
},
42+
"can_speak" : "I can only speak one language",
43+
"can_speak_plural" : "I can speak __count__ languages"
44+
}
4545

4646
API
4747
===
@@ -54,24 +54,23 @@ initialize jsperanto by loading the dictionary, calling back when ready
5454

5555
**options** extends these defaults
5656

57-
o.interpolationPrefix = '__';
58-
o.interpolationSuffix = '__';
59-
o.aboveOneSuffix = '_aboveOne';
60-
o.exactlyOneSuffix = '_exactlyOne';
61-
o.belowOneSuffix = '_belowOne';
62-
o.maxRecursion = 50; //used while applying reuse of strings to avoid infinite loop
63-
o.reusePrefix = "$t("; //nested lookup prefix
64-
o.reuseSuffix = ")"; //nested lookup suffix
65-
o.fallbackLang = 'en-US'; // see Language fallback section
66-
o.dicoPath = 'locales'; // see Dictionary section
67-
o.keyseparator = "."; // keys passed to $.jsperanto.translate use this separator
68-
o.setDollarT = true; // $.t aliases $.jsperanto.translate, nice shortcut
69-
o.dictionary = false; // to supply the dictionary instead of loading it using $.ajax. A (big) javascript object containing your namespaced translations
70-
o.lang = false; //specify a language to use i.e en-US
57+
o.interpolationPrefix = '__';
58+
o.interpolationSuffix = '__';
59+
o.pluralSuffix = "_plural";
60+
o.getSuffixMethod = function(count){ return "_foo"; } // A custom method to calculate the suffix if language rules differ
61+
o.maxRecursion = 50; //used while applying reuse of strings to avoid infinite loop
62+
o.reusePrefix = "$t("; //nested lookup prefix
63+
o.reuseSuffix = ")"; //nested lookup suffix
64+
o.fallbackLang = 'en-US'; // see Language fallback section
65+
o.dicoPath = 'locales'; // see Dictionary section
66+
o.keyseparator = "."; // keys passed to $.jsperanto.translate use this separator
67+
o.setDollarT = true; // $.t aliases $.jsperanto.translate, nice shortcut
68+
o.dictionary = false; // to supply the dictionary instead of loading it using $.ajax. A (big) javascript object containing your namespaced translations
69+
o.lang = false; //specify a language to use i.e en-US
7170

7271
Use init to switch language too :
7372

74-
$.jsperanto.init(someMethod,{lang:"fr"})
73+
$.jsperanto.init(someMethod,{lang:"fr"})
7574

7675
**$.jsperanto.translate(key,options)**
7776

@@ -81,7 +80,7 @@ looks up the key in the dictionary applying plural, interpolation & nested looku
8180

8281
**options** each prop name are are used for interpolation
8382

84-
**options.count** special prop that indicates to retrieve the counted version (**key**_aboveOne, _exactlyOne, _belowOne). Also used for interpolation
83+
**options.count** special prop that indicates to retrieve the plural version (**key**_plural) if its greater than 1. Also used for interpolation
8584

8685
**options.defaultValue** specify default value if the key can't be resolved (the key itself will be sent back if no defaultValue is provided)
8786

@@ -92,7 +91,7 @@ Dictionary loading
9291

9392
Using defaults, jsperanto uses a basic browser language detection
9493

95-
(navigator.language) ? navigator.language : navigator.userLanguage
94+
(navigator.language) ? navigator.language : navigator.userLanguage
9695

9796
to determine what dictionary file to load. You can also instruct jsperanto to load a specific language (via init option _lang_).
9897

@@ -105,7 +104,25 @@ Switching language
105104

106105
Simply use init again and specify a language (or dictionary) to use.
107106

108-
$.jsperanto.init(someMethod,{lang:"fr"})
107+
$.jsperanto.init(someMethod,{lang:"fr"})
108+
109+
Custom suffixes
110+
==================
111+
112+
At init time specify a method which will calculate the suffic to use if count is present. the argument passed to this method is the count and can be a string or number.
113+
Anything other than a string returned will be disregarded.
114+
115+
$.jsperanto.init(someMethod, {
116+
lang:"en-us",
117+
getSuffixMethod : function(count){
118+
if ( count === 0 ) {
119+
return "_zero";
120+
}
121+
if ( count !== 1 ) {
122+
return "_plural";
123+
}
124+
}
125+
)
109126

110127
Licence
111128
=======
@@ -142,11 +159,10 @@ Inspiration & similar projects
142159

143160
[l10n.js](https://github.com/eligrey/l10n.js)
144161

145-
[javascript_i18n](https://github.com/qoobaa/javascript_i18n)
162+
[javascript_i18n](https://github.com/qoobaa/javascript_i18n)
146163

147164

148165
Thanks
149166
======
150167

151-
Many thanks to Radialpoint for letting me open source this work
152-
168+
Many thanks to Radialpoint for letting me open source this work

jquery.jsperanto.js

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
//jquery 1.3.2 dependencies : $.each, $.extend, $.ajax
22

33
(function($) {
4+
45
//defaults
6+
57
var o = {};
68
o.interpolationPrefix = '__';
79
o.interpolationSuffix = '__';
8-
o.aboveOneSuffix = '_aboveOne';
9-
o.exactlyOneSuffix = '_exactlyOne';
10-
o.belowOneSuffix = '_belowOne';
10+
o.pluralSuffix = '_plural';
11+
o.getSuffixMethod = false;
1112
o.maxRecursion = 50; //used while applying reuse of strings to avoid infinite loop
1213
o.reusePrefix = "$t(";
1314
o.reuseSuffix = ")";
@@ -22,6 +23,7 @@
2223
var dictionary = false; //not yet loaded
2324
var currentLang = false;
2425
var count_of_replacement = 0;
26+
2527

2628
function init(callback,options){
2729
$.extend(o,options);
@@ -32,7 +34,7 @@
3234
callback(translate);
3335
});
3436
}
35-
37+
3638
function applyReplacement(string,replacementHash){
3739
$.each(replacementHash,function(key,value){
3840
string = string.replace([o.interpolationPrefix,key,o.interpolationSuffix].join(''),value);
@@ -63,23 +65,25 @@
6365
}
6466

6567
function containsCount(options){
66-
return (typeof options.count != 'undefined');
68+
return (typeof options.count == 'number' || typeof options.count == 'string');
6769
}
6870

69-
function getCountSuffix(options) {
70-
if (!isNaN(options.count)) {
71-
return (options.count > 1) ? o.aboveOneSuffix : (options.count < 1) ? o.belowOneSuffix : o.exactlyOneSuffix;
72-
}
73-
else {
74-
return o.aboveOneSuffix;
71+
function getDefaultSuffix(count){
72+
if ( count > 1 || typeof(count) == "string" ) {
73+
return o.pluralSuffix;
7574
}
75+
}
76+
77+
function getCountSuffix(options) {
78+
var suffix = ( typeof(o.getSuffixMethod) == "function" ) ? o.getSuffixMethod(options.count) : getDefaultSuffix(options.count);
79+
return ( typeof(suffix) == "string" ) ? suffix : '';
7680
}
7781

7882
function translate(dottedkey,options){
7983
count_of_replacement = 0;
8084
return _translate(dottedkey,options);
8185
}
82-
86+
8387
/*
8488
options.defaultValue
8589
options.count
@@ -142,7 +146,7 @@
142146
function lang(){
143147
return currentLang;
144148
}
145-
149+
146150
$.jsperanto = $.jsperanto || {
147151
init:init,
148152
t:translate,

test/locales/testlang.json

Lines changed: 39 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,42 @@
11
{
2-
"sections" : {
3-
"home" : "Home",
4-
"files" : "My Files"
5-
},
6-
"product" : {
7-
"name" : "jsperanto"
8-
},
9-
"withHTML" : "<b>this would be bold</b>",
10-
"withreuse" : "$t(product.name) and $t(sections.home)",
11-
"withreplacement" : "since __year__",
12-
"4" : {
13-
"level" : {
14-
"of" : {
15-
"nesting": "4 level of nesting"
16-
}
17-
}
18-
},
19-
"countversionexist" : "non count version of countversionexist",
20-
"countversionexist_aboveOne" : "aboveOne version of countversionexist",
21-
"countversionexist_exactlyOne" : "exactlyOne version of countversionexist",
22-
"countversionexist_belowOne" : "belowOne version of countversionexist",
23-
"countversiondoesnotexist" : "count version does not exist",
24-
"count and replacement_aboveOne" : "you have __count__ friends",
25-
"count and replacement_exactlyOne" : "you have __count__ friend",
26-
"count and replacement_belowOne" : "you have __count__ friends",
2+
"sections" : {
3+
"home" : "Home",
4+
"files" : "My Files"
5+
},
6+
"product" : {
7+
"name" : "jsperanto"
8+
},
9+
"withHTML" : "<b>this would be bold</b>",
10+
"withreuse" : "$t(product.name) and $t(sections.home)",
11+
"withreplacement" : "since __year__",
12+
"4" : {
13+
"level" : {
14+
"of" : {
15+
"nesting": "4 level of nesting"
16+
}
17+
}
18+
},
19+
"pluralversionexist" : "singular version of pluralversionexist",
20+
"pluralversionexist_plural" : "plural version of pluralversionexist",
21+
"pluralversiondoesnotexist" : "plural version does not exist",
22+
"count and replacement" : "you have __count__ friend",
23+
"count and replacement_plural" : "you have __count__ friends",
2724

28-
"infinite" : "infinite $t(recursion)",
29-
"recursion" : "recursion $t(infinite)",
30-
"project" : {
31-
"name" : "jsperanto",
32-
"store" : "JSON",
33-
"size" : {
34-
"source" : "$t(project.name) is __value__ __unit__",
35-
"min" : "$t(project.size.source) when minified",
36-
"gzip" : "$t(project.size.min) and gzipped"
37-
}
38-
},
39-
"can_speak" : "I can speak a language",
40-
"can_speak_aboveOne" : "I can speak __count__ languages"
25+
"infinite" : "infinite $t(recursion)",
26+
"recursion" : "recursion $t(infinite)",
27+
"project" : {
28+
"name" : "jsperanto",
29+
"store" : "JSON",
30+
"size" : {
31+
"source" : "$t(project.name) is __value__ __unit__",
32+
"min" : "$t(project.size.source) when minified",
33+
"gzip" : "$t(project.size.min) and gzipped"
34+
}
35+
},
36+
"can_speak" : "I can only speak one language",
37+
"can_speak_plural" : "I can speak __count__ languages",
38+
39+
"countCustomSuffix" : "I have exactly one. The count is __count__",
40+
"countCustomSuffix_zero" : "I have exactly zero. The count is __count__",
41+
"countCustomSuffix_plural" : "I have many. The count is __count__"
4142
}

0 commit comments

Comments
 (0)