module Globalize::Automatic::TranslatedExtension
Public Instance Methods
add_automatic_translated_field_locales!(fields, locales)
click to toggle source
翻訳先フィールドと言語
# File lib/globalize/automatic.rb, line 52 def add_automatic_translated_field_locales!(fields, locales) fields.each do |field| automatic_translated_field_locales[field] = locales end end
add_automatic_translation_field_locales!(fields, locales)
click to toggle source
翻訳元フィールドと言語
# File lib/globalize/automatic.rb, line 45 def add_automatic_translation_field_locales!(fields, locales) fields.each do |field| automatic_translation_field_locales[field] = locales end end
add_automatic_translation_fields!(*fields)
click to toggle source
# File lib/globalize/automatic.rb, line 82 def add_automatic_translation_fields!(*fields) automatic_translation_class.add_fields!(*fields) end
automatic_translation_attribute_name_for(attr_name, locale)
click to toggle source
自動翻訳ON/OFF属性名
# File lib/globalize/automatic.rb, line 69 def automatic_translation_attribute_name_for(attr_name, locale) "#{attr_name}_#{locale.to_s.underscore}_automatically" end
automatic_translation_class()
click to toggle source
# File lib/globalize/automatic.rb, line 25 def automatic_translation_class return @automatic_translation_class if defined?(@automatic_translation_class) if self.const_defined?(:AutomaticTranslation, false) klass = self.const_get(AutomaticTranslation, false) else klass = Class.new(Globalize::Automatic::Translation) self.const_set(:AutomaticTranslation, klass) end foreign_key = name.foreign_key klass.belongs_to :automatic_translated_model, class_name: name, foreign_key: foreign_key, inverse_of: :automatic_translations klass.define_singleton_method(:automatic_translated_model_foreign_key) do foreign_key end @automatic_translation_class = klass end
automatic_translation_for(locale)
click to toggle source
# File lib/globalize/automatic.rb, line 88 def automatic_translation_for(locale) automatic_translation_caches[locale] ||= automatic_translations.where(locale: locale). first_or_initialize(default_translation_automatically(locale)) end
automatic_translation_locale(attr_name)
click to toggle source
自動翻訳元言語 attr_nameの自動変換が有効なとき 現在翻訳元として設定されていてかつ自動変換が無効なものの中で 一番優先度の高い翻訳元localeを返す。 どの言語も設定されてない場合は一番優先度の高いもの 自動翻訳元でない場合nil
# File lib/globalize/automatic.rb, line 116 def automatic_translation_locale(attr_name) locales = automatic_translation_field_locales[attr_name] locales.each do |locale| next if send(self.class.automatic_translation_attribute_name_for(attr_name, locale)) return locale unless translation_for(locale)[attr_name].blank? end locales.first end
create_automatic_translation_table!(*fields)
click to toggle source
# File lib/globalize/automatic.rb, line 74 def create_automatic_translation_table!(*fields) automatic_translation_class.create_table!(*fields) end
drop_automatic_translation_table!()
click to toggle source
# File lib/globalize/automatic.rb, line 78 def drop_automatic_translation_table! automatic_translation_class.drop_table! end
initialize_dup(other)
click to toggle source
Calls superclass method
# File lib/globalize/automatic.rb, line 131 def initialize_dup(other) @automatic_translation_caches = nil super(other) other.automatic_translation_attribute_names.each do |attr_name| self.attributes = { attr_name => other.send(attr_name) } end end
reload(options = nil)
click to toggle source
Calls superclass method
# File lib/globalize/automatic.rb, line 125 def reload(options = nil) automatic_translation_caches.clear automatic_translation_attribute_names.each { |attr_name| @attributes.reset(attr_name.to_s) } super(options) end
run_automatic_translation(from_locale: , attribute_names:)
click to toggle source
from_locale から attribute_names を自動翻訳 自動翻訳が対象でない場合なにもしない
# File lib/globalize/automatic.rb, line 96 def run_automatic_translation(from_locale: , attribute_names:) attribute_names.each do |attr_name| # 自動翻訳対象以外 next unless automatic_translation_field_locales[attr_name].include?(from_locale) # 自動翻訳先としては無効化されている next if automatic_translation_for(from_locale)[:"#{attr_name}_automatically"] automatic_translated_field_locales[attr_name].each do |to_locale| next if to_locale == from_locale automatic_translation_for(to_locale).translate(attr_name) end end true end
translate_field_automatically?(field, locale)
click to toggle source
field が locale で自動翻訳元指定されているか
# File lib/globalize/automatic.rb, line 59 def translate_field_automatically?(field, locale) automatic_translation_field_locales[field].include?(locale) end
translated_field_automatically?(field, locale)
click to toggle source
field が locale で自動翻訳先指定されているか
# File lib/globalize/automatic.rb, line 64 def translated_field_automatically?(field, locale) automatic_translated_field_locales[field].include?(locale) end
Private Instance Methods
automatic_translation_caches()
click to toggle source
# File lib/globalize/automatic.rb, line 159 def automatic_translation_caches @automatic_translation_caches ||= {} end
default_translation_automatically(locale)
click to toggle source
# File lib/globalize/automatic.rb, line 140 def default_translation_automatically(locale) # 自動翻訳元指定されていなくて、 # 自動翻訳先指定されているものを # デフォルトで自動翻訳ONにする # # 自動翻訳元指定されていて # 自動翻訳先指定されているものは # デフォルトで自動翻訳OFFにする # # 自動翻訳先指定されていないものは対象外 translated_attribute_names.inject({}) do |defaults, attr_name| if self.class.translated_field_automatically?(attr_name, locale) defaults[:"#{attr_name}_automatically"] = !self.class.translate_field_automatically?(attr_name, locale) end defaults end end
update_automatic_translations()
click to toggle source
# File lib/globalize/automatic.rb, line 163 def update_automatic_translations automatic_translation_caches.values.flatten.each(&:save) end