Luckily, this is more of a data problem than a programming problem. Globalize provides
data to help with all of these issues. The date formatting helpers, Time#localize
and Date#localize, serve as a replacement for Time#strftime that translates day and
month names (both full and abbreviated):
>> Time.now.strftime("%A, %d %B %Y")
=> "Saturday, 02 June 2007"
>> Locale.set 'en-US'
>> Time.now.localize("%A, %d %B %Y")
=> "Saturday, 02 June 2007"
>> Locale.set 'pl-PL'
>> Time.now.localize("%A, %d %B %Y")
=> "Sobota, 02 Czerwiec 2007"
The localize method is also available on Integers and Floats, and provides numbers
localized to the current locale:
>> Locale.set 'en-US'
>> 123456.789.localize
=> "123,456.789"
>> Locale.set 'de-DE'
>> 123456.789.localize
=> "123.456,789"
Globalize also provides a Currency class to handle money. It acts like Tobias L??tke??™s
Money class, in that it stores prices as integers in the database. But it is more flexible
in handling multiple currencies. Refer to the API documentation for the full story,
but here is a sample usage:
# app/models/product.rb
class Product < ActiveRecord::Base
composed_of :price, :class_name => 'Globalize::Currency',
:mapping => [%w(price cents)]
end
Then we can create a product:
@product = Product.new :price => Currency.new(1000_00)
puts @product.price.to_s
# >> 1,000.00
The Currency#format method formats the currency according to the specifications of
the current locale (which, as usual, is set with the Locale.
Pages:
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400