Skip to content

Commit

Permalink
Add methods code, title and ltr? to Locale
Browse files Browse the repository at this point in the history
  • Loading branch information
ai committed Sep 10, 2009
1 parent 4e08636 commit 2c56294
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 25 deletions.
8 changes: 4 additions & 4 deletions r18n-core/README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -216,14 +216,14 @@ To get information about locale create R18n::Locale instance:
You can get from locale:
* Locale title and RFC 3066 code:

locale['title'] #=> "English"
locale['code'] #=> "en"
locale.title #=> "English"
locale.code #=> "en"

* Language direction (left to right, or right to left for Arabic and Hebrew):

locale['direction'] #=> "ltr"
locale.ltr? #=> true

* Week start day ("sunday" or "monday")
* Week start day ("sunday" or "monday"):

locale['week']['start'] #=> "sunday"

Expand Down
8 changes: 4 additions & 4 deletions r18n-core/lib/r18n-core/i18n.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ module R18n
# i18n.one #=> "Один"
# i18n.two #=> "Two"
#
# i18n.locale['title'] #=> "Русский"
# i18n.locale['code'] #=> "ru"
# i18n.locale['direction'] #=> "ltr"
# i18n.locale.title #=> "Русский"
# i18n.locale.code #=> "ru"
# i18n.locale.ltr? #=> true
#
# i18n.l -11000.5 #=> "−11 000,5"
# i18n.l Time.now #=> "Вск, 21 сен 2008, 22:10:10 MSD"
Expand Down Expand Up @@ -142,7 +142,7 @@ def initialize(locales, translation_dirs = nil)
# translations.
def translations
Translation.available(@translation_dirs).inject({}) do |all, code|
all[code] = Locale.load(code)['title']
all[code] = Locale.load(code).title
all
end
end
Expand Down
25 changes: 20 additions & 5 deletions r18n-core/lib/r18n-core/locale.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ module R18n
# Get Russian locale and print it information
#
# ru = R18n::Locale.load('ru')
# ru['title'] #=> "Русский"
# ru['code'] #=> "ru"
# ru['direction'] #=> "ltr"
# ru.title #=> "Русский"
# ru.code #=> "ru"
# ru.ltr? #=> true
#
# == Available data
#
Expand Down Expand Up @@ -121,6 +121,21 @@ def self.load(code)
def initialize(data)
@data = data
end

# Locale RFC 3066 code.
def code
@data['code']
end

# Locale title.
def title
@data['title']
end

# Is locale has left-to-right write direction.
def ltr?
@data['direction'] == 'ltr'
end

# Get information about locale
def [](name)
Expand All @@ -129,7 +144,7 @@ def [](name)

# Is another locale has same code
def ==(locale)
@data['code'] == locale['code']
code == locale.code
end

# Is locale has information file. In this class always return true.
Expand All @@ -139,7 +154,7 @@ def supported?

# Human readable locale code and title
def inspect
"Locale #{@data['code']} (#{@data['title']})"
"Locale #{code} (#{title})"
end

# Returns the integer in String form, according to the rules of the locale.
Expand Down
4 changes: 2 additions & 2 deletions r18n-core/lib/r18n-core/translated.rb
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ def #{name}(*params)
unlocalized = self.class.unlocalized_getters(#{name.inspect})
R18n.get.locales.each do |locale|
code = locale['code']
code = locale.code
next unless unlocalized.has_key? code
result = method(unlocalized[code]).#{call}
next unless result
Expand All @@ -145,7 +145,7 @@ def #{name}(*params)
def #{name}=(*params)
unlocalized = self.class.unlocalized_setters(#{name.inspect})
R18n.get.locales.each do |locale|
code = locale['code']
code = locale.code
next unless unlocalized.has_key? code
return method(unlocalized[code]).call(*params)
end
Expand Down
4 changes: 2 additions & 2 deletions r18n-core/lib/r18n-core/translation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ module R18n
# i18n.one #=> "Один"
# i18n.two #=> "Two"
#
# i18n.two.locale['code'] #=> "en"
# i18n.two.locale['direction'] #=> "ltr"
# i18n.two.locale.code #=> "en"
# i18n.two.locale.ltr? #=> "ltr"
#
# i18n.entry.between(2, 3) #=> "between 2 and 3"
# i18n['methods', 'object'] #=> "Is object method"
Expand Down
16 changes: 13 additions & 3 deletions r18n-core/lib/r18n-core/unsupported_locale.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ def supported?
false
end

# Human readable locale code and title
# Human readable locale code and title.
def inspect
"Unsupported locale #{@code}"
end

# Get information about locale
# Get information about locale.
def [](name)
case name
when 'code'
Expand All @@ -53,10 +53,20 @@ def [](name)
@base[name]
end
end

# Locale RFC 3066 code.
def code
@code
end

# Locale code as title.
def title
@code
end

# Is another locale has same code
def ==(locale)
@code == locale['code']
@code == locale.code
end

# Proxy to default locale object.
Expand Down
15 changes: 11 additions & 4 deletions r18n-core/spec/locale_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@

it "should load locale" do
locale = R18n::Locale.load('ru')
locale['code'].should == 'ru'
locale['title'].should == 'Русский'
locale.code.should == 'ru'
locale.title.should == 'Русский'
locale.ltr?.should be_true
end

it "should load locale by Symbol" do
Expand All @@ -29,8 +30,8 @@
it "should include locale by +include+ option" do
en_US = R18n::Locale.load('en_US')
en = R18n::Locale.load('en')
en_US['title'].should == 'English (US)'
en['title'].should == 'English'
en_US.title.should == 'English (US)'
en.title.should == 'English'
en_US['week'].should == en['week']
end

Expand Down Expand Up @@ -65,9 +66,15 @@
unsupported = R18n::Locale.load('no_LC')
unsupported.should_not be_supported
unsupported.should be_a(R18n::UnsupportedLocale)

unsupported.code.should == 'no_LC'
unsupported.title.should == 'no_LC'
unsupported.ltr?.should be_true

unsupported['code'].should == 'no_LC'
unsupported['title'].should == 'no_LC'
unsupported['direction'].should == 'ltr'

unsupported.pluralize(5).should == 'n'
unsupported.inspect.should == 'Unsupported locale no_LC'
end
Expand Down
2 changes: 1 addition & 1 deletion sinatra-r18n/spec/app/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
end

get '/locale' do
i18n.locale['title']
i18n.locale.title
end

get '/locales' do
Expand Down

0 comments on commit 2c56294

Please sign in to comment.