lib/rosetta/store.rb in rosetta-rails-0.1.1 vs lib/rosetta/store.rb in rosetta-rails-0.2.0

- old
+ new

@@ -11,23 +11,20 @@ end def initialize(locale) @locale = locale @cache_expiration_timestamp = @locale.updated_at - @autodiscovery_queue = Queue.new - - start_key_autodiscovery! end - def lookup(key_value) - if translations.has_key?(key_value) - translations[key_value] + def lookup(content) + if translations.has_key?(content) + translations[content] else - @autodiscovery_queue << key_value + TextEntry.create_later(content) # Set the key in the translations store to locate it # once only. - translations[key_value] = nil + translations[content] = nil end end def reload! @translations = nil @@ -45,31 +42,16 @@ end private def load_translations - loaded_translations = Rosetta.with_locale(@locale) do - TranslationKey - .includes(:translation_in_current_locale) - .map do |translation_key| - [ translation_key.value, translation_key.translation_in_current_locale&.value ] - end.to_h - end + loaded_translations = TextEntry + .with_translated_version(@locale) + .where(locale: Rosetta::Locale.default_locale) + .map do |text_entry| + [ text_entry.content, text_entry.public_send(:"#{@locale.code}_translated_version")&.content ] + end.to_h Concurrent::Hash.new.merge(loaded_translations) - end - - def start_key_autodiscovery! - Thread.new do - Thread.current.name = "Rosetta #{locale.code} store thread" - - loop do - key_value = @autodiscovery_queue.pop - - unless TranslationKey.exists?(value: key_value) - TranslationKey.create!(value: key_value) - end - end - end end end end