Class: Postnhost::Generators::LocaleGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Defined in:
lib/generators/postnhost/locale/locale_generator.rb

Instance Method Summary collapse

Instance Method Details

#copy_postnhost_localeObject

Raises:

  • (Thor::Error)


10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/generators/postnhost/locale/locale_generator.rb', line 10

def copy_postnhost_locale
  code = locale.to_s.strip
  raise Thor::Error, "Locale cannot be blank." if code.blank?
  raise Thor::Error, "Invalid locale #{code.inspect} (use letters, numbers, hyphen, underscore, dot)." unless code.match?(/\A[a-zA-Z0-9_.-]+\z/)

  destination = File.join("config/locales", "#{code}.yml")
  if File.exist?(destination)
    say_status :skip, "#{destination} already exists", :yellow
    return
  end

  source_path = Postnhost::Engine.root.join("config/locales/en.yml")
  en_tree = YAML.load_file(source_path)["en"]
  raise Thor::Error, "Could not find `en` root key in #{source_path}" unless en_tree

  header = <<~TXT
    # Postnhost public UI translations for "#{code}".
    # Generated by: bin/rails g postnhost:locale #{code}
    # Translate values under postnhost.public, then add "#{code}" to config.i18n.available_locales if needed.

  TXT

  create_file destination, header + YAML.dump({ code => en_tree })
end