【发布时间】:2017-05-24 15:55:04
【问题描述】:
是否可以在创建主记录时保存所有查看 I18n.available_locales(或其他 Globalize 配置文件)的翻译?
我将 Globalize 与 Active Admin 结合使用,并且我为翻译创建了一个自定义页面,但我希望需要翻译的人知道哪些字段尚未翻译。
这就是我现在正在做的事情(基本模型),尽管我并不为此感到自豪。似乎无缘无故地被扭曲了,我确实尝试了更简单的解决方案,起初似乎是有效的,但结果却不起作用。
after_save :add_empty_translations
def add_empty_translations
# if the class is translatable
if (self.class.translates?)
# get available locales
locales = I18n.available_locales.map do |l| l.to_s end
# get foreign key for translated table
foreign_key = "#{self.class.to_s.underscore}_id"
# get translated columns
translated_columns = self.class::Translation.column_names.select do |col|
!['id', 'created_at', 'updated_at', 'locale', "#{self.class.to_s.underscore}_id"].include? col
end
# save current locale
current_locale = I18n.locale
# foreach available locale check if column was difined by user
locales.each do |l|
I18n.locale = l
add_translation = true
translated_columns.each do |col|
add_translation = add_translation && self[col].nil?
end
if (add_translation)
payload = {}
payload[foreign_key] = self.id
payload['locale'] = l
self.class::Translation.create(payload)
end
end
#restore locale
I18n.locale = current_locale
end
end
有没有办法通过全球化来做到这一点?
【问题讨论】: