ci-18n 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1 @@
1
+ require File.dirname(__FILE__) + "/ci18n"
@@ -0,0 +1,17 @@
1
+ # Configure Rails 3.1 to precompile the included javascripts
2
+ module Ci18n
3
+ module Rails
4
+ class Engine < ::Rails::Engine
5
+
6
+ initializer "Ci18n.precompile_js_assets" do |app|
7
+ app.config.assets.precompile << "ci-18n.js"
8
+ Dir[::Rails.root.join("app", "assets", "javascripts", "locales", "*")].each do |locale|
9
+ locale = locale.gsub(/\.coffee/, '') # this should no be needed?
10
+ app.config.assets.precompile << File.join("locales", File.basename(locale))
11
+ end
12
+ end
13
+
14
+ end
15
+ end
16
+ end
17
+
@@ -0,0 +1,122 @@
1
+
2
+ describe "I18n as autoload capable", ->
3
+
4
+ beforeEach ->
5
+ window.$i18n = undefined
6
+ I18n.clearLanguages() if I18n.clearLanguages instanceof Function
7
+
8
+ waitLanguageLoaded = (lang, func) ->
9
+ waitsFor(1000, ->
10
+ I18n.language(lang)?
11
+ , "the language wasn't loaded")
12
+ runs(func)
13
+
14
+ it "should load some translations", ->
15
+ I18n.load("/fixtures/", "it")
16
+ waitLanguageLoaded("it", ->
17
+ expect((new I18n("it")).t("hello")).toEqual("world")
18
+ )
19
+
20
+ it "should load some translations (disambiguation)", ->
21
+ I18n.load("/fixtures/loc1", "it")
22
+ waitLanguageLoaded("it", ->
23
+ expect((new I18n("it")).t("hello")).toEqual("mondo")
24
+ )
25
+
26
+ it "should load some translations with default", ->
27
+ I18n.load("/fixtures/loc1", "it")
28
+ I18n.load("/fixtures/loc1", "en")
29
+ waitLanguageLoaded("it", ->
30
+ expect((new I18n("it", "en")).t("another")).toEqual("aaa")
31
+ )
32
+
33
+ it "should detect the language in Firefox", ->
34
+ navigator = { language: "it" }
35
+ expect(I18n.detectLanguage(navigator)).toEqual("it")
36
+
37
+ it "should detect the language in Firefox (disambiguation)", ->
38
+ navigator = { language: "en" }
39
+ expect(I18n.detectLanguage(navigator)).toEqual("en")
40
+
41
+ it "should detect the language in IE", ->
42
+ navigator = { browserLanguage: "it" }
43
+ expect(I18n.detectLanguage(navigator)).toEqual("it")
44
+
45
+ it "should detect the language in IE (disambiguation)", ->
46
+ navigator = { browserLanguage: "en" }
47
+ expect(I18n.detectLanguage(navigator)).toEqual("en")
48
+
49
+ it "should autoload a language", ->
50
+ spyOn(I18n, "detectLanguage").andReturn("en")
51
+ loadSpy = spyOn(I18n, "load")
52
+ I18n.autoloadAndSetup(path: "/fixtures")
53
+ expect(loadSpy.mostRecentCall.args).toEqual(["/fixtures", "en"])
54
+
55
+ it "should autoload a language (disambiguation)", ->
56
+ spyOn(I18n, "detectLanguage").andReturn("it")
57
+ loadSpy = spyOn(I18n, "load")
58
+ I18n.autoloadAndSetup(path: "/fixtures")
59
+ expect(loadSpy.mostRecentCall.args).toEqual(["/fixtures", "it"])
60
+
61
+ it "should autoload calling detectLanguage with window.navigator", ->
62
+ detectSpy = spyOn(I18n, "detectLanguage").andReturn("en")
63
+ I18n.autoloadAndSetup(path: "/fixtures")
64
+ expect(detectSpy.mostRecentCall.args).toEqual([window.navigator])
65
+
66
+ it "should autoload a language with a default", ->
67
+ spyOn(I18n, "detectLanguage").andReturn("en")
68
+ loadSpy = spyOn(I18n, "load")
69
+ I18n.autoloadAndSetup(path: "/fixtures", default: "it")
70
+ expect(loadSpy.argsForCall[0]).toEqual(["/fixtures", "en"])
71
+ expect(loadSpy.argsForCall[1]).toEqual(["/fixtures", "it"])
72
+
73
+ it "should autoload the lang files (disambiguation)", ->
74
+ spyOn(I18n, "detectLanguage").andReturn("it")
75
+ loadSpy = spyOn(I18n, "load")
76
+ I18n.autoloadAndSetup(path: "/fixtures", default: "en")
77
+ expect(loadSpy.argsForCall[0]).toEqual(["/fixtures", "it"])
78
+ expect(loadSpy.argsForCall[1]).toEqual(["/fixtures", "en"])
79
+
80
+ it "should autoload and setup an $i18n object based on the passed language", ->
81
+ I18n.autoloadAndSetup(path: "/fixtures/loc1", language: "it", default: "en")
82
+ waitLanguageLoaded("en", ->
83
+ expect($i18n.t("another")).toEqual("aaa")
84
+ expect($i18n.t("hello")).toEqual("mondo")
85
+ )
86
+
87
+ it "should autoload and call setup", ->
88
+ setupSpy = spyOn(I18n, "setup")
89
+ I18n.autoloadAndSetup(path: "/fixtures/loc1", language: "it", default: "en")
90
+ expect(setupSpy.mostRecentCall.args).toEqual(["it", "en"])
91
+
92
+ it "should setup a new instance", ->
93
+ I18n.load("/fixtures/loc1", "it")
94
+ I18n.load("/fixtures/loc1", "en")
95
+ waitLanguageLoaded("it", ->
96
+ I18n.setup("it", "en")
97
+ expect($i18n.t("another")).toEqual("aaa")
98
+ expect($i18n.t("hello")).toEqual("mondo")
99
+ )
100
+
101
+ it "should autosetup calling detectLanguage with window.navigator", ->
102
+ detectSpy = spyOn(I18n, "detectLanguage").andReturn("en")
103
+ I18n.autosetup()
104
+ expect(detectSpy.mostRecentCall.args).toEqual([window.navigator])
105
+
106
+ it "should autosetup calling setup", ->
107
+ detectSpy = spyOn(I18n, "detectLanguage").andReturn("en")
108
+ setupSpy = spyOn(I18n, "setup")
109
+ I18n.autosetup()
110
+ expect(setupSpy.mostRecentCall.args).toEqual(["en", undefined])
111
+
112
+ it "should autosetup calling setup with a default language", ->
113
+ detectSpy = spyOn(I18n, "detectLanguage").andReturn("en")
114
+ setupSpy = spyOn(I18n, "setup")
115
+ I18n.autosetup("it")
116
+ expect(setupSpy.mostRecentCall.args).toEqual(["en", "it"])
117
+
118
+ it "should autosetup calling setup with a default language (disambiguation)", ->
119
+ detectSpy = spyOn(I18n, "detectLanguage").andReturn("it")
120
+ setupSpy = spyOn(I18n, "setup")
121
+ I18n.autosetup("en")
122
+ expect(setupSpy.mostRecentCall.args).toEqual(["it", "en"])
@@ -0,0 +1,6 @@
1
+ beforeEach ->
2
+ this.addMatchers {
3
+ toBePlaying: (expectedSong) ->
4
+ player = this.actual
5
+ player.currentlyPlayingSong == expectedSong && player.isPlaying
6
+ }
@@ -0,0 +1,34 @@
1
+
2
+ describe "I18n as a language repository", ->
3
+
4
+ afterEach ->
5
+ I18n.clearLanguages() if I18n.clearLanguages instanceof Function
6
+
7
+ it "should add a language", ->
8
+ I18n.addLanguage("it", { hello: "world" })
9
+ expect(I18n.language("it")).toEqual(hello: "world")
10
+
11
+ it "should add and clear languages", ->
12
+ I18n.addLanguage("it", { hello: "world" })
13
+ I18n.clearLanguages()
14
+ expect(I18n.language("it")).toEqual(undefined)
15
+
16
+ it "should create a I18n object based on the specified language", ->
17
+ I18n.addLanguage("it", { hello: "world" })
18
+ instance = new I18n("it")
19
+ expect(instance.t("hello")).toEqual("world")
20
+
21
+ it "should create a I18n object with a specified default language", ->
22
+ I18n.addLanguage("it", { hello: "world" })
23
+ instance = new I18n(undefined, "it")
24
+ expect(instance.t("hello")).toEqual("world")
25
+
26
+ it "should setup a default language", ->
27
+ I18n.addLanguage("en", { hello: "world" })
28
+ I18n.setDefaultLanguage("en")
29
+ instance = new I18n()
30
+ expect(instance.t("hello")).toEqual("world")
31
+
32
+ it "should get the default language", ->
33
+ I18n.setDefaultLanguage("en")
34
+ expect(I18n.getDefaultLanguage()).toEqual("en")
@@ -0,0 +1,139 @@
1
+
2
+ describe "I18n#localize", ->
3
+
4
+ beforeEach ->
5
+ @date = new Date(2008, 2, 1)
6
+ @datetime = new Date(2008, 2, 1, 6)
7
+ #taken directly from https://github.com/svenfuchs/i18n/blob/master/lib/i18n/tests/localization/date.rb
8
+ @instance = new I18n({
9
+ date: {
10
+ formats: {
11
+ default: "%d.%m.%Y",
12
+ short: "%d. %b",
13
+ long: "%d. %B %Y",
14
+ },
15
+ day_names: ["Sonntag", "Montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag", "Samstag"],
16
+ abbr_day_names: ["So", "Mo", "Di", "Mi", "Do", "Fr", "Sa"],
17
+ month_names: ["Januar", "Februar", "März", "April", "Mai", "Juni", "Juli", "August", "September", "Oktober", "November", "Dezember", null],
18
+ abbr_month_names:["Jan", "Feb", "Mar", "Apr", "Mai", "Jun", "Jul", "Aug", "Sep", "Okt", "Nov", "Dez"]
19
+ }
20
+ time: {
21
+ formats: {
22
+ default: "%a, %d. %b %Y %H:%M:%S %z",
23
+ short: "%d. %b %H:%M",
24
+ long: "%d. %B %Y %H:%M"
25
+ },
26
+ am: 'am',
27
+ pm: 'pm'
28
+ }
29
+ })
30
+
31
+ it "should alias localize to l", ->
32
+ expect(@instance.l).toEqual(@instance.localize)
33
+
34
+ it "should raise an error if given null", ->
35
+ expect(=> @instance.l(null)).toThrow("Argument Error: null is not localizable")
36
+
37
+ it "should raise an error if given undefined", ->
38
+ expect(=> @instance.l(undefined)).toThrow("Argument Error: undefined is not localizable")
39
+
40
+ it "should raise an error if given a plain object", ->
41
+ expect(=> @instance.l({})).toThrow("Argument Error: [object Object] is not localizable")
42
+
43
+ it "should localize date: given the short format it uses it", ->
44
+ # should be Mrz, shouldn't it?
45
+ expect(@instance.l(@date, type: "date", format: "short")).toEqual("01. Mar")
46
+
47
+ it "should localize date: given the long format it uses it", ->
48
+ expect(@instance.l(@date, type: "date", format: "long")).toEqual('01. März 2008')
49
+
50
+ it "should localize date: given the default format it uses it", ->
51
+ expect(@instance.l(@date, type: "date", format: "default")).toEqual('01.03.2008')
52
+
53
+ it "should localize date: given a format is missing it raises I18n::MissingTranslationData", ->
54
+ expect(=> @instance.l(@date, type: "date", format: "missing")).toThrow("Argument Error: no such format")
55
+
56
+ it "should localize date: it does not alter the format string", ->
57
+ expect(@instance.l(new Date(2009,01, 01), type: "date", format: "long")).toEqual('01. Februar 2009')
58
+ expect(@instance.l(new Date(2009, 9, 1), type: "date", format: "long")).toEqual('01. Oktober 2009')
59
+
60
+ it "should localize datetime: given the short format it uses it", ->
61
+ # TODO should be Mrz, shouldn't it?
62
+ expect(@instance.l(@datetime, format: "short", type: "datetime")).toEqual('01. Mar 06:00')
63
+
64
+ it "should localize datetime: given the long format it uses it", ->
65
+ expect(@instance.l(@datetime, format: "long", type: "datetime")).toEqual('01. März 2008 06:00')
66
+
67
+ it "should localize datetime: given the default format it uses it", ->
68
+ spyOn(@datetime, "getTimezoneOffset").andReturn(0)
69
+ expect(@instance.l(@datetime, format: "default", type: "datetime")).toEqual('Sa, 01. Mar 2008 06:00:00 +0000')
70
+
71
+ it "should the % sign", ->
72
+ expect(@instance.l(@date, format: '%%')).toEqual('%')
73
+
74
+ it "should localize format: given a day name format it returns the correct day name", ->
75
+ expect(@instance.l(@date, format: '%A')).toEqual('Samstag')
76
+
77
+ it "should localize format: given an abbreviated day name format it returns the correct abbreviated day name", ->
78
+ expect(@instance.l(@date, format: '%a')).toEqual('Sa')
79
+
80
+ it "should localize format: given a month name format it returns the correct month name", ->
81
+ expect(@instance.l(@date, format: '%B')).toEqual('März')
82
+
83
+ it "should localize format: given an abbreviated month name format it returns the correct abbreviated month name", ->
84
+ # TODO should be Mrz, shouldn't it?
85
+ expect(@instance.l(@date, format: '%b')).toEqual('Mar')
86
+
87
+ it "should localize format: given an unknown format it does not fail", ->
88
+ expect(@instance.l(@date, format: '%x')).toEqual('%x') # really I don't know what we should return
89
+
90
+ it "should localize format: given a day name format it returns the correct day name", ->
91
+ expect(@instance.l(@datetime, format: "%A")).toEqual('Samstag')
92
+
93
+ it "should localize format: given an abbreviated day name format it returns the correct abbreviated day name", ->
94
+ expect(@instance.l(@datetime, format: "%a")).toEqual('Sa')
95
+
96
+ it "should localize format: given a month name format it returns the correct month name", ->
97
+ expect(@instance.l(@datetime, format: "%B")).toEqual('März')
98
+
99
+ it "should localize format: given an abbreviated month name format it returns the correct abbreviated month name", ->
100
+ expect(@instance.l(@datetime, format: "%b")).toEqual('Mar')
101
+
102
+ it "should localize format: given a an am date it returns the corrent meridian indicator", ->
103
+ expect(@instance.l(@datetime, format: "%p")).toEqual('am')
104
+
105
+ it "should localize format: given a a pm date it returns the corrent meridian indicator", ->
106
+ expect(@instance.l(new Date(2008, 1, 1, 20, 20), format: "%p")).toEqual('pm')
107
+
108
+ it "should localize format: given a day format it returns the correct day", ->
109
+ expect(@instance.l(@datetime, format: "%e")).toEqual('1')
110
+
111
+ it "should localize format: given an hour padded format it returns the correct hour", ->
112
+ expect(@instance.l(@datetime, format: "%I")).toEqual('06')
113
+
114
+ it "should localize format: given an hour padded format (pm) it returns the correct hour", ->
115
+ expect(@instance.l(new Date(2008, 1, 1, 20, 20), format: "%I")).toEqual('08')
116
+
117
+ it "should localize format: given a day_of_year format (first day) it returns the correct day", ->
118
+ expect(@instance.l(new Date(2008, 0, 1, 20, 20), format: "%j")).toEqual('1')
119
+
120
+ it "should localize format: given a day_of_year format (last day) it returns the correct day", ->
121
+ expect(@instance.l(new Date(2008, 11, 31, 20, 20), format: "%j")).toEqual('366')
122
+
123
+ it "should localize format: given a 24 hour format it returns the correct hour not padded", ->
124
+ expect(@instance.l(new Date(2008, 11, 31, 6, 20), format: "%k")).toEqual('6')
125
+
126
+ it "should localize format: given a 24 hour format it returns the correct hour not padded", ->
127
+ expect(@instance.l(new Date(2008, 11, 31, 20, 20), format: "%k")).toEqual('20')
128
+
129
+ it "should localize format: given a 12 hour format it returns the correct hour not padded", ->
130
+ expect(@instance.l(new Date(2008, 11, 31, 20, 20), format: "%l")).toEqual('8')
131
+
132
+ it "should localize format: given a 12 hour format it returns the correct hour not padded", ->
133
+ expect(@instance.l(new Date(2008, 11, 31, 20, 20), format: "%l")).toEqual('8')
134
+
135
+ it "should localize format: given a week day format it returns the correct week day", ->
136
+ expect(@instance.l(new Date(2008, 11, 31, 20, 20), format: "%w")).toEqual('3')
137
+
138
+ it "should localize format: given a abbr year format it returns the correct year", ->
139
+ expect(@instance.l(new Date(2008, 11, 31, 20, 20), format: "%y")).toEqual('08')
@@ -0,0 +1,74 @@
1
+
2
+ describe "I18n#translate", ->
3
+
4
+ beforeEach ->
5
+ @instance = new I18n()
6
+
7
+ it "should be able to translate a simple keyword", ->
8
+ @instance = new I18n(keyword: "hello world")
9
+ expect(@instance.translate("keyword")).toEqual("hello world")
10
+
11
+ it "should alias translate to t", ->
12
+ expect(@instance.t).toEqual(@instance.translate)
13
+
14
+ it "should be able to translate a simple keyword going to the default", ->
15
+ @instance = new I18n({}, { keyword: "hello world" })
16
+ expect(@instance.t("keyword")).toEqual("hello world")
17
+
18
+ it "should be able to set the mail locale", ->
19
+ @instance = new I18n({ keyword: "ahaha" })
20
+ expect(@instance.t("keyword")).toEqual("ahaha")
21
+
22
+ it "should be able to set the default locale", ->
23
+ @instance.defaultLocale = -> { keyword: "ahaha" }
24
+ expect(@instance.t("keyword")).toEqual("ahaha")
25
+
26
+ it "should be able to translate complex keyword", ->
27
+ @instance.locale = -> { a: { keyword: "bbbb" } }
28
+ expect(@instance.t("a.keyword")).toEqual("bbbb")
29
+
30
+ it "should be able to translate complex keywords in the default", ->
31
+ @instance.defaultLocale = -> { a: { keyword: "bbbb" } }
32
+ expect(@instance.t("a.keyword")).toEqual("bbbb")
33
+
34
+ it "should be able to translate complex keywords in the default with some common ancestor in the locale", ->
35
+ @instance.locale = -> { a: "grrr" }
36
+ @instance.defaultLocale = -> { a: { keyword: "bbbb" } }
37
+ expect(@instance.t("a.keyword")).toEqual("bbbb")
38
+
39
+ it "should be able to translate complex keywords in the default with a deep ancestor in the locale", ->
40
+ @instance.locale = -> { a: "grrr" }
41
+ @instance.defaultLocale = -> { a: { long: { keyword: "bbbb" } } }
42
+ expect(@instance.t("a.long.keyword")).toEqual("bbbb")
43
+
44
+ it "should return undefined if it doesn't find a translation", ->
45
+ expect(@instance.t("something")).toBeUndefined
46
+
47
+ it "should call I18n.normalizeKeys", ->
48
+ spyOn(I18n, "normalizeKeys").andCallThrough()
49
+ @instance.t("something")
50
+ expect(I18n.normalizeKeys).toHaveBeenCalled()
51
+
52
+ it "should be able to translate complex keyword using a scope", ->
53
+ @instance.locale = -> { a: { keyword: "bbbb" } }
54
+ expect(@instance.t("keyword", scope: "a")).toEqual("bbbb")
55
+
56
+ it "should call I18n.interpolate", ->
57
+ spyOn(I18n, "interpolate").andCallThrough()
58
+ @instance.t("something")
59
+ expect(I18n.interpolate).toHaveBeenCalled()
60
+
61
+ it "should interpolate with a scope", ->
62
+ @instance.locale = -> { a: { keyword: "%{something}" } }
63
+ expect(@instance.t("keyword", scope: "a", something: "bbbb")).toEqual("bbbb")
64
+
65
+ it "should interpolate without a scope", ->
66
+ @instance.locale = -> { a: "%{something}" }
67
+ expect(@instance.t("a", something: "bbbb")).toEqual("bbbb")
68
+
69
+ it "should allow to specify a default for the current call", ->
70
+ expect(@instance.t("a", default: "bbbb")).toEqual("bbbb")
71
+
72
+ it "should work correctly even if a default was specified", ->
73
+ @instance.locale = -> { a: "%{something}" }
74
+ expect(@instance.t("a", something: "bbbb", default: "aaaaa")).toEqual("bbbb")
@@ -0,0 +1,61 @@
1
+
2
+ describe "I18n.normalizeKeys", ->
3
+
4
+ beforeEach ->
5
+ @instance = I18n.normalizeKeys
6
+
7
+ it "should return an array containing a single value if there are no dots", ->
8
+ expect(@instance("hello")).toEqual(["hello"])
9
+
10
+ it "should extract a simple dot-separated keyword list", ->
11
+ expect(@instance("hello.world")).toEqual(["hello", "world"])
12
+
13
+ it "should extract a simple dot-separated keyword list", ->
14
+ expect(@instance("hello.world")).toEqual(["hello", "world"])
15
+
16
+ it "should extract a simple dot-separated keyword list even with options with no scope", ->
17
+ expect(@instance("hello.world", {})).toEqual(["hello", "world"])
18
+
19
+ it "should return an array if an array was passed", ->
20
+ expect(@instance(["hello", "world"])).toEqual(["hello", "world"])
21
+
22
+ it "should remove empty keywords", ->
23
+ expect(@instance("hello......world")).toEqual(["hello", "world"])
24
+
25
+ it "should accept a scope", ->
26
+ expect(@instance("hello.world", { scope: "foo.bar" })).toEqual(["foo", "bar", "hello", "world"])
27
+
28
+ describe "I18n.interpolate", ->
29
+
30
+ beforeEach ->
31
+ @instance = I18n.interpolate
32
+
33
+ it "should work with undefined", ->
34
+ expect(@instance(undefined)).toEqual(undefined)
35
+
36
+ it "should work with null", ->
37
+ expect(@instance(null)).toEqual(null)
38
+
39
+ it "should work with the empty string", ->
40
+ expect(@instance("")).toEqual("")
41
+
42
+ it "should return the passed string if there is no placeholder", ->
43
+ expect(@instance("hello world")).toEqual("hello world")
44
+
45
+ it "should return the string with the placeholder replaced", ->
46
+ expect(@instance("%{hello}", hello: "world")).toEqual("world")
47
+
48
+ it "should return the string with two placeholders replaced", ->
49
+ expect(@instance("%{a} %{b}", a: "hello", b: "world")).toEqual("hello world")
50
+
51
+ it "should interpolate the same placeholder twice", ->
52
+ expect(@instance("%{a} %{a}", a: "hello")).toEqual("hello hello")
53
+
54
+ it "should raise an exception if a passed keyword didn't match", ->
55
+ func = @instance
56
+ expect( ->
57
+ func("%{a}", a: "hello", b: "something")
58
+ ).toThrow(new Error("Missing placeholder for keyword \"b\""))
59
+
60
+ it "should interpolate named placeholders with sprintf syntax", ->
61
+ expect(@instance("%<integer>d, %<float>.1f", integer: 10, float: 42.42)).toEqual("10, 42.4")