我正在试着根据语言的语法来翻译一些项目。我遇到了一个问题,doc提到了复数形式,每种语言的复数形式都不同。我原以为I18n
中已经有rules了,我需要做的就是填充翻译。但是当我在i18n/backend/base.rb
上读到pluralize
方法的代码时
def pluralize(locale, entry, count)
return entry unless entry.is_a?(Hash) && count
key = :zero if count == 0 && entry.has_key?(:zero)
key ||= count == 1 ? :one : :other
raise InvalidPluralizationData.new(entry, count) unless entry.has_key?(key)
entry[key]
end
我发现它只支持en
。例如,我现在正在做阿拉伯语翻译。
I18n.backend.store_translations :ar, path: { one: 'one', two: 'two', other: 'other', many: 'many', few: 'few'}
在这里,我填充了翻译后端,我希望如果我通过count
选项11,那么我将收到由语言复数规则指定的:many
翻译。
I18n.t :path, count: 11, locale: :ar #should return 'many'
我需要自己指定这些规则吗?那在哪里呢?或者我需要喝点什么?
发布于 2016-06-23 13:26:44
您可以创建自己的规则,为此,您可以创建文件: config/initializers/inflections.rb,并根据实际情况调整此示例:
ActiveSupport::Inflector.inflections(:en) do |inflect|
inflect.plural /^(ox)$/i, '\1\2en'
inflect.singular /^(ox)en/i, '\1'
inflect.irregular 'octopus', 'octopi'
inflect.uncountable 'equipment'
end
请查看文档:Inflections
发布于 2016-06-23 14:22:04
This answer解决了我的问题。我需要启用自定义复数规则的加载模式,然后为每个案例指定关键字。
https://stackoverflow.com/questions/37982563
复制相似问题