Simple design attribution until we get a proper AUTHORS file or credits page
[ajatus.git] / js / ajatus.i10n.js
blobab96aae9e08e3e6a96664d4a9e284ac8f66a9bd4
1 /*
2 * This file is part of
4 * Ajatus - Distributed CRM
5 * @requires jQuery v1.2.1
6 *
7 * Copyright (c) 2007 Jerry Jalava <jerry.jalava@gmail.com>
8 * Copyright (c) 2007 Nemein Oy <http://nemein.com>
9 * Website: http://ajatus.info
10 * Licensed under the GPL license
11 * http://www.gnu.org/licenses/gpl.html
15 (function($){
16 $.ajatus = $.ajatus || {};
18 $.ajatus.i10n = {
19 lang: 'en_GB',
20 dict: {},
21 initialized: false,
22 available: null
24 $.extend($.ajatus.i10n, {
25 init: function(lang) {
26 $.ajatus.i10n.lang = lang || 'en_GB';
27 $.ajatus.i10n._load_lang($.ajatus.i10n.lang);
29 get_available_langs: function(force_refresh) {
30 if (typeof force_refresh == 'undefined') {
31 var force_refresh = false;
33 var refresh = false;
35 if ($.ajatus.i10n.available == null) {
36 $.ajatus.i10n.available = [];
37 refresh = true;
40 if ( refresh
41 || force_refresh)
43 // TODO: Somehow get all available translations from js/languages -folder
44 $.ajatus.i10n.available = [ 'en_GB', 'fi_FI' ];
47 return $.ajatus.i10n.available;
49 get: function() { //string, keys, pluralize_key
50 if (arguments.length < 1) {
51 return;
53 var string = new String();
54 var trans_key = arguments[0].toString().toLowerCase();
55 // console.log("i10n get: "+trans_key);
57 var replace_args = [];
58 if ( arguments.length > 1
59 && typeof(arguments[1]) == 'object')
61 replace_args = arguments[1];
64 var auto_plural = false;
65 if (arguments.length < 3) {
66 auto_plural = true;
69 if (typeof($.ajatus.i10n.dict[trans_key]) != 'undefined') {
70 string = String($.ajatus.i10n.dict[trans_key]);
71 } else {
72 string = arguments[0].toString();
75 if (replace_args.length > 0) {
76 string = $.ajatus.i10n.format_result(string, replace_args, auto_plural);
79 if ( arguments.length > 2
80 && typeof(arguments[2]) == 'string')
82 string = $.ajatus.i10n.plural(string, arguments[2]);
85 // console.log("return: "+string);
86 return string;
87 },
88 format_result: function(string, args, auto_plural) {
89 if (typeof auto_plural == 'undefined') {
90 var auto_plural = true;
92 $.each(args, function(i,value){
93 var key = $.ajatus.i10n._resolve_replace_type(value);
94 if ( key == '%d'
95 && auto_plural
96 && (value > 1))
98 var re = new RegExp(/\b[%]?d.(\w+)\b/);
99 var m = re.exec(string);
100 if (m != null) {
101 var sing = m[(m.length-1)];
102 var plural = $.ajatus.i10n.inflector.pluralize(sing);
103 string = string.toString().replace(sing, plural);
106 string = string.toString().replace(key, value);
108 return string;
110 plural: function(string, key) {
111 if (typeof key == 'undefined') {
112 var key = string;
115 var plural_key = $.ajatus.i10n.inflector.pluralize(key);
117 var result = string.toString().replace(key, plural_key);
119 return result;
121 _resolve_replace_type: function(value) {
122 if (typeof value == 'string') {
123 return '%s';
125 if (typeof value == 'number') {
126 return '%d';
129 return '%s';
131 _load_lang: function(lang) {
132 if ( typeof lang == 'undefined'
133 || lang == '')
135 return;
138 $.ajaxSetup({
139 async: false
141 $.ajatus.events.lock_pool.increase();
142 $.getScript($.ajatus.preferences.client.application_url + 'js/languages/'+lang+'.js', function(){
143 $.ajaxSetup({
144 async: true
146 $.ajatus.i10n.initialized = true;
147 $.ajatus.events.lock_pool.decrease();
152 $.ajatus.i10n.inflections = {};
153 $.ajatus.i10n.inflector = {
154 inflection: null
156 $.extend($.ajatus.i10n.inflector, {
157 ordinalize: function(number) {
158 $.ajatus.i10n.inflector._get_inflector();
159 if ($.ajatus.i10n.inflector.inflection == false) {
160 return number;
163 if (typeof ($.ajatus.i10n.inflector.inflection['ordinalize']) == 'function') {
164 return $.ajatus.i10n.inflector.inflection.ordinalize(number);
166 return number;
168 pluralize: function(word) {
169 $.ajatus.i10n.inflector._get_inflector();
170 if ($.ajatus.i10n.inflector.inflection == false) {
171 return word;
174 var result = '';
175 $.each($.ajatus.i10n.inflector.inflection.uncountable, function(i,uncountable){
176 if (word.toLowerCase() == uncountable) {
177 result = uncountable;
180 if (result != '') {
181 return result;
183 $.each($.ajatus.i10n.inflector.inflection.irregular, function(i,irr){
184 var singular = irr[0];
185 var plural = irr[1];
186 if ((word.toLowerCase() == singular) || (word == plural)) {
187 result = plural;
190 if (result != '') {
191 return result;
193 $.each($.ajatus.i10n.inflector.inflection.plural, function(i,plur){
194 var regex = plur[0];
195 var replace_string = plur[1];
196 if (regex.test(word)) {
197 result = word.replace(regex, replace_string);
200 if (result != '') {
201 return result;
204 return word;
206 singularize: function(word) {
207 $.ajatus.i10n.inflector._get_inflector();
208 if ($.ajatus.i10n.inflector.inflection == false) {
209 return word;
212 var result = '';
214 $.each($.ajatus.i10n.inflector.inflection.uncountable, function(i,uncountable){
215 if (word.toLowerCase() == uncountable) {
216 result = uncountable;
219 if (result != '') {
220 return result;
222 $.each($.ajatus.i10n.inflector.inflection.irregular, function(i,irr){
223 var singular = irr[0];
224 var plural = irr[1];
225 if ((word.toLowerCase() == singular) || (word == plural)) {
226 result = plural;
229 if (result != '') {
230 return result;
232 $.each($.ajatus.i10n.inflector.inflection.singular, function(i,sing){
233 var regex = sing[0];
234 var replace_string = sing[1];
235 if (regex.test(word)) {
236 result = word.replace(regex, replace_string);
239 if (result != '') {
240 return result;
243 return word;
245 _get_inflector: function(lng) {
246 var lang = lng || $.ajatus.i10n.lang;
248 if ($.ajatus.i10n.inflector.inflection == null) {
249 if (typeof($.ajatus.i10n.inflections[lang]) == 'undefined') {
250 $.ajatus.i10n.inflector.inflection = false;
251 } else {
252 $.ajatus.i10n.inflector.inflection = $.ajatus.i10n.inflections[lang];
258 })(jQuery);