refinerycms-i18n 0.9.8
Sign up to get free protection for your applications and to get access to all the features.
- data/REFINERY_README +8 -0
- data/Rakefile +34 -0
- data/config/locales/ar.yml +121 -0
- data/config/locales/bg.yml +198 -0
- data/config/locales/bn-IN.yml +180 -0
- data/config/locales/bs.yml +114 -0
- data/config/locales/ca-ES.yml +155 -0
- data/config/locales/cz.rb +166 -0
- data/config/locales/da.yml +167 -0
- data/config/locales/de-AT.yml +140 -0
- data/config/locales/de.yml +141 -0
- data/config/locales/dsb.yml +182 -0
- data/config/locales/el.yml +191 -0
- data/config/locales/es-AR.yml +168 -0
- data/config/locales/es-CO.yml +146 -0
- data/config/locales/es-MX.yml +118 -0
- data/config/locales/es.yml +186 -0
- data/config/locales/et.yml +109 -0
- data/config/locales/fa.yml +119 -0
- data/config/locales/fi.yml +154 -0
- data/config/locales/fr-CH.yml +123 -0
- data/config/locales/fr.yml +140 -0
- data/config/locales/fun/en-AU.rb +105 -0
- data/config/locales/fun/gibberish.rb +109 -0
- data/config/locales/fur.yml +141 -0
- data/config/locales/gl-ES.yml +193 -0
- data/config/locales/he.yml +103 -0
- data/config/locales/hr.yml +116 -0
- data/config/locales/hsb.yml +190 -0
- data/config/locales/hu.yml +144 -0
- data/config/locales/id.yml +122 -0
- data/config/locales/is.yml +142 -0
- data/config/locales/it.yml +146 -0
- data/config/locales/ja.yml +139 -0
- data/config/locales/ko.yml +153 -0
- data/config/locales/lo.yml +201 -0
- data/config/locales/lt.yml +141 -0
- data/config/locales/lv.yml +132 -0
- data/config/locales/mk.yml +115 -0
- data/config/locales/nb.yml +103 -0
- data/config/locales/nl.yml +173 -0
- data/config/locales/nn.yml +96 -0
- data/config/locales/pl.yml +127 -0
- data/config/locales/pt-BR.yml +148 -0
- data/config/locales/pt-PT.yml +220 -0
- data/config/locales/rm.yml +134 -0
- data/config/locales/ro.yml +152 -0
- data/config/locales/ru.yml +210 -0
- data/config/locales/sk.yml +139 -0
- data/config/locales/sl.yml +190 -0
- data/config/locales/sr-Latn.yml +116 -0
- data/config/locales/sr.yml +116 -0
- data/config/locales/sv-SE.yml +191 -0
- data/config/locales/sw.yml +123 -0
- data/config/locales/th.rb +222 -0
- data/config/locales/tr.yml +139 -0
- data/config/locales/uk.yml +237 -0
- data/config/locales/vi.yml +198 -0
- data/config/locales/zh-CN.yml +131 -0
- data/config/locales/zh-TW.yml +132 -0
- data/i18n-js-readme.rdoc +249 -0
- data/lib/gemspec.rb +29 -0
- data/lib/refinery/i18n-filter.rb +38 -0
- data/lib/refinery/i18n-js.rb +132 -0
- data/lib/refinery/i18n-js.yml +4 -0
- data/lib/refinery/i18n.js +339 -0
- data/lib/refinery/i18n.rb +146 -0
- data/lib/refinery/translate.rb +13 -0
- data/lib/tasks/i18n-js_tasks.rake +16 -0
- data/lib/tasks/translate.rake +200 -0
- data/lib/translate/file.rb +35 -0
- data/lib/translate/keys.rb +123 -0
- data/lib/translate/log.rb +39 -0
- data/lib/translate/storage.rb +20 -0
- data/test/i18n-test.html +50 -0
- data/test/i18n-test.js +661 -0
- data/test/i18n_js_test.rb +168 -0
- data/test/jsunittest/jsunittest.js +1017 -0
- data/test/jsunittest/unittest.css +54 -0
- data/test/resources/custom_path.yml +4 -0
- data/test/resources/default.yml +4 -0
- data/test/resources/locales.yml +76 -0
- data/test/resources/multiple_files.yml +6 -0
- data/test/resources/no_scope.yml +3 -0
- data/test/resources/simple_scope.yml +4 -0
- data/test/test_helper.rb +24 -0
- data/translate-readme.md +61 -0
- metadata +154 -0
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'fileutils'
|
2
|
+
class Translate::Log
|
3
|
+
attr_accessor :from_locale, :to_locale, :keys
|
4
|
+
|
5
|
+
def initialize(from_locale, to_locale, keys)
|
6
|
+
self.from_locale = from_locale
|
7
|
+
self.to_locale = to_locale
|
8
|
+
self.keys = keys
|
9
|
+
end
|
10
|
+
|
11
|
+
def write_to_file
|
12
|
+
current_texts = File.exists?(file_path) ? file.read : {}
|
13
|
+
current_texts.merge!(from_texts)
|
14
|
+
file.write(current_texts)
|
15
|
+
end
|
16
|
+
|
17
|
+
def read
|
18
|
+
file.read
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
def file
|
23
|
+
@file ||= Translate::File.new(file_path)
|
24
|
+
end
|
25
|
+
|
26
|
+
def from_texts
|
27
|
+
Translate::File.deep_stringify_keys(Translate::Keys.to_deep_hash(keys.inject({}) do |hash, key|
|
28
|
+
hash[key] = I18n.backend.send(:lookup, from_locale, key)
|
29
|
+
hash
|
30
|
+
end))
|
31
|
+
end
|
32
|
+
|
33
|
+
def file_path
|
34
|
+
#make sure diff log dir exists
|
35
|
+
translate_log_dir = File.join(RAILS_ROOT, 'log', 'translate')
|
36
|
+
FileUtils.mkdir_p(translate_log_dir)
|
37
|
+
File.join(translate_log_dir, "from_#{from_locale}_to_#{to_locale}.yml")
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
class Translate::Storage
|
2
|
+
attr_accessor :locale
|
3
|
+
|
4
|
+
def initialize(locale)
|
5
|
+
self.locale = locale.to_sym
|
6
|
+
end
|
7
|
+
|
8
|
+
def write_to_file
|
9
|
+
Translate::File.new(file_path).write(keys)
|
10
|
+
end
|
11
|
+
|
12
|
+
private
|
13
|
+
def keys
|
14
|
+
{locale => I18n.backend.send(:translations)[locale]}
|
15
|
+
end
|
16
|
+
|
17
|
+
def file_path
|
18
|
+
File.join(Translate.locales_dir, "#{locale}.yml")
|
19
|
+
end
|
20
|
+
end
|
data/test/i18n-test.html
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
|
2
|
+
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
3
|
+
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
|
4
|
+
<head>
|
5
|
+
<title>JavaScript unit test file</title>
|
6
|
+
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
|
7
|
+
<script src="jsunittest/jsunittest.js" type="text/javascript"></script>
|
8
|
+
<link rel="stylesheet" href="jsunittest/unittest.css" type="text/css" />
|
9
|
+
|
10
|
+
<style type="text/css" media="screen">
|
11
|
+
#logger p {
|
12
|
+
background: #ffc;
|
13
|
+
padding: 5px;
|
14
|
+
}
|
15
|
+
</style>
|
16
|
+
<script type="text/javascript" charset="utf-8">
|
17
|
+
function log(name, message) {
|
18
|
+
var tag = document.getElementById("logger");
|
19
|
+
message = message.toString();
|
20
|
+
message = message.replace(/&/gm, "&");
|
21
|
+
message = message.replace(/</gm, "<");
|
22
|
+
message = message.replace(/>/gm, ">");
|
23
|
+
tag.innerHTML += "<p><strong>" + name + ":</strong> " + message + "</p>";
|
24
|
+
}
|
25
|
+
</script>
|
26
|
+
<script src="../lib/i18n.js" type="text/javascript"></script>
|
27
|
+
</head>
|
28
|
+
<body>
|
29
|
+
|
30
|
+
<div id="content">
|
31
|
+
<div id="header">
|
32
|
+
<h1>JavaScript unit test file</h1>
|
33
|
+
<p>
|
34
|
+
This file tests <strong>i18n.js</strong>.
|
35
|
+
</p>
|
36
|
+
</div>
|
37
|
+
|
38
|
+
<!-- Log output (one per Runner, via {testLog: "testlog"} option)-->
|
39
|
+
<div id="testlog"></div>
|
40
|
+
|
41
|
+
<!-- General debugger -->
|
42
|
+
<div id="logger"></div>
|
43
|
+
|
44
|
+
<!-- Put sample/test html here -->
|
45
|
+
<div id="sample">
|
46
|
+
</div>
|
47
|
+
</div>
|
48
|
+
<script src="i18n-test.js" type="text/javascript" charset="utf-8"></script>
|
49
|
+
</body>
|
50
|
+
</html>
|
data/test/i18n-test.js
ADDED
@@ -0,0 +1,661 @@
|
|
1
|
+
new Test.Unit.Runner({
|
2
|
+
setup: function() {
|
3
|
+
I18n.defaultLocale = "en";
|
4
|
+
I18n.locale = null;
|
5
|
+
|
6
|
+
I18n.translations = {
|
7
|
+
en: {
|
8
|
+
hello: "Hello World!",
|
9
|
+
greetings: {
|
10
|
+
stranger: "Hello stranger!",
|
11
|
+
name: "Hello %{name}!"
|
12
|
+
},
|
13
|
+
profile: {
|
14
|
+
details: "%{name} is %{age}-years old"
|
15
|
+
},
|
16
|
+
inbox: {
|
17
|
+
one: "You have %{count} message",
|
18
|
+
other: "You have %{count} messages",
|
19
|
+
zero: "You have no messages"
|
20
|
+
},
|
21
|
+
unread: {
|
22
|
+
one: "You have 1 new message (%{unread} unread)",
|
23
|
+
other: "You have %{count} new messages (%{unread} unread)",
|
24
|
+
zero: "You have no new messages (%{unread} unread)"
|
25
|
+
},
|
26
|
+
number: null
|
27
|
+
},
|
28
|
+
|
29
|
+
pt: {
|
30
|
+
hello: "Olá Mundo!",
|
31
|
+
date: {
|
32
|
+
formats: {
|
33
|
+
"default": "%d/%m/%Y",
|
34
|
+
"short": "%d de %B",
|
35
|
+
"long": "%d de %B de %Y"
|
36
|
+
},
|
37
|
+
day_names: ["Domingo", "Segunda", "Terça", "Quarta", "Quinta", "Sexta", "Sábado"],
|
38
|
+
abbr_day_names: ["Dom", "Seg", "Ter", "Qua", "Qui", "Sex", "Sáb"],
|
39
|
+
month_names: [null, "Janeiro", "Fevereiro", "Março", "Abril", "Maio", "Junho", "Julho", "Agosto", "Setembro", "Outubro", "Novembro", "Dezembro"],
|
40
|
+
abbr_month_names: [null, "Jan", "Fev", "Mar", "Abr", "Mai", "Jun", "Jul", "Ago", "Set", "Out", "Nov", "Dez"]
|
41
|
+
},
|
42
|
+
number: {
|
43
|
+
percentage: {
|
44
|
+
format: {
|
45
|
+
delimiter: "",
|
46
|
+
separator: ",",
|
47
|
+
precision: 2
|
48
|
+
}
|
49
|
+
}
|
50
|
+
},
|
51
|
+
time: {
|
52
|
+
formats: {
|
53
|
+
"default": "%A, %d de %B de %Y, %H:%M h",
|
54
|
+
"short": "%d/%m, %H:%M h",
|
55
|
+
"long": "%A, %d de %B de %Y, %H:%M h"
|
56
|
+
},
|
57
|
+
am: "AM",
|
58
|
+
pm: "PM"
|
59
|
+
}
|
60
|
+
}
|
61
|
+
}
|
62
|
+
},
|
63
|
+
|
64
|
+
teardown: function() {
|
65
|
+
},
|
66
|
+
|
67
|
+
// Defaults
|
68
|
+
testDefaults: function() { with(this) {
|
69
|
+
assertEqual("en", I18n.defaultLocale);
|
70
|
+
assertEqual(null, I18n.locale);
|
71
|
+
assertEqual("en", I18n.currentLocale());
|
72
|
+
}},
|
73
|
+
|
74
|
+
// Custom locale
|
75
|
+
testCustomLocale: function() { with(this) {
|
76
|
+
I18n.locale = "pt";
|
77
|
+
assertEqual("pt", I18n.currentLocale());
|
78
|
+
}},
|
79
|
+
|
80
|
+
// Aliases methods
|
81
|
+
testAliasesMethods: function() { with(this) {
|
82
|
+
assertEqual(I18n.translate, I18n.t);
|
83
|
+
assertEqual(I18n.localize, I18n.l);
|
84
|
+
assertEqual(I18n.pluralize, I18n.p);
|
85
|
+
}},
|
86
|
+
|
87
|
+
// Translation for single scope
|
88
|
+
testTranslationForSingleScope: function() { with(this) {
|
89
|
+
assertEqual("Hello World!", I18n.translate("hello"));
|
90
|
+
}},
|
91
|
+
|
92
|
+
// Translation as object
|
93
|
+
testTranslationAsObject: function() { with(this) {
|
94
|
+
assertEqual("object", typeof I18n.translate("greetings"));
|
95
|
+
}},
|
96
|
+
|
97
|
+
// Translation with invalid scope shall not block
|
98
|
+
testTranslationWithInvalidScope: function() { with(this) {
|
99
|
+
assertEqual('[missing "en.invalid.scope.shall.not.block" translation]', I18n.translate("invalid.scope.shall.not.block"));
|
100
|
+
}},
|
101
|
+
|
102
|
+
// Translation for single scope on a custom locale
|
103
|
+
testTranslationForSingleScopeOnACustomLocale: function() { with(this) {
|
104
|
+
I18n.locale = "pt";
|
105
|
+
assertEqual("Olá Mundo!", I18n.translate("hello"));
|
106
|
+
}},
|
107
|
+
|
108
|
+
// Translation for multiple scopes
|
109
|
+
testTranslationForMultipleScopes: function() { with(this) {
|
110
|
+
assertEqual("Hello stranger!", I18n.translate("greetings.stranger"));
|
111
|
+
}},
|
112
|
+
|
113
|
+
// Single interpolation
|
114
|
+
testSingleInterpolation: function() { with(this) {
|
115
|
+
actual = I18n.translate("greetings.name", {name: "John Doe"});
|
116
|
+
assertEqual("Hello John Doe!", actual);
|
117
|
+
}},
|
118
|
+
|
119
|
+
// Multiple interpolations
|
120
|
+
testMultipleInterpolations: function() { with(this) {
|
121
|
+
actual = I18n.translate("profile.details", {name: "John Doe", age: 27});
|
122
|
+
assertEqual("John Doe is 27-years old", actual);
|
123
|
+
}},
|
124
|
+
|
125
|
+
// Translation with count option
|
126
|
+
testTranslationWithCountOption: function() { with(this) {
|
127
|
+
assertEqual("You have 1 message", I18n.translate("inbox", {count: 1}));
|
128
|
+
assertEqual("You have 5 messages", I18n.translate("inbox", {count: 5}));
|
129
|
+
assertEqual("You have no messages", I18n.translate("inbox", {count: 0}));
|
130
|
+
}},
|
131
|
+
|
132
|
+
// Translation with count option and multiple placeholders
|
133
|
+
testTranslationWithCountOptionAndMultiplePlaceholders: function() { with(this) {
|
134
|
+
actual = I18n.translate("unread", {unread: 5, count: 1});
|
135
|
+
assertEqual("You have 1 new message (5 unread)", actual);
|
136
|
+
|
137
|
+
actual = I18n.translate("unread", {unread: 2, count: 10});
|
138
|
+
assertEqual("You have 10 new messages (2 unread)", actual);
|
139
|
+
|
140
|
+
actual = I18n.translate("unread", {unread: 5, count: 0});
|
141
|
+
assertEqual("You have no new messages (5 unread)", actual);
|
142
|
+
}},
|
143
|
+
|
144
|
+
// Missing translation with count option
|
145
|
+
testMissingTranslationWithCountOption: function() { with(this) {
|
146
|
+
actual = I18n.translate("invalid", {count: 1});
|
147
|
+
assertEqual('[missing "en.invalid" translation]', actual);
|
148
|
+
|
149
|
+
I18n.translations.en.inbox.one = null;
|
150
|
+
actual = I18n.translate("inbox", {count: 1});
|
151
|
+
assertEqual('[missing "en.inbox.one" translation]', actual);
|
152
|
+
}},
|
153
|
+
|
154
|
+
// Pluralization
|
155
|
+
testPluralization: function() { with(this) {
|
156
|
+
assertEqual("You have 1 message", I18n.pluralize(1, "inbox"));
|
157
|
+
assertEqual("You have 5 messages", I18n.pluralize(5, "inbox"));
|
158
|
+
assertEqual("You have no messages", I18n.pluralize(0, "inbox"));
|
159
|
+
}},
|
160
|
+
|
161
|
+
// Pluralize should return "other" scope
|
162
|
+
testPlurationShouldReturnOtherScope: function() { with(this) {
|
163
|
+
I18n.translations["en"]["inbox"]["zero"] = null;
|
164
|
+
assertEqual("You have 0 messages", I18n.pluralize(0, "inbox"));
|
165
|
+
}},
|
166
|
+
|
167
|
+
// Pluralize should return "zero" scope
|
168
|
+
testPlurationShouldReturnZeroScope: function() { with(this) {
|
169
|
+
I18n.translations["en"]["inbox"]["zero"] = "No messages (zero)";
|
170
|
+
I18n.translations["en"]["inbox"]["none"] = "No messages (none)";
|
171
|
+
|
172
|
+
assertEqual("No messages (zero)", I18n.pluralize(0, "inbox"));
|
173
|
+
}},
|
174
|
+
|
175
|
+
// Pluralize should return "none" scope
|
176
|
+
testPlurationShouldReturnNoneScope: function() { with(this) {
|
177
|
+
I18n.translations["en"]["inbox"]["zero"] = null;
|
178
|
+
I18n.translations["en"]["inbox"]["none"] = "No messages (none)";
|
179
|
+
|
180
|
+
assertEqual("No messages (none)", I18n.pluralize(0, "inbox"));
|
181
|
+
}},
|
182
|
+
|
183
|
+
// Pluralize with negative values
|
184
|
+
testPluralizeWithNegativeValues: function() { with(this) {
|
185
|
+
assertEqual("You have -1 message", I18n.pluralize(-1, "inbox"));
|
186
|
+
assertEqual("You have -5 messages", I18n.pluralize(-5, "inbox"));
|
187
|
+
}},
|
188
|
+
|
189
|
+
// Pluralize with multiple placeholders
|
190
|
+
testPluralizeWithMultiplePlaceholders: function() { with(this) {
|
191
|
+
actual = I18n.pluralize(1, "unread", {unread: 5});
|
192
|
+
assertEqual("You have 1 new message (5 unread)", actual);
|
193
|
+
|
194
|
+
actual = I18n.pluralize(10, "unread", {unread: 2});
|
195
|
+
assertEqual("You have 10 new messages (2 unread)", actual);
|
196
|
+
|
197
|
+
actual = I18n.pluralize(0, "unread", {unread: 5});
|
198
|
+
assertEqual("You have no new messages (5 unread)", actual);
|
199
|
+
}},
|
200
|
+
|
201
|
+
// Numbers with default settings
|
202
|
+
testNumbersWithDefaultSettings: function() { with(this) {
|
203
|
+
assertEqual("1.000", I18n.toNumber(1));
|
204
|
+
assertEqual("12.000", I18n.toNumber(12));
|
205
|
+
assertEqual("123.000", I18n.toNumber(123));
|
206
|
+
assertEqual("1,234.000", I18n.toNumber(1234));
|
207
|
+
assertEqual("123,456.000", I18n.toNumber(123456));
|
208
|
+
assertEqual("1,234,567.000", I18n.toNumber(1234567));
|
209
|
+
assertEqual("12,345,678.000", I18n.toNumber(12345678));
|
210
|
+
}},
|
211
|
+
|
212
|
+
// Numbers with partial translation and default options
|
213
|
+
testNumbersWithPartialTranslationAndDefaultOptions: function() { with(this) {
|
214
|
+
I18n.translations.en.number = {
|
215
|
+
format: {
|
216
|
+
precision: 2
|
217
|
+
}
|
218
|
+
}
|
219
|
+
|
220
|
+
assertEqual("1,234.00", I18n.toNumber(1234));
|
221
|
+
}},
|
222
|
+
|
223
|
+
// Numbers with full translation and default options
|
224
|
+
testNumbersWithFullTranslationAndDefaultOptions: function() { with(this) {
|
225
|
+
I18n.translations.en.number = {
|
226
|
+
format: {
|
227
|
+
delimiter: ".",
|
228
|
+
separator: ",",
|
229
|
+
precision: 2
|
230
|
+
}
|
231
|
+
}
|
232
|
+
|
233
|
+
assertEqual("1.234,00", I18n.toNumber(1234));
|
234
|
+
}},
|
235
|
+
|
236
|
+
// Numbers with some custom options that should be merged with default options
|
237
|
+
testNumbersWithSomeCustomOptionsThatShouldBeMergedWithDefaultOptions: function() { with(this) {
|
238
|
+
assertEqual("1,234", I18n.toNumber(1234, {precision: 0}));
|
239
|
+
assertEqual("1,234-000", I18n.toNumber(1234, {separator: "-"}));
|
240
|
+
assertEqual("1-234.000", I18n.toNumber(1234, {delimiter: "-"}));
|
241
|
+
}},
|
242
|
+
|
243
|
+
// Numbers considering options
|
244
|
+
testNumbersConsideringOptions: function() { with(this) {
|
245
|
+
options = {
|
246
|
+
precision: 2,
|
247
|
+
separator: ",",
|
248
|
+
delimiter: "."
|
249
|
+
};
|
250
|
+
|
251
|
+
assertEqual("1,00", I18n.toNumber(1, options));
|
252
|
+
assertEqual("12,00", I18n.toNumber(12, options));
|
253
|
+
assertEqual("123,00", I18n.toNumber(123, options));
|
254
|
+
assertEqual("1.234,00", I18n.toNumber(1234, options));
|
255
|
+
assertEqual("123.456,00", I18n.toNumber(123456, options));
|
256
|
+
assertEqual("1.234.567,00", I18n.toNumber(1234567, options));
|
257
|
+
assertEqual("12.345.678,00", I18n.toNumber(12345678, options));
|
258
|
+
}},
|
259
|
+
|
260
|
+
// Numbers with different precisions
|
261
|
+
testNumbersWithDifferentPrecisions: function() { with(this) {
|
262
|
+
options = {separator: ".", delimiter: ","};
|
263
|
+
|
264
|
+
options["precision"] = 2;
|
265
|
+
assertEqual("1.98", I18n.toNumber(1.98, options));
|
266
|
+
|
267
|
+
options["precision"] = 3;
|
268
|
+
assertEqual("1.980", I18n.toNumber(1.98, options));
|
269
|
+
|
270
|
+
options["precision"] = 2;
|
271
|
+
assertEqual("1.99", I18n.toNumber(1.987, options));
|
272
|
+
|
273
|
+
options["precision"] = 1;
|
274
|
+
assertEqual("2.0", I18n.toNumber(1.98, options));
|
275
|
+
|
276
|
+
options["precision"] = 0;
|
277
|
+
assertEqual("2", I18n.toNumber(1.98, options));
|
278
|
+
}},
|
279
|
+
|
280
|
+
// Currency with default settings
|
281
|
+
testCurrencyWithDefaultSettings: function() { with(this) {
|
282
|
+
assertEqual("$100.99", I18n.toCurrency(100.99));
|
283
|
+
assertEqual("$1,000.99", I18n.toCurrency(1000.99));
|
284
|
+
}},
|
285
|
+
|
286
|
+
// Current with custom settings
|
287
|
+
testCurrencyWithCustomSettings: function() { with(this) {
|
288
|
+
I18n.translations.en.number = {
|
289
|
+
currency: {
|
290
|
+
format: {
|
291
|
+
format: "%n %u",
|
292
|
+
unit: "USD",
|
293
|
+
delimiter: ".",
|
294
|
+
separator: ",",
|
295
|
+
precision: 2
|
296
|
+
}
|
297
|
+
}
|
298
|
+
};
|
299
|
+
|
300
|
+
assertEqual("12,00 USD", I18n.toCurrency(12));
|
301
|
+
assertEqual("123,00 USD", I18n.toCurrency(123));
|
302
|
+
assertEqual("1.234,56 USD", I18n.toCurrency(1234.56));
|
303
|
+
}},
|
304
|
+
|
305
|
+
// Currency with custom settings and partial overriding
|
306
|
+
testCurrencyWithCustomSettingsAndPartialOverriding: function() { with(this) {
|
307
|
+
I18n.translations.en.number = {
|
308
|
+
currency: {
|
309
|
+
format: {
|
310
|
+
format: "%n %u",
|
311
|
+
unit: "USD",
|
312
|
+
delimiter: ".",
|
313
|
+
separator: ",",
|
314
|
+
precision: 2
|
315
|
+
}
|
316
|
+
}
|
317
|
+
};
|
318
|
+
|
319
|
+
assertEqual("12 USD", I18n.toCurrency(12, {precision: 0}));
|
320
|
+
assertEqual("123,00 bucks", I18n.toCurrency(123, {unit: "bucks"}));
|
321
|
+
}},
|
322
|
+
|
323
|
+
// Currency with some custom options that should be merged with default options
|
324
|
+
testCurrencyWithSomeCustomOptionsThatShouldBeMergedWithDefaultOptions: function() { with(this) {
|
325
|
+
assertEqual("$1,234", I18n.toCurrency(1234, {precision: 0}));
|
326
|
+
assertEqual("º1,234.00", I18n.toCurrency(1234, {unit: "º"}));
|
327
|
+
assertEqual("$1,234-00", I18n.toCurrency(1234, {separator: "-"}));
|
328
|
+
assertEqual("$1-234.00", I18n.toCurrency(1234, {delimiter: "-"}));
|
329
|
+
assertEqual("$ 1,234.00", I18n.toCurrency(1234, {format: "%u %n"}));
|
330
|
+
}},
|
331
|
+
|
332
|
+
// Localize numbers
|
333
|
+
testLocalizeNumbers: function() { with(this) {
|
334
|
+
assertEqual("1,234,567.000", I18n.localize("number", 1234567));
|
335
|
+
}},
|
336
|
+
|
337
|
+
// Localize currency
|
338
|
+
testLocalizeCurrency: function() { with(this) {
|
339
|
+
assertEqual("$1,234,567.00", I18n.localize("currency", 1234567));
|
340
|
+
}},
|
341
|
+
|
342
|
+
// Parse date
|
343
|
+
testParseDate: function() { with(this) {
|
344
|
+
expected = new Date(2009, 0, 24, 0, 0, 0);
|
345
|
+
assertEqual(expected.toString(), I18n.parseDate("2009-01-24").toString());
|
346
|
+
|
347
|
+
expected = new Date(2009, 0, 24, 0, 15, 0);
|
348
|
+
assertEqual(expected.toString(), I18n.parseDate("2009-01-24 00:15:00").toString());
|
349
|
+
|
350
|
+
expected = new Date(2009, 0, 24, 0, 0, 15);
|
351
|
+
assertEqual(expected.toString(), I18n.parseDate("2009-01-24 00:00:15").toString());
|
352
|
+
|
353
|
+
expected = new Date(2009, 0, 24, 15, 33, 44);
|
354
|
+
assertEqual(expected.toString(), I18n.parseDate("2009-01-24 15:33:44").toString());
|
355
|
+
|
356
|
+
expected = new Date(2009, 0, 24, 0, 0, 0);
|
357
|
+
assertEqual(expected.toString(), I18n.parseDate(expected.getTime()).toString());
|
358
|
+
|
359
|
+
expected = new Date(2009, 0, 24, 0, 0, 0);
|
360
|
+
assertEqual(expected.toString(), I18n.parseDate("01/24/2009").toString());
|
361
|
+
|
362
|
+
expected = new Date(2009, 0, 24, 14, 33, 55);
|
363
|
+
assertEqual(expected.toString(), I18n.parseDate(expected).toString());
|
364
|
+
|
365
|
+
expected = new Date(2009, 0, 24, 15, 33, 44);
|
366
|
+
assertEqual(expected.toString(), I18n.parseDate("2009-01-24T15:33:44").toString());
|
367
|
+
|
368
|
+
expected = new Date(Date.UTC(2009, 0, 24, 15, 33, 44));
|
369
|
+
assertEqual(expected.toString(), I18n.parseDate("2009-01-24T15:33:44Z").toString());
|
370
|
+
}},
|
371
|
+
|
372
|
+
// Date formatting
|
373
|
+
testDateFormatting: function() { with(this) {
|
374
|
+
I18n.locale = "pt";
|
375
|
+
|
376
|
+
// 2009-04-26 19:35:44 (Sunday)
|
377
|
+
var date = new Date(2009, 3, 26, 19, 35, 44);
|
378
|
+
|
379
|
+
// short week day
|
380
|
+
assertEqual("Dom", I18n.strftime(date, "%a"));
|
381
|
+
|
382
|
+
// full week day
|
383
|
+
assertEqual("Domingo", I18n.strftime(date, "%A"));
|
384
|
+
|
385
|
+
// short month
|
386
|
+
assertEqual("Abr", I18n.strftime(date, "%b"));
|
387
|
+
|
388
|
+
// full month
|
389
|
+
assertEqual("Abril", I18n.strftime(date, "%B"));
|
390
|
+
|
391
|
+
// day
|
392
|
+
assertEqual("26", I18n.strftime(date, "%d"));
|
393
|
+
|
394
|
+
// 24-hour
|
395
|
+
assertEqual("19", I18n.strftime(date, "%H"));
|
396
|
+
|
397
|
+
// 12-hour
|
398
|
+
assertEqual("07", I18n.strftime(date, "%I"));
|
399
|
+
|
400
|
+
// month
|
401
|
+
assertEqual("04", I18n.strftime(date, "%m"));
|
402
|
+
|
403
|
+
// minutes
|
404
|
+
assertEqual("35", I18n.strftime(date, "%M"));
|
405
|
+
|
406
|
+
// meridian
|
407
|
+
assertEqual("PM", I18n.strftime(date, "%p"));
|
408
|
+
|
409
|
+
// seconds
|
410
|
+
assertEqual("44", I18n.strftime(date, "%S"));
|
411
|
+
|
412
|
+
// week day
|
413
|
+
assertEqual("0", I18n.strftime(date, "%w"));
|
414
|
+
|
415
|
+
// short year
|
416
|
+
assertEqual("09", I18n.strftime(date, "%y"));
|
417
|
+
|
418
|
+
// full year
|
419
|
+
assertEqual("2009", I18n.strftime(date, "%Y"));
|
420
|
+
}},
|
421
|
+
|
422
|
+
// Date formatting without padding
|
423
|
+
testDateFormattingWithoutPadding: function() { with(this) {
|
424
|
+
I18n.locale = "pt";
|
425
|
+
|
426
|
+
// 2009-04-26 19:35:44 (Sunday)
|
427
|
+
var date = new Date(2009, 3, 9, 7, 8, 9);
|
428
|
+
|
429
|
+
// 24-hour without padding
|
430
|
+
assertEqual("7", I18n.strftime(date, "%-H"));
|
431
|
+
|
432
|
+
// 12-hour without padding
|
433
|
+
assertEqual("7", I18n.strftime(date, "%-I"));
|
434
|
+
|
435
|
+
// minutes without padding
|
436
|
+
assertEqual("8", I18n.strftime(date, "%-M"));
|
437
|
+
|
438
|
+
// seconds without padding
|
439
|
+
assertEqual("9", I18n.strftime(date, "%-S"));
|
440
|
+
|
441
|
+
// short year without padding
|
442
|
+
assertEqual("9", I18n.strftime(date, "%-y"));
|
443
|
+
|
444
|
+
// month without padding
|
445
|
+
assertEqual("4", I18n.strftime(date, "%-m"));
|
446
|
+
|
447
|
+
// day without padding
|
448
|
+
assertEqual("9", I18n.strftime(date, "%-d"));
|
449
|
+
}},
|
450
|
+
|
451
|
+
// Date formatting with padding
|
452
|
+
testDateFormattingWithPadding: function() { with(this) {
|
453
|
+
I18n.locale = "pt";
|
454
|
+
|
455
|
+
// 2009-04-26 19:35:44 (Sunday)
|
456
|
+
var date = new Date(2009, 3, 9, 7, 8, 9);
|
457
|
+
|
458
|
+
// 24-hour
|
459
|
+
assertEqual("07", I18n.strftime(date, "%H"));
|
460
|
+
|
461
|
+
// 12-hour
|
462
|
+
assertEqual("07", I18n.strftime(date, "%I"));
|
463
|
+
|
464
|
+
// minutes
|
465
|
+
assertEqual("08", I18n.strftime(date, "%M"));
|
466
|
+
|
467
|
+
// seconds
|
468
|
+
assertEqual("09", I18n.strftime(date, "%S"));
|
469
|
+
|
470
|
+
// short year
|
471
|
+
assertEqual("09", I18n.strftime(date, "%y"));
|
472
|
+
|
473
|
+
// month
|
474
|
+
assertEqual("04", I18n.strftime(date, "%m"));
|
475
|
+
|
476
|
+
// day
|
477
|
+
assertEqual("09", I18n.strftime(date, "%d"));
|
478
|
+
}},
|
479
|
+
|
480
|
+
// Date formatting with negative Timezone
|
481
|
+
testDateFormattingWithNegativeTimezone: function() { with(this) {
|
482
|
+
I18n.locale = "pt";
|
483
|
+
|
484
|
+
var date = new Date(2009, 3, 26, 19, 35, 44);
|
485
|
+
|
486
|
+
date.getTimezoneOffset = function() {
|
487
|
+
return 345;
|
488
|
+
};
|
489
|
+
|
490
|
+
assertMatch(/^(\+|-)[\d]{4}$/, I18n.strftime(date, "%z"));
|
491
|
+
assertEqual("-0545", I18n.strftime(date, "%z"));
|
492
|
+
}},
|
493
|
+
|
494
|
+
// Date formatting with positive Timezone
|
495
|
+
testDateFormattingWithPositiveTimezone: function() { with(this) {
|
496
|
+
I18n.locale = "pt";
|
497
|
+
|
498
|
+
var date = new Date(2009, 3, 26, 19, 35, 44);
|
499
|
+
|
500
|
+
date.getTimezoneOffset = function() {
|
501
|
+
return -345;
|
502
|
+
};
|
503
|
+
|
504
|
+
assertMatch(/^(\+|-)[\d]{4}$/, I18n.strftime(date, "%z"));
|
505
|
+
assertEqual("+0545", I18n.strftime(date, "%z"));
|
506
|
+
}},
|
507
|
+
|
508
|
+
// Localize date strings
|
509
|
+
testLocalizeDateStrings: function() { with(this) {
|
510
|
+
I18n.locale = "pt";
|
511
|
+
|
512
|
+
assertEqual("29/11/2009", I18n.localize("date.formats.default", "2009-11-29"));
|
513
|
+
assertEqual("07 de Janeiro", I18n.localize("date.formats.short", "2009-01-07"));
|
514
|
+
assertEqual("07 de Janeiro de 2009", I18n.localize("date.formats.long", "2009-01-07"));
|
515
|
+
}},
|
516
|
+
|
517
|
+
// Localize time strings
|
518
|
+
testLocalizeTimeStrings: function() { with(this) {
|
519
|
+
I18n.locale = "pt";
|
520
|
+
assertEqual("Domingo, 29 de Novembro de 2009, 15:07 h", I18n.localize("time.formats.default", "2009-11-29 15:07:59"));
|
521
|
+
assertEqual("07/01, 09:12 h", I18n.localize("time.formats.short", "2009-01-07 09:12:35"));
|
522
|
+
assertEqual("Domingo, 29 de Novembro de 2009, 15:07 h", I18n.localize("time.formats.long", "2009-11-29 15:07:59"));
|
523
|
+
}},
|
524
|
+
|
525
|
+
// Localize percentage
|
526
|
+
testLocalizePercentage: function() { with(this) {
|
527
|
+
I18n.locale = "pt";
|
528
|
+
assertEqual("123,45%", I18n.localize("percentage", 123.45));
|
529
|
+
}},
|
530
|
+
|
531
|
+
|
532
|
+
|
533
|
+
// Default value for simple translation
|
534
|
+
testDefaultValueForSimpleTranslation: function() { with(this) {
|
535
|
+
actual = I18n.translate("warning", {defaultValue: "Warning!"});
|
536
|
+
assertEqual("Warning!", actual);
|
537
|
+
}},
|
538
|
+
|
539
|
+
// Default value with interpolation
|
540
|
+
testDefaultValueWithInterpolation: function() { with(this) {
|
541
|
+
actual = I18n.translate("alert", {defaultValue: "Attention! %{message}", message: "You're out of quota!"});
|
542
|
+
assertEqual("Attention! You're out of quota!", actual);
|
543
|
+
}},
|
544
|
+
|
545
|
+
// Default value should not be used when scope exist
|
546
|
+
testDefaultValueShouldNotBeUsedWhenScopeExist: function() { with(this) {
|
547
|
+
actual = I18n.translate("hello", {defaultValue: "What's up?"});
|
548
|
+
assertEqual("Hello World!", actual);
|
549
|
+
}},
|
550
|
+
|
551
|
+
// Default value for pluralize
|
552
|
+
testDefaultValueForPluralize: function() { with(this) {
|
553
|
+
options = {defaultValue: {
|
554
|
+
none: "No things here!",
|
555
|
+
one: "There is %{count} thing here!",
|
556
|
+
other: "There are %{count} things here!"
|
557
|
+
}};
|
558
|
+
|
559
|
+
assertEqual("No things here!", I18n.pluralize(0, "things", options));
|
560
|
+
assertEqual("There is 1 thing here!", I18n.pluralize(1, "things", options));
|
561
|
+
assertEqual("There are 5 things here!", I18n.pluralize(5, "things", options));
|
562
|
+
}},
|
563
|
+
|
564
|
+
// Default value for pluralize should not be used when scope exist
|
565
|
+
testDefaultValueForPluralizeShouldNotBeUsedWhenScopeExist: function() { with(this) {
|
566
|
+
options = {defaultValue: {
|
567
|
+
none: "No things here!",
|
568
|
+
one: "There is %{count} thing here!",
|
569
|
+
other: "There are %{count} things here!"
|
570
|
+
}};
|
571
|
+
|
572
|
+
assertEqual("You have no messages", I18n.pluralize(0, "inbox", options));
|
573
|
+
assertEqual("You have 1 message", I18n.pluralize(1, "inbox", options));
|
574
|
+
assertEqual("You have 5 messages", I18n.pluralize(5, "inbox", options));
|
575
|
+
}},
|
576
|
+
|
577
|
+
// Prepare options
|
578
|
+
testPrepareOptions: function() { with(this) {
|
579
|
+
options = I18n.prepareOptions(
|
580
|
+
{name: "Mary Doe"},
|
581
|
+
{name: "John Doe", role: "user"}
|
582
|
+
);
|
583
|
+
|
584
|
+
assertEqual("Mary Doe", options["name"]);
|
585
|
+
assertEqual("user", options["role"]);
|
586
|
+
}},
|
587
|
+
|
588
|
+
// Prepare options with multiple options
|
589
|
+
testPrepareOptionsWithMultipleOptions: function() { with(this) {
|
590
|
+
options = I18n.prepareOptions(
|
591
|
+
{name: "Mary Doe"},
|
592
|
+
{name: "John Doe", role: "user"},
|
593
|
+
{age: 33},
|
594
|
+
{email: "mary@doe.com", url: "http://marydoe.com"},
|
595
|
+
{role: "admin", email: "john@doe.com"}
|
596
|
+
);
|
597
|
+
|
598
|
+
assertEqual("Mary Doe", options["name"]);
|
599
|
+
assertEqual("user", options["role"]);
|
600
|
+
assertEqual(33, options["age"]);
|
601
|
+
assertEqual("mary@doe.com", options["email"]);
|
602
|
+
assertEqual("http://marydoe.com", options["url"]);
|
603
|
+
}},
|
604
|
+
|
605
|
+
// Prepare options should return an empty hash when values are null
|
606
|
+
testPrepareOptionsShouldReturnAnEmptyHashWhenValuesAreNull: function() { with(this) {
|
607
|
+
assertNotNullOrUndefined(I18n.prepareOptions(null, null));
|
608
|
+
}},
|
609
|
+
|
610
|
+
// Percentage with defaults
|
611
|
+
testPercentageWithDefaults: function() { with(this) {
|
612
|
+
assertEqual("1234.000%", I18n.toPercentage(1234));
|
613
|
+
}},
|
614
|
+
|
615
|
+
// Percentage with custom options
|
616
|
+
testPercentageWithCustomOptions: function() { with(this) {
|
617
|
+
assertEqual("1_234%", I18n.toPercentage(1234, {delimiter: "_", precision: 0}));
|
618
|
+
}},
|
619
|
+
|
620
|
+
// Percentage with translation
|
621
|
+
testPercentageWithTranslation: function() { with(this) {
|
622
|
+
I18n.translations.en.number = {
|
623
|
+
percentage: {
|
624
|
+
format: {
|
625
|
+
precision: 2,
|
626
|
+
delimiter: ".",
|
627
|
+
separator: ","
|
628
|
+
}
|
629
|
+
}
|
630
|
+
}
|
631
|
+
|
632
|
+
assertEqual("1.234,00%", I18n.toPercentage(1234));
|
633
|
+
}},
|
634
|
+
|
635
|
+
// Percentage with translation and custom options
|
636
|
+
testPercentageWithTranslationAndCustomOptions: function() { with(this) {
|
637
|
+
I18n.translations.en.number = {
|
638
|
+
percentage: {
|
639
|
+
format: {
|
640
|
+
precision: 2,
|
641
|
+
delimiter: ".",
|
642
|
+
separator: ","
|
643
|
+
}
|
644
|
+
}
|
645
|
+
}
|
646
|
+
|
647
|
+
assertEqual("1-234+0000%", I18n.toPercentage(1234, {precision: 4, delimiter: "-", separator: "+"}));
|
648
|
+
}},
|
649
|
+
|
650
|
+
// Scope option as string
|
651
|
+
testScopeOptionAsString: function() { with(this) {
|
652
|
+
actual = I18n.translate("stranger", {scope: "greetings"});
|
653
|
+
assertEqual("Hello stranger!", actual);
|
654
|
+
}},
|
655
|
+
|
656
|
+
// Scope as array
|
657
|
+
testScopeAsArray: function() { with(this) {
|
658
|
+
actual = I18n.translate(["greetings", "stranger"]);
|
659
|
+
assertEqual("Hello stranger!", actual);
|
660
|
+
}}
|
661
|
+
});
|