在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):carlosantoniodasilva/i18n_alchemy开源软件地址(OpenSource Url):https://github.com/carlosantoniodasilva/i18n_alchemy开源编程语言(OpenSource Language):Ruby 98.0%开源软件介绍(OpenSource Introduction):I18nAlchemyI18n date/number parsing/localization I18nAlchemy aims to handle date, time and number parsing, based on current I18n locale format. The main idea is to have ORMs, such as ActiveRecord for now, to automatically accept dates/numbers given in the current locale format, and return these values localized as well. WhyAlmost all projects I've been working so far required some sort of input using dates and numbers, and it has always been a pain due to lack of this support in ActiveRecord itself. As I do most of my projects using different date/number formats than the English defaults (I live in Brazil), we've been adopting different ways to handle that in each application. I've already used the delocalize gem in some of these projects, and validates_timeliness parser in others, and both work pretty well actually. But I think it might work a bit different than delocalize, and mainly, I wanted to learn more about I18n and ActiveRecord internals. UsageI18nAlchemy is pretty straigthforward to use, you just need to include it in your ActiveRecord model. Lets say we are working with a Product model: class Product < ActiveRecord::Base
include I18n::Alchemy
end By mixing the module into your model, you get the localized method: @product = Product.first
@localized = @product.localized Here are some examples on how to use it with numeric values: @localized.price = "1.99"
@product.price # => 1.99
@localized.price # => "1.99"
I18n.with_locale :pt do
@localized.price = "1,88"
@product.price # => 1.88
@localized.price # => "1,88"
end Please notice that the localized proxy also formats your numeric values based on the current precision in your I18n locale file: @product.price = 1.3
@localized.price # => "1.30", considering a precision of 2 And with thousand separators as well: @product.price = 1840.32
@localized.price # => "1,840.32", considering separator = "," and delimiter = "." Some examples with date / time objects: @localized.released_at = "12/31/2011"
@product.released_at # => Date.new(2011, 12, 31)
@localized.released_at # => "12/31/2011"
I18n.with_locale :pt do
@localized.released_at = "31/12/2011"
@product.released_at # => Date.new(2011, 12, 31)
@localized.released_at # => "31/12/2011"
end The localized method quacks like ActiveRecord: you can give a hash of attributes and extra options if you want, and it will delegate everything to the object, parsing the attributes before: # This will parse the attributes in the given hash.
I18n.with_locale :pt do
@localized = @product.localized(:price => "1,88")
@product.price # => 1.88
@localized.price # => "1,88"
end Localizing methodsGiven a product model with a class Product < ActiveRecord::Base
include I18n::Alchemy
localize :total, :using => :number
def total
quantity * price
end
end
@product = Product.first
@product.price = 1.99
@product.quantity = 10
@product.total # => 19.90
@localized.total # => "19,90" If the method has a writer method, in this case With class Product
include I18n::Alchemy
localize :released_at, :using => :date
attr_accessor :released_at
end Custom ParsersIf you want to customize the way an attribute is parsed/localized, you can create a custom parser that looks like this: module MyCustomDateParser
include I18n::Alchemy::DateParser
extend self
def localize(value)
I18n.localize value, :format => :custom
end
protected
def i18n_format
I18n.t(:custom, :scope => [:date, :formats])
end
end And then just configure the attribute you want to use with this new parser: class Product < ActiveRecord::Base
include I18n::Alchemy
localize :released_month, :using => MyCustomDateParser
end By doing this, I18n::Alchemy will be set up to use your custom parser for that particular attribute, which in this case will make use of the If you are using class Product < ActiveRecord::Base
include I18n::Alchemy
localize :total, :using => MyCustomNumberParser
end I18n configurationRight now the lib uses the same configuration for numeric, date and time values from Rails: en:
date:
formats:
default: "%m/%d/%Y"
time:
formats:
default: "%m/%d/%Y %H:%M:%S"
number:
format:
separator: '.'
delimiter: ','
precision: 2 Please notice the default date and time format is considered for input values for now, and it will only accept valid values matching these formats. We plan to add specific formats and to parse a list of possible input formats for I18nAlchemy, to make it more flexible, please refer to TODO file for more info. ContactCarlos Antonio da Silva (http://github.com/carlosantoniodasilva) LicenseMIT License. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论