Class: AwesomeTranslations::TranslationMigrator

Inherits:
Object
  • Object
show all
Defined in:
lib/awesome_translations/translation_migrator.rb

Overview

Transfers manual made translations also found in handlers over into the AwesomeTranslations namespace

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ TranslationMigrator

Returns a new instance of TranslationMigrator.



3
4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/awesome_translations/translation_migrator.rb', line 3

def initialize(args)
  require "fileutils"

  @handler_translation = args[:handler_translation]
  @translation_value = args.fetch(:translation_value)
  @translation_key = @translation_value.translation_key

  @key_parts = @translation_key.key.split(".")

  @locale = @translation_value.locale

  @old_path = @translation_value.file_path
  @new_path = "#{@handler_translation.dir}/#{@translation_value.locale}.yml" if @handler_translation
end

Instance Method Details

#executeObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/awesome_translations/translation_migrator.rb', line 18

def execute
  translations_hash = YAML.load_file(@old_path)

  new_translations_hash = YAML.load_file(@new_path) if @new_path && File.exist?(@new_path)
  new_translations_hash ||= {}
  new_translations_hash[@locale] ||= {}

  pointer = translations_hash.fetch(@locale)

  transfer_from_old_hash_to_new(
    new_translations_hash.fetch(@locale),
    @key_parts.clone,
    pointer
  )

  clean_empty_hash(translations_hash)

  if @new_path
    FileUtils.mkdir_p(File.dirname(@new_path))
    File.open(@new_path, "w") { |fp| fp.write(YAML.dump(new_translations_hash)) }
  end

  if translations_hash.empty?
    I18n.load_path.delete(@old_path)
    File.unlink(@old_path)
  else
    File.open(@old_path, "w") { |fp| fp.write(YAML.dump(translations_hash)) }
  end

  @translation_value.update!(file_path: @new_path) if @new_path
end