rails-timeago 1.3.0 → 1.4.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +11 -0
- data/lib/rails-timeago.rb +16 -0
- data/lib/rails-timeago/helper.rb +10 -3
- data/lib/rails-timeago/version.rb +1 -1
- data/spec/timeago/helper_spec.rb +20 -1
- metadata +2 -2
data/README.md
CHANGED
@@ -104,6 +104,17 @@ locale file lookup for `timeago_script_tag`.
|
|
104
104
|
To add your own locales add a file in JavaScript assets directory named
|
105
105
|
`locales/jquery.timeago.<locale>.js` and add your locale to `Rails::Timeago.locales`.
|
106
106
|
|
107
|
+
**rails-timeago** provides support for custom locale file mappings. That may be
|
108
|
+
especially useful to override the embedded `en` locale because there is no English
|
109
|
+
locale file that can be overridden:
|
110
|
+
|
111
|
+
```ruby
|
112
|
+
Rails::Timeago.map_locale "en", "better/locales/en.js"
|
113
|
+
```
|
114
|
+
|
115
|
+
Given that mapping rails-timeago's `timeago_script_tag` will include the mapped
|
116
|
+
locale file into your page.
|
117
|
+
|
107
118
|
## License
|
108
119
|
|
109
120
|
[MIT License](http://www.opensource.org/licenses/mit-license.php)
|
data/lib/rails-timeago.rb
CHANGED
@@ -83,6 +83,21 @@ module Rails
|
|
83
83
|
end
|
84
84
|
end
|
85
85
|
|
86
|
+
# Allow to map a locale to a specific url. May be useful for
|
87
|
+
# overriding embedded english locale:
|
88
|
+
#
|
89
|
+
# Rails::Timeago.map_locale :en, "better/locales/en.js"
|
90
|
+
#
|
91
|
+
def self.map_locale(locale, url)
|
92
|
+
@locale_map ||= {}
|
93
|
+
@locale_map[locale.to_s] = url
|
94
|
+
end
|
95
|
+
|
96
|
+
def self.mapped_locale(locale)
|
97
|
+
@locale_map ||= {}
|
98
|
+
@locale_map[locale.to_s]
|
99
|
+
end
|
100
|
+
|
86
101
|
def self.locale_path
|
87
102
|
File.dirname(__FILE__) + '/../vendor/assets/javascripts/locales/'
|
88
103
|
end
|
@@ -124,6 +139,7 @@ module Rails
|
|
124
139
|
|
125
140
|
def self.has_locale(locale)
|
126
141
|
return locales.include? locale if locales.any?
|
142
|
+
return true if @locale_map and @locale_map[locale.to_s]
|
127
143
|
return has_locale_file locale
|
128
144
|
end
|
129
145
|
|
data/lib/rails-timeago/helper.rb
CHANGED
@@ -56,11 +56,18 @@ module Rails
|
|
56
56
|
end
|
57
57
|
|
58
58
|
# Return a JavaScript tag to include jQuery timeago
|
59
|
-
# locale file for current or given locale.
|
59
|
+
# or custom mapped locale file for current or given locale.
|
60
60
|
def timeago_script_tag(locale = nil)
|
61
61
|
locale = ::Rails::Timeago.lookup_locale locale
|
62
|
-
|
63
|
-
|
62
|
+
|
63
|
+
file = ::Rails::Timeago.mapped_locale locale
|
64
|
+
return javascript_include_tag file if file
|
65
|
+
|
66
|
+
if locale != 'en'
|
67
|
+
javascript_include_tag 'locales/' + ::Rails::Timeago.locale_file_name(locale)
|
68
|
+
else
|
69
|
+
'' # English locale is embedded in jQuery timeago plugin file.
|
70
|
+
end
|
64
71
|
end
|
65
72
|
end
|
66
73
|
end
|
data/spec/timeago/helper_spec.rb
CHANGED
@@ -87,16 +87,35 @@ describe Rails::Timeago::Helper do
|
|
87
87
|
end
|
88
88
|
|
89
89
|
context "#timeago_script_tag" do
|
90
|
-
it "should not return anything if locale is 'en'" do
|
90
|
+
it "should not return anything if locale is 'en' and no 'en' mapping exists" do
|
91
|
+
Rails::Timeago.map_locale :en, nil
|
91
92
|
I18n.locale = "en"
|
92
93
|
@stub.timeago_script_tag.should == ""
|
93
94
|
end
|
94
95
|
|
96
|
+
it "should return mapped 'en' locale file if mapping exists" do
|
97
|
+
Rails::Timeago.map_locale :en, "better/locale/en.js"
|
98
|
+
|
99
|
+
I18n.locale = "en"
|
100
|
+
@stub.timeago_script_tag.should == '<script src="better/locale/en.js"></script>'
|
101
|
+
|
102
|
+
Rails::Timeago.map_locale :en, nil
|
103
|
+
end
|
104
|
+
|
95
105
|
it "should return javascript tag with locale file" do
|
96
106
|
I18n.locale = "de"
|
97
107
|
@stub.timeago_script_tag.should == '<script src="locales/jquery.timeago.de.js"></script>'
|
98
108
|
end
|
99
109
|
|
110
|
+
it "should return javascript tag with mapped locale file" do
|
111
|
+
Rails::Timeago.map_locale :de, "deutsch.js"
|
112
|
+
|
113
|
+
I18n.locale = "de"
|
114
|
+
@stub.timeago_script_tag.should == '<script src="deutsch.js"></script>'
|
115
|
+
|
116
|
+
Rails::Timeago.map_locale :de, nil
|
117
|
+
end
|
118
|
+
|
100
119
|
it "should return javascript tag with full locale file if available" do
|
101
120
|
I18n.locale = "zh-CN"
|
102
121
|
@stub.timeago_script_tag.should == '<script src="locales/jquery.timeago.zh-CN.js"></script>'
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rails-timeago
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.4.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-06-23 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|