Skip to content

Commit

Permalink
CLDR!
Browse files Browse the repository at this point in the history
  • Loading branch information
Sven Fuchs committed Jan 10, 2010
1 parent f2b929a commit 860eadf
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/i18n/backend.rb
Expand Up @@ -5,6 +5,7 @@ module Backend
autoload :Cache, 'i18n/backend/cache'
autoload :Cascade, 'i18n/backend/cascade'
autoload :Chain, 'i18n/backend/chain'
autoload :Cldr, 'i18n/backend/cldr'
autoload :Fallbacks, 'i18n/backend/fallbacks'
autoload :Fast, 'i18n/backend/fast'
autoload :Gettext, 'i18n/backend/gettext'
Expand Down
34 changes: 34 additions & 0 deletions lib/i18n/backend/cldr.rb
@@ -0,0 +1,34 @@
# encoding: utf-8
$:.unshift '/Volumes/Users/sven/Development/projects/i18n/cldr/lib'

This comment has been minimized.

Copy link
@yaroslav

yaroslav Jan 10, 2010

Collaborator

whoops.

require 'cldr'

module I18n
module Backend
module Cldr
include ::Cldr::Format

def localize(locale, object, format = :default, options = {})
case object
when ::Numeric
format(locale, object, { :as => :number }.merge(options))
else
super
end
end

protected

def lookup_number_format(locale, type, format)
I18n.t(:"numbers.formats.#{type}.#{format || :default}.pattern", :locale => locale)
end

def lookup_number_symbols(locale)
I18n.t(:'numbers.symbols', :locale => locale)
end

def lookup_currency(locale, currency, count)
I18n.t(:"currencies.#{currency}", :locale => locale, :count => count)
end
end
end
end
42 changes: 42 additions & 0 deletions test/cases/backend/cldr_test.rb
@@ -0,0 +1,42 @@
# encoding: utf-8

require File.expand_path(File.dirname(__FILE__) + '/../../test_helper')
require 'i18n/backend/cldr'

class I18nBackendCldrTest < Test::Unit::TestCase
class Backend
include I18n::Backend::Base
include I18n::Backend::Cldr
end

def setup
I18n.backend = Backend.new
I18n.locale = :de
I18n.load_path += Dir[locales_dir + '/cldr/**/*.{yml,rb}']
super
end

define_method :"test: format_number" do
assert_equal '123.456,78', I18n.l(123456.78)
end

define_method :"test: format_currency" do
assert_equal '123.456,78 EUR', I18n.l(123456.78, :currency => 'EUR')
end

# hu? does this actually make any sense?
define_method :"test: format_currency translating currency names" do
assert_equal '1,00 Irisches Pfund', I18n.l(1, :currency => :IEP)
assert_equal '2,00 Irische Pfund', I18n.l(2, :currency => :IEP)
end

# this is odd but the cldr percent format does not include a fraction
define_method :"test: format_percent" do
assert_equal '123.457 %', I18n.l(123456.78, :as => :percent)
end

# so we can pass a precision manually
define_method :"test: format_percent w/ precision" do
assert_equal '123.456,70 %', I18n.l(123456.7, :as => :percent, :precision => 2)
end
end
8 changes: 8 additions & 0 deletions test/locales/cldr/de/currencies.yml
@@ -0,0 +1,8 @@
de:
currencies:
EUR:
one: Euro
other: Euro
IEP:
one: "Irisches Pfund"
other: "Irische Pfund"
31 changes: 31 additions & 0 deletions test/locales/cldr/de/numbers.yml
@@ -0,0 +1,31 @@
de:
numbers:
formats:
currency:
default:
pattern: "#,##0.00 ¤"
unit:
one: "{0} {1}"
other: "{0} {1}"
decimal:
default:
pattern: "#,##0.###"
percent:
default:
pattern: "#,##0 %"
scientific:
default:
pattern: "#E0"
symbols:
decimal: ","
exponential: E
group: "."
infinity:
list: ;
minus_sign: "-"
nan: NaN
native_zero_digit: 0
pattern_digit: "#"
per_mille:
percent_sign: "%"
plus_sign: +

7 comments on commit 860eadf

@svenfuchs
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

whoops?

@roidrage
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

personal project load path whoops.

@yaroslav
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Look at the line, does not look like it belogs to library master :)

@svenfuchs
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WHOOPS :)

@tomash
Copy link
Contributor

@tomash tomash commented on 860eadf Jan 10, 2010

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Plus it just came out you're using Mac. I couldn't decide which one is more embarassing ;)

@yaroslav
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, not using Microsoft Windows is so immature and unprofessional of y'all.

@tomash
Copy link
Contributor

@tomash tomash commented on 860eadf Jan 11, 2010

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Damn, I almost trolled successfully ;)

Please sign in to comment.