genki-merb_babel 0.1.1.1 → 0.1.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -5,7 +5,7 @@ require 'merb-core'
5
5
  require 'merb-core/tasks/merb'
6
6
 
7
7
  GEM_NAME = "merb_babel"
8
- GEM_VERSION = "0.1.1.1"
8
+ GEM_VERSION = "0.1.1.2"
9
9
  AUTHOR = "Matt Aimonetti"
10
10
  EMAIL = "mattaimonetti@gmail.com"
11
11
  HOMEPAGE = "http://github.com/mattetti/merb_babel/"
data/lib/merb_babel.rb CHANGED
@@ -25,18 +25,22 @@ if defined?(Merb::Plugins)
25
25
  begin
26
26
  options = args.pop if args.last.kind_of?(Hash)
27
27
  options ||= {}
28
- options.merge!(:keys => args)
29
- options.merge!(:language => language) unless options.has_key?(:language)
30
- options.merge!(:country => country) unless options.has_key?(:country)
31
- MI18n.lookup(options)
32
- #rescue
33
- # key.to_s
28
+ options[:language] ||= language
29
+ options[:country] ||= country
30
+ case key = args.last
31
+ when Date, Time
32
+ format = MI18n.lookup(options.merge(:keys => args[0..-2]))
33
+ ML10n.localize_time(key, format, options)
34
+ else
35
+ MI18n.lookup(options.merge(:keys => args))
36
+ end
37
+ rescue
38
+ key.to_s
34
39
  end
35
40
  end
36
41
  alias :translate :babelize
37
42
  alias :t :babelize
38
43
  alias :_ :babelize
39
-
40
44
  end
41
45
  end
42
46
 
@@ -101,6 +101,31 @@ module ML10n
101
101
  def localize(key, *options)
102
102
  MI18n.localize(options.merge({:locale => locale}))
103
103
  end
104
+
105
+ def localize_time(object, format, options)
106
+ table_for = proc do |table_name, key, default|
107
+ keys = ["DateFormat", table_name]
108
+ table = MI18n.lookup(options.merge(:keys => keys)) || {}
109
+ table[key] || default
110
+ end
111
+ format = format.to_s.dup
112
+ format.gsub!(/%a/) do
113
+ table_for["AbbrDayNames", object.wday, "%a"]
114
+ end
115
+ format.gsub!(/%A/) do
116
+ table_for["AbbrMonthNames", object.mon - 1, "%A"]
117
+ end
118
+ format.gsub!(/%B/) do
119
+ table_for["MonthNames", object.mon - 1, "%B"]
120
+ end
121
+ format.gsub!(/%p/) do
122
+ table_for["AmPm", object.hour < 12 ? 0 : 1, "%p"]
123
+ end if object.respond_to?(:hour)
124
+ format.gsub!(/%\{([a-zA-Z]\w*)\}/) do
125
+ object.send($1) rescue $1
126
+ end
127
+ object.strftime(format)
128
+ end
104
129
 
105
130
  protected
106
131
 
data/spec/lang/en.yml CHANGED
@@ -13,4 +13,10 @@ mloc_language_name: English
13
13
 
14
14
  right: right
15
15
  left: left
16
- greetings: Hello
16
+ greetings: Hello
17
+
18
+ night:
19
+ greetings: Good evening
20
+
21
+ DateFormat:
22
+ AbbrDayNames: ["S", "M", "T", "W", "T", "F", "S"]
data/spec/lang/ja.yml ADDED
@@ -0,0 +1,23 @@
1
+ #################################################
2
+ # Japanese localization
3
+ #
4
+ # localization specific must go in their own file
5
+ # see ja-JP.yml for instance
6
+ #################################################
7
+ mloc_language_code: ja
8
+ # Optional, the following key has the name of the localized language in its ow language.
9
+ mloc_language_name: Japanese
10
+ # mloc_country:
11
+ #################################################
12
+
13
+
14
+ right: 右
15
+ left: 左
16
+ greetings: こんにちわ
17
+
18
+ night:
19
+ greetings: こんばんわ
20
+
21
+ DateFormat:
22
+ AbbrDayNames: ["日", "月", "火", "水", "木", "金", "土"]
23
+ AmPm: ["午前", "午後"]
@@ -0,0 +1,42 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+
3
+ describe 'country detector' do
4
+ it "should detect country from request" do
5
+ c = dispatch_to(TestController, :index, {},
6
+ 'HTTP_ACCEPT_LANGUAGE' => 'en-UK')
7
+ c.country.should == 'UK'
8
+ end
9
+
10
+ it "should detect language from request" do
11
+ c = dispatch_to(TestController, :index, {},
12
+ 'HTTP_ACCEPT_LANGUAGE' => 'fr-FR')
13
+ c.language.should == 'fr'
14
+ end
15
+
16
+ it "should detect full locale from request" do
17
+ c = dispatch_to(TestController, :index, {},
18
+ 'HTTP_ACCEPT_LANGUAGE' => 'en-UK')
19
+ c.locale.should == 'en-UK'
20
+ end
21
+
22
+ it "should detect full locale from request 2" do
23
+ c = dispatch_to(TestController, :index, {},
24
+ 'HTTP_ACCEPT_LANGUAGE' => 'en_UK')
25
+ c.language.should == 'en'
26
+ c.country.should == 'UK'
27
+ c.locale.should == 'en-UK'
28
+ end
29
+
30
+ it "should guess country from language" do
31
+ c = dispatch_to(TestController, :index, {},
32
+ 'HTTP_ACCEPT_LANGUAGE' => 'ja')
33
+ c.country.should == 'JP'
34
+ c.locale.should == 'ja-JP'
35
+ end
36
+
37
+ it "should detect language from request including candidates" do
38
+ c = dispatch_to(TestController, :index, {},
39
+ 'HTTP_ACCEPT_LANGUAGE' => 'ja,en;q=0.9,fr;q=0.8,de;q=0.7,es;q=0.6')
40
+ c.locale.should == 'ja-JP'
41
+ end
42
+ end
data/spec/m_i18n_spec.rb CHANGED
@@ -50,4 +50,26 @@ describe '#babelize' do
50
50
  @c.t(:greetings, :language => 'fr').should == 'Salut'
51
51
  end
52
52
 
53
+ it "should translate with domain" do
54
+ @c.t(:night, :greetings, :language => 'en').should == 'Good evening'
55
+ @c.t(:night, :greetings, :language => 'ja').should == 'こんばんわ'
56
+ end
57
+
58
+ it "should localize date" do
59
+ date = Date.new(2009,1,1)
60
+ @c.t("%Y/%m/%d", date, :language => 'en').should == "2009/01/01"
61
+ @c.t("%Y/%{mon}/%{day}", date, :language => 'en').should == "2009/1/1"
62
+ @c.t("%a/%d", date, :language => 'en').should == "T/01"
63
+ @c.t("%a/%d", date, :language => 'en', :country => 'UK').should == "T/01"
64
+ @c.t("%a/%d", date, :language => 'ja').should == "木/01"
65
+ end
66
+
67
+ it "should localize time" do
68
+ time = Time.now
69
+ if time.hour < 12
70
+ @c.t("%p", time, :language => "ja").should == "午前"
71
+ else
72
+ @c.t("%p", time, :language => "ja").should == "午後"
73
+ end
74
+ end
53
75
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: genki-merb_babel
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1.1
4
+ version: 0.1.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matt Aimonetti
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-01-13 00:00:00 -08:00
12
+ date: 2009-01-17 00:00:00 -08:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -56,6 +56,8 @@ files:
56
56
  - spec/lang/en-UK.yml
57
57
  - spec/lang/en-US.yml
58
58
  - spec/lang/en.yml
59
+ - spec/lang/ja.yml
60
+ - spec/locale_detector_spec.rb
59
61
  - spec/m_i18n_spec.rb
60
62
  - spec/m_l10n_spec.rb
61
63
  - spec/merb_babel_spec.rb