Mercurial > babel > old > babel-test
annotate contrib/babel.js @ 363:a03ce004eff1
Added documentation to format method in babel.js
author | aronacher |
---|---|
date | Wed, 18 Jun 2008 12:39:01 +0000 |
parents | 9cd9d787dcb2 |
children | 85e1beadacb0 |
rev | line source |
---|---|
362
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
1 /** |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
2 * Babel JavaScript Support |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
3 * |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
4 * Copyright (C) 2008 Edgewall Software |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
5 * All rights reserved. |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
6 * |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
7 * This software is licensed as described in the file COPYING, which |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
8 * you should have received as part of this distribution. The terms |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
9 * are also available at http://babel.edgewall.org/wiki/License. |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
10 * |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
11 * This software consists of voluntary contributions made by many |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
12 * individuals. For the exact contribution history, see the revision |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
13 * history and logs, available at http://babel.edgewall.org/log/. |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
14 */ |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
15 |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
16 /** |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
17 * A simple module that provides a gettext like translation interface. |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
18 * The catalog passed to load() must be a object conforming to this |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
19 * interface:: |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
20 * |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
21 * { |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
22 * messages: an object of {msgid: translations} items where |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
23 * translations is an array of messages or a single |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
24 * string if the message is not pluralizable. |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
25 * plural_expr: the plural expression for the language. |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
26 * locale: the identifier for this locale. |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
27 * domain: the name of the domain. |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
28 * } |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
29 * |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
30 * Missing elements in the object are ignored. |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
31 * |
363 | 32 * Typical usage:: |
362
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
33 * |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
34 * var translations = babel.Translations.load(...).install(); |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
35 */ |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
36 var babel = new function() { |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
37 |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
38 var defaultPluralExpr = function(n) { return n == 1 ? 0 : 1; }; |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
39 var formatRegex = /%?%(?:\(([^\)]+)\))?([disr])/g; |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
40 |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
41 /** |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
42 * A translations object implementing the gettext interface |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
43 */ |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
44 var Translations = this.Translations = function(locale, domain) { |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
45 this.messages = {}; |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
46 this.locale = locale || 'unknown'; |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
47 this.domain = domain || 'messages'; |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
48 this.pluralexpr = defaultPluralExpr; |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
49 }; |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
50 |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
51 /** |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
52 * Create a new translations object from the catalog and return it. |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
53 * See the babel-module comment for more details. |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
54 */ |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
55 Translations.load = function(catalog) { |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
56 var rv = new Translations(); |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
57 rv.load(catalog); |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
58 return rv; |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
59 }; |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
60 |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
61 Translations.prototype = { |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
62 /** |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
63 * translate a single string. |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
64 */ |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
65 gettext: function(string) { |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
66 var translated = this.messages[string]; |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
67 if (typeof translated == 'undefined') |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
68 return string; |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
69 return (typeof translated == 'string') ? translated : translated[0]; |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
70 }, |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
71 |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
72 /** |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
73 * translate a pluralizable string |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
74 */ |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
75 ngettext: function(singular, plural, n) { |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
76 var translated = this.messages[singular]; |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
77 if (typeof translated == 'undefined') |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
78 return (n == 1) ? singular : plural; |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
79 return translated[this.pluralexpr(n)]; |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
80 }, |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
81 |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
82 /** |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
83 * Install this translation document wide. After this call, there are |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
84 * three new methods on the window object: _, gettext and ngettext |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
85 */ |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
86 install: function() { |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
87 var self = this; |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
88 window._ = window.gettext = function(string) { |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
89 return self.gettext(string); |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
90 }; |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
91 window.ngettext = function(singular, plural, n) { |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
92 return self.ngettext(singular, plural, n); |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
93 }; |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
94 return this; |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
95 }, |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
96 |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
97 /** |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
98 * Works like Translations.load but updates the instance rather |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
99 * then creating a new one. |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
100 */ |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
101 load: function(catalog) { |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
102 if (catalog.messages) |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
103 this.update(catalog.messages) |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
104 if (catalog.plural_expr) |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
105 this.setPluralExpr(catalog.plural_expr); |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
106 if (catalog.locale) |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
107 this.locale = catalog.locale; |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
108 if (catalog.domain) |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
109 this.domain = catalog.domain; |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
110 return this; |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
111 }, |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
112 |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
113 /** |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
114 * Updates the translations with the object of messages. |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
115 */ |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
116 update: function(mapping) { |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
117 for (var key in mapping) |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
118 if (mapping.hasOwnProperty(key)) |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
119 this.messages[key] = mapping[key]; |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
120 return this; |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
121 }, |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
122 |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
123 /** |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
124 * Sets the plural expression |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
125 */ |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
126 setPluralExpr: function(expr) { |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
127 this.pluralexpr = new Function('n', 'return +(' + expr + ')'); |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
128 return this; |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
129 } |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
130 }; |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
131 |
363 | 132 /** |
133 * A python inspired string formatting function. Supports named and | |
134 * positional placeholders and "s", "d" and "i" as type characters | |
135 * without any formatting specifications. | |
136 * | |
137 * Examples:: | |
138 * | |
139 * babel.format(_('Hello %s'), name) | |
140 * babel.format(_('Progress: %(percent)s%%'), {percent: 100}) | |
141 */ | |
362
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
142 this.format = function() { |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
143 var arg, string = arguments[0], idx = 0; |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
144 if (arguments.length == 1) |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
145 return string; |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
146 else if (arguments.length == 2 && typeof arguments[1] == 'object') |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
147 arg = arguments[1]; |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
148 else { |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
149 arg = []; |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
150 for (var i = 1, n = arguments.length; i != n; ++i) |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
151 arg[i - 1] = arguments[i]; |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
152 } |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
153 return string.replace(formatRegex, function(all, name, type) { |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
154 if (all[0] == all[1]) return all.substring(1); |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
155 var value = arg[name || idx++]; |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
156 return (type == 'i' || type == 'd') ? +value : value; |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
157 }); |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
158 } |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
159 |
9cd9d787dcb2
Added contrib folder with a JavaScript support file for babel.
aronacher
parents:
diff
changeset
|
160 }; |