/******************************************************************************
* jquery.i18n.properties
*
* Dual licensed under the GPL (http://dev.jquery.com/browser/trunk/jquery/GPL-LICENSE.txt) and
* MIT (http://dev.jquery.com/browser/trunk/jquery/MIT-LICENSE.txt) licenses.
*
* @version 1.0.x
* @author Nuno Fernandes
* @url www.codingwithcoffee.com
* @inspiration Localisation assistance for jQuery (http://keith-wood.name/localisation.html)
* by Keith Wood (kbwood{at}iinet.com.au) June 2007
*
*****************************************************************************/
(function($) {
$.i18n = {};
/** Map holding bundle keys (if mode: 'map') */
$.i18n.map = {};
/**
* Load and parse message bundle files (.properties),
* making bundles keys available as javascript variables.
*
* i18n files are named <name>.js, or <name>_<language>.js or <name>_<language>_<country>.js
* Where:
* The <language> argument is a valid ISO Language Code. These codes are the lower-case,
* two-letter codes as defined by ISO-639. You can find a full list of these codes at a
* number of sites, such as: http://www.loc.gov/standards/iso639-2/englangn.html
* The <country> argument is a valid ISO Country Code. These codes are the upper-case,
* two-letter codes as defined by ISO-3166. You can find a full list of these codes at a
* number of sites, such as: http://www.iso.ch/iso/en/prods-services/iso3166ma/02iso-3166-code-lists/list-en1.html
*
* Sample usage for a bundles/Messages.properties bundle:
* $.i18n.properties({
* name: 'Messages',
* language: 'en_US',
* path: 'bundles'
* });
* @param name (string/string[], optional) names of file to load (eg, 'Messages' or ['Msg1','Msg2']). Defaults to "Messages"
* @param language (string, optional) language/country code (eg, 'en', 'en_US', 'pt_PT'). if not specified, language reported by the browser will be used instead.
* @param path (string, optional) path of directory that contains file to load
* @param mode (string, optional) whether bundles keys are available as JavaScript variables/functions or as a map (eg, 'vars' or 'map')
* @param cache (boolean, optional) whether bundles should be cached by the browser, or forcibly reloaded on each page load. Defaults to false (i.e. forcibly reloaded)
* @param encoding (string, optional) the encoding to request for bundles. Property file resource bundles are specified to be in ISO-8859-1 format. Defaults to UTF-8 for backward compatibility.
* @param callback (function, optional) callback function to be called after script is terminated
*/
$.i18n.properties = function(settings) {
// set up settings
var defaults = {
name: 'Messages',
language: '',
path: '',
mode: 'vars',
cache: false,
encoding: 'UTF-8',
callback: null
};
settings = $.extend(defaults, settings);
if(settings.language === null || settings.language == '') {
settings.language = $.i18n.browserLang();
}
if(settings.language === null) {settings.language='';}
// load and parse bundle files
var files = getFiles(settings.name);
for(i=0; i<files.length; i++) {
// 1. load base (eg, Messages.properties)
loadAndParseFile(settings.path + files[i] + '.properties', settings);
// 2. with language code (eg, Messages_pt.properties)
if(settings.language.length >= 2) {
loadAndParseFile(settings.path + files[i] + '_' + settings.language.substring(0, 2) +'.properties', settings);
}
// 3. with language code and country code (eg, Messages_pt_PT.properties)
if(settings.language.length >= 5) {
loadAndParseFile(settings.path + files[i] + '_' + settings.language.substring(0, 5) +'.properties', settings);
}
}
// call callback
if(settings.callback){ settings.callback(); }
};
/**
* When configured with mode: 'map', allows access to bundle values by specifying its key.
* Eg, jQuery.i18n.prop('com.company.bundles.menu_add')
*/
$.i18n.prop = function(key /* Add parameters as function arguments as necessary */) {
var value = $.i18n.map[key];
if (value == null)
return '[' + key + ']';
// if(arguments.length < 2) // No arguments.
// //if(key == 'spv.lbl.modified') {alert(value);}
// return value;
// if (!$.isArray(placeHolderValues)) {
// // If placeHolderValues is not an array, make it into one.
// placeHolderValues = [placeHolderValues];
// for (var i=2; i<arguments.length; i++)
// placeHolderValues.push(arguments[i]);
// }
// Place holder replacement
/**
* Tested with:
* test.t1=asdf ''{0}''
* test.t2=asdf '{0}' '{1}'{1}'zxcv
* test.t3=This is \"a quote" 'a''{0}''s'd{fgh{ij'
* test.t4="'''{'0}''" {0}{a}
* test.t5="'''{0}'''" {1}
* test.t6=a {1} b {0} c
* test.t7=a 'quoted \\ s\ttringy' \t\t x
*
* Produces:
* test.t1, p1 ==> asdf 'p1'
* test.t2, p1 ==> asdf {0} {1}{1}zxcv
* test.t3, p1 ==> This is "a quote" a'{0}'sd{fgh{ij
* test.t4, p1 ==> "'{0}'" p1{a}
* test.t5, p1 ==> "'{0}'" {1}
* test.t6, p1 ==> a {1} b p1 c
* test.t6, p1, p2 ==> a p2 b p1 c
* test.t6, p1, p2, p3 ==> a p2 b p1 c
* test.t7 ==> a quoted \ s tringy x
*/
var i;
if (typeof(value) == 'string') {
// Handle escape characters. Done separately from the tokenizing loop below because escape characters are
// active in quoted strings.
i = 0;
while ((i = value.indexOf('\\', i)) != -1) {
if (value[i+1] == 't')
value = value.substring(0, i) + '\t' + value.substring((i++) + 2); // tab
else if (value[i+1] == 'r')
value = value.substring(0, i) + '\r' + value.substring((i++) + 2); // return
else if (value[i+1] == 'n')
value = value.substring(0, i) + '\n' + value.substring((i++) + 2); // line feed
else if (value[i+1] == 'f')
value = value.substring(0, i) + '\f' + value.substring((i++) + 2); // form feed
else if (value[i+1] == '\\')
value = value.substring(0, i) + '\\' + value.substring((i++) + 2); // \
else
value = value.substring(0, i) + value.substring(i+1); // Quietly drop the character
}
// Lazily convert the string to a list of tokens.
var arr = [], j, index;
i = 0;
while (i < value.length) {
if (value[i] == '\'') {
// Handle quotes
if (i == value.length-1)
value = value.substring(0, i); // Silently drop the trailing quote
else if (value[i+1] == '\'')
value = value.substring(0, i) + value.substring(++i); // Escaped quote
else {
// Quoted string
j = i + 2;
while ((j = value.indexOf('\'', j)) != -1) {
if (j == value.length-1 || value[j+1] != '\'') {
// Found start and end quotes. Remove them
value = value.substring(0,i) + value.substring(i+1, j) + value.substring(j+1);
i = j - 1;
break;
}
else {
// Found a double quote, reduce to a single quote.
value = value.substring(0,j) + value.substring(++j);
}
}
if (j == -1) {
// There is no end quote. Drop the start quote
value = value.substring(0,i) + value.substring(i+1);
}
}
}
else if (value[i] == '{') {
// Beginning of an unquoted place holder.
j = value.indexOf('}', i+1);
if (j == -1)
i++; // No end. Process the rest of the line. Java would throw an exception
else {
// Add 1 to the index so that it aligns with the function arguments.
index = parseInt(value.substring(i+1, j));
if (!isNaN(index) && index >= 0) {
// Put the line thus far (if it isn't empty) into the array
var s = value.substring(0, i);
if (s != "")
arr.push(s);
// Put the parameter reference into the array
arr.push(index);
// Start the processing over again starting from the rest of the li