add jquery.i18n for client side translations

This commit is contained in:
friendica 2012-08-13 04:28:12 -07:00
parent 184cf51d2f
commit 6fcb3b4415
10 changed files with 6868 additions and 0 deletions

2
library/jquery.i18n/.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
build/compiler.jar

View File

@ -0,0 +1,19 @@
Copyright (c) 2010 Dave Perrett, http://recursive-design.com/
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

View File

@ -0,0 +1,152 @@
About
-----
_jQuery-i18n_ is a jQuery plugin for doing client-side translations in javascript. It is based heavily on [javascript i18n that almost doesn't suck](http://markos.gaivo.net/blog/?p=100) by Marko Samastur, and is licensed under the [MIT license](http://www.opensource.org/licenses/mit-license.php).
Installation
------------
You'll need to download the [jQuery library](http://docs.jquery.com/Downloading_jQuery#Current_Release), and include it before _jquery.i18n.js_ in your HTML source. See the _examples_ folder for examples.
Usage
-----
Before you can do any translation you have to initialise the plugin with a 'dictionary' (basically a property list mapping keys to their translations).
```javascript
var my_dictionary = {
'some text': 'a translation',
'some more text': 'another translation'
}
$.i18n.setDictionary(my_dictionary);
```
Once you've initialised it with a dictionary, you can translate strings using the $.i18n._() function, for example:
```javascript
$('div#example').text($.i18n._('some text'));
```
or using $('selector')._t() function
```javascript
$('div#example')._t('some text');
```
Wildcards
---------
It's straightforward to pass dynamic data into your translations. First, add _%s_ in the translation for each variable you want to swap in :
```javascript
var my_dictionary = {
"wildcard example" : "We have been passed two values : %s and %s."
}
$.i18n.setDictionary(my_dictionary);
```
Next, pass an array of values in as the second argument when you perform the translation :
```javascript
$('div#example').text($.i18n._('wildcard example', [100, 200]));
```
or
```javascript
$('div#example')._t('wildcard example', [100, 200]);
```
This will output _We have been passed two values : 100 and 200._
Because some languages will need to order arguments differently to english, you can also specify the order in which the variables appear :
```javascript
var my_dictionary = {
"wildcard example" : "We have been passed two values : %2$s and %1$s."
}
$.i18n.setDictionary(my_dictionary);
$('div#example').text($.i18n._('wildcard example', [100, 200]));
```
This will output: _We have been passed two values: 200 and 100._
Building From Scratch
---------------------
You can build the regular, un-minified version simply by running _ant_:
```bash
$ ant
Buildfile: build.xml
jquery.i18n:
[echo] Building ./jquery.i18n.js
[echo] ./jquery.i18n.js built.
BUILD SUCCESSFUL
Total time: 0 seconds
```
Before you can build the minified version yourself, you'll need to download the [Google Closure Compiler](http://closure-compiler.googlecode.com/files/compiler-latest.zip) and put it in a folder called _build_:
```bash
$ mkdir build
$ cd build
$ wget http://closure-compiler.googlecode.com/files/compiler-latest.zip
$ unzip compiler-latest.zip
```
Once you have the compiler, you can build the minified version by running _ant min_:
```bash
$ ant min
Buildfile: build.xml
jquery.i18n:
[echo] Building ./jquery.i18n.js
[echo] ./jquery.i18n.js built.
min:
[echo] Building ./jquery.i18n.min.js
[apply] Applied java to 1 file and 0 directories.
[delete] Deleting: /Users/dave/Documents/Code/jquery/jquery-i18n/tmpmin
[echo] ./jquery.i18n.min.js built.
BUILD SUCCESSFUL
Total time: 1 second
```
Bug Reports
-----------
If you come across any problems, please [create a ticket](https://github.com/recurser/jquery-i18n/issues) and we'll try to get it fixed as soon as possible.
Contributing
------------
Once you've made your commits:
1. [Fork](http://help.github.com/fork-a-repo/) jquery-i18n
2. Create a topic branch - `git checkout -b my_branch`
3. Push to your branch - `git push origin my_branch`
4. Create a [Pull Request](http://help.github.com/pull-requests/) from your branch
5. That's it!
Author
------
Dave Perrett :: mail@recursive-design.com :: [@recurser](http://twitter.com/recurser)
Copyright
---------
Copyright (c) 2010 Dave Perrett. See [License](https://github.com/recurser/jquery-i18n/blob/master/LICENSE) for details.

View File

@ -0,0 +1 @@
0.9.2

View File

@ -0,0 +1,55 @@
<?xml version="1.0" encoding="UTF-8"?>
<project name="jquery.i18n" default="jquery.i18n" basedir=".">
<tstamp />
<loadfile property="version" srcfile="VERSION"/>
<property description="Folder for jquery.i18n and min target" name="dist" value="."/>
<property name="JQI" value="${dist}/jquery.i18n.js"/>
<property name="JQI_MIN" value="${dist}/jquery.i18n.min.js"/>
<target name="jquery.i18n" description="Main jquery.i18n build, concatenates source files and replaces @VERSION">
<echo message="Building ${JQI}"/>
<mkdir dir="${dist}"/>
<concat destfile="${JQI}">
<fileset file="src/jquery.i18n.js"/>
</concat>
<replaceregexp match="@VERSION" replace="${version}" flags="g" byline="true" file="${JQI}"/>
<replaceregexp match="@DATE" replace="${DSTAMP}${TSTAMP}" file="${JQI}"/>
<echo message="${JQI} built."/>
</target>
<target name="min" depends="jquery.i18n" description="Remove all comments and whitespace, no compression, great in combination with GZip">
<echo message="Building ${JQI_MIN}"/>
<apply executable="java" parallel="false" verbose="true" dest="${dist}">
<fileset dir="${dist}">
<include name="jquery.i18n.js"/>
</fileset>
<arg line="-jar"/>
<arg path="build/compiler.jar"/>
<arg value="--warning_level"/>
<arg value="QUIET"/>
<arg value="--js_output_file"/>
<targetfile/>
<arg value="--js"/>
<mapper type="glob" from="jquery.i18n.js" to="tmpmin"/>
</apply>
<concat destfile="${JQI_MIN}">
<filelist files="${JQI}, tmpmin"/>
<filterchain>
<headfilter lines="11"/>
</filterchain>
</concat>
<concat destfile="${JQI_MIN}" append="yes">
<filelist files="tmpmin"/>
</concat>
<delete file="tmpmin"/>
<echo message="${JQI_MIN} built."/>
</target>
<target name="clean">
<delete dir="tmpmin"/>
</target>
</project>

View File

@ -0,0 +1,78 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf8" />
<title>jQuery i18n Plugin</title>
<script type="text/javascript" src="jquery-1.4.2.js"></script>
<script type="text/javascript" src="../jquery.i18n.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
i18n_dict = {
"Example 1" : "teiän veen",
"Example 2" : "tei'än ve'en",
"Example 3" : "teiä vede",
"Example 4" : "teirän veren",
"Example 5" : "teilän velen",
"Example 6" : "teijjän vejen",
"Example 7" : "teidän veden",
"Example 8" : "teitän veten",
"Example 9" : "teiðän veðen",
"Example 10" : "teidhän vethen",
"Dynamic Content" : "Your browser window is %s x %s",
"Ordered Dynamic Content": "%2$s is the height of your browser window, and %1$s is the width."
};
$.i18n.setDictionary(i18n_dict);
$('input#translate_button').click( function(event) {
$('div#example1').text($.i18n._('Example 1'));
$('div#example2').text($.i18n._('Example 2'));
$('div#example3').text($.i18n._('Example 3'));
$('div#example4').text($.i18n._('Example 4'));
$('div#example5').text($.i18n._('Example 5'));
$('div#example6').text($.i18n._('Example 6'));
$('div#example7').text($.i18n._('Example 7'));
$('div#example8').text($.i18n._('Example 8'));
$('div#example9').text($.i18n._('Example 9'));
$('div#example10').text($.i18n._('Example 10'));
$('div#dynamic').text($.i18n._('Dynamic Content', [$(document).width(), $(document).height()]));
$('div#orderedDynamic').text($.i18n._('Ordered Dynamic Content', [$(document).width(), $(document).height()]));
});
});
</script>
<style type="text/css">
body {
font-size: 30px;
text-align: center;
}
input {
font-size: 30px;
}
p {
font-size: 17px;
}
</style>
<body>
<p>
Click the button to translate the following text into some random Finnish from the
<a href='http://en.wikipedia.org/wiki/Finnish_phonology'>Wikipedia Finnish Phonology Article</a>
</p>
<div id='example1'>Example 1</div>
<div id='example2'>Example 2</div>
<div id='example3'>Example 3</div>
<div id='example4'>Example 4</div>
<div id='example5'>Example 5</div>
<div id='example6'>Example 6</div>
<div id='example7'>Example 7</div>
<div id='example8'>Example 8</div>
<div id='example9'>Example 9</div>
<div id='example10'>Example 10</div>
<div id='dynamic'>Dynamic Content</div>
<div id='orderedDynamic'>Ordered Dynamic Content</div>
<input type='button' id='translate_button' value='Internationalize!' />
</body>
</html>

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,154 @@
/*
* jQuery i18n plugin
* @requires jQuery v1.1 or later
*
* See http://recursive-design.com/projects/jquery-i18n/
*
* Licensed under the MIT license:
* http://www.opensource.org/licenses/mit-license.php
*
* Version: 0.9.2 (201204070102)
*/
(function($) {
/**
* i18n provides a mechanism for translating strings using a jscript dictionary.
*
*/
/*
* i18n property list
*/
$.i18n = {
dict: null,
/**
* setDictionary()
* Initialise the dictionary and translate nodes
*
* @param property_list i18n_dict : The dictionary to use for translation
*/
setDictionary: function(i18n_dict) {
this.dict = i18n_dict;
},
/**
* _()
* The actual translation function. Looks the given string up in the
* dictionary and returns the translation if one exists. If a translation
* is not found, returns the original word
*
* @param string str : The string to translate
* @param property_list params : params for using printf() on the string
* @return string : Translated word
*
*/
_: function (str, params) {
var transl = str;
if (this.dict && this.dict[str]) {
transl = this.dict[str];
}
return this.printf(transl, params);
},
/**
* toEntity()
* Change non-ASCII characters to entity representation
*
* @param string str : The string to transform
* @return string result : Original string with non-ASCII content converted to entities
*
*/
toEntity: function (str) {
var result = '';
for (var i=0;i<str.length; i++) {
if (str.charCodeAt(i) > 128)
result += "&#"+str.charCodeAt(i)+";";
else
result += str.charAt(i);
}
return result;
},
/**
* stripStr()
*
* @param string str : The string to strip
* @return string result : Stripped string
*
*/
stripStr: function(str) {
return str.replace(/^\s*/, "").replace(/\s*$/, "");
},
/**
* stripStrML()
*
* @param string str : The multi-line string to strip
* @return string result : Stripped string
*
*/
stripStrML: function(str) {
// Split because m flag doesn't exist before JS1.5 and we need to
// strip newlines anyway
var parts = str.split('\n');
for (var i=0; i<parts.length; i++)
parts[i] = stripStr(parts[i]);
// Don't join with empty strings, because it "concats" words
// And strip again
return stripStr(parts.join(" "));
},
/*
* printf()
* C-printf like function, which substitutes %s with parameters
* given in list. %%s is used to escape %s.
*
* Doesn't work in IE5.0 (splice)
*
* @param string S : string to perform printf on.
* @param string L : Array of arguments for printf()
*/
printf: function(S, L) {
if (!L) return S;
var nS = "";
var search = /%(\d+)\$s/g;
// replace %n1$ where n is a number
while (result = search.exec(S)) {
var index = parseInt(result[1], 10) - 1;
S = S.replace('%' + result[1] + '\$s', (L[index]));
L.splice(index, 1);
}
var tS = S.split("%s");
if (tS.length > 1) {
for(var i=0; i<L.length; i++) {
if (tS[i].lastIndexOf('%') == tS[i].length-1 && i != L.length-1)
tS[i] += "s"+tS.splice(i+1,1)[0];
nS += tS[i] + L[i];
}
}
return nS + tS[tS.length-1];
}
};
/*
* _t
* Allows you to translate a jQuery selector
*
* eg $('h1')._t('some text')
*
* @param string str : The string to translate
* @param property_list params : params for using printf() on the string
* @return element : chained and translated element(s)
*/
$.fn._t = function(str, params) {
return $(this).text($.i18n._(str, params));
};
})(jQuery);

13
library/jquery.i18n/jquery.i18n.min.js vendored Normal file
View File

@ -0,0 +1,13 @@
/*
* jQuery i18n plugin
* @requires jQuery v1.1 or later
*
* See http://recursive-design.com/projects/jquery-i18n/
*
* Licensed under the MIT license:
* http://www.opensource.org/licenses/mit-license.php
*
* Version: 0.9.2 (201204070102)
*/
(function(f){f.i18n={dict:null,setDictionary:function(a){this.dict=a},_:function(a,b){var d=a;if(this.dict&&this.dict[a])d=this.dict[a];return this.printf(d,b)},toEntity:function(a){for(var b="",d=0;d<a.length;d++)b+=a.charCodeAt(d)>128?"&#"+a.charCodeAt(d)+";":a.charAt(d);return b},stripStr:function(a){return a.replace(/^\s*/,"").replace(/\s*$/,"")},stripStrML:function(a){a=a.split("\n");for(var b=0;b<a.length;b++)a[b]=stripStr(a[b]);return stripStr(a.join(" "))},printf:function(a,b){if(!b)return a;
for(var d="",e=/%(\d+)\$s/g;result=e.exec(a);){var c=parseInt(result[1],10)-1;a=a.replace("%"+result[1]+"$s",b[c]);b.splice(c,1)}e=a.split("%s");if(e.length>1)for(c=0;c<b.length;c++){if(e[c].lastIndexOf("%")==e[c].length-1&&c!=b.length-1)e[c]+="s"+e.splice(c+1,1)[0];d+=e[c]+b[c]}return d+e[e.length-1]}};f.fn._t=function(a,b){return f(this).text(f.i18n._(a,b))}})(jQuery);

View File

@ -0,0 +1,154 @@
/*
* jQuery i18n plugin
* @requires jQuery v1.1 or later
*
* See http://recursive-design.com/projects/jquery-i18n/
*
* Licensed under the MIT license:
* http://www.opensource.org/licenses/mit-license.php
*
* Version: @VERSION (@DATE)
*/
(function($) {
/**
* i18n provides a mechanism for translating strings using a jscript dictionary.
*
*/
/*
* i18n property list
*/
$.i18n = {
dict: null,
/**
* setDictionary()
* Initialise the dictionary and translate nodes
*
* @param property_list i18n_dict : The dictionary to use for translation
*/
setDictionary: function(i18n_dict) {
this.dict = i18n_dict;
},
/**
* _()
* The actual translation function. Looks the given string up in the
* dictionary and returns the translation if one exists. If a translation
* is not found, returns the original word
*
* @param string str : The string to translate
* @param property_list params : params for using printf() on the string
* @return string : Translated word
*
*/
_: function (str, params) {
var transl = str;
if (this.dict && this.dict[str]) {
transl = this.dict[str];
}
return this.printf(transl, params);
},
/**
* toEntity()
* Change non-ASCII characters to entity representation
*
* @param string str : The string to transform
* @return string result : Original string with non-ASCII content converted to entities
*
*/
toEntity: function (str) {
var result = '';
for (var i=0;i<str.length; i++) {
if (str.charCodeAt(i) > 128)
result += "&#"+str.charCodeAt(i)+";";
else
result += str.charAt(i);
}
return result;
},
/**
* stripStr()
*
* @param string str : The string to strip
* @return string result : Stripped string
*
*/
stripStr: function(str) {
return str.replace(/^\s*/, "").replace(/\s*$/, "");
},
/**
* stripStrML()
*
* @param string str : The multi-line string to strip
* @return string result : Stripped string
*
*/
stripStrML: function(str) {
// Split because m flag doesn't exist before JS1.5 and we need to
// strip newlines anyway
var parts = str.split('\n');
for (var i=0; i<parts.length; i++)
parts[i] = stripStr(parts[i]);
// Don't join with empty strings, because it "concats" words
// And strip again
return stripStr(parts.join(" "));
},
/*
* printf()
* C-printf like function, which substitutes %s with parameters
* given in list. %%s is used to escape %s.
*
* Doesn't work in IE5.0 (splice)
*
* @param string S : string to perform printf on.
* @param string L : Array of arguments for printf()
*/
printf: function(S, L) {
if (!L) return S;
var nS = "";
var search = /%(\d+)\$s/g;
// replace %n1$ where n is a number
while (result = search.exec(S)) {
var index = parseInt(result[1], 10) - 1;
S = S.replace('%' + result[1] + '\$s', (L[index]));
L.splice(index, 1);
}
var tS = S.split("%s");
if (tS.length > 1) {
for(var i=0; i<L.length; i++) {
if (tS[i].lastIndexOf('%') == tS[i].length-1 && i != L.length-1)
tS[i] += "s"+tS.splice(i+1,1)[0];
nS += tS[i] + L[i];
}
}
return nS + tS[tS.length-1];
}
};
/*
* _t
* Allows you to translate a jQuery selector
*
* eg $('h1')._t('some text')
*
* @param string str : The string to translate
* @param property_list params : params for using printf() on the string
* @return element : chained and translated element(s)
*/
$.fn._t = function(str, params) {
return $(this).text($.i18n._(str, params));
};
})(jQuery);