Class: InertiaI18n::Generators::InstallGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Defined in:
lib/generators/inertia_i18n/install_generator.rb

Instance Method Summary collapse

Instance Method Details

#add_frontend_dependenciesObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/generators/inertia_i18n/install_generator.rb', line 45

def add_frontend_dependencies
  packages_to_add = ["i18next"]

  case @framework
  when :react
    say "Detected React. Adding react-i18next...", :green
    packages_to_add << "react-i18next"
  when :vue
    say "Detected Vue. Adding i18next-vue...", :green
    packages_to_add << "i18next-vue"
  when :svelte
    say "Detected Svelte.", :green
  end

  return unless File.exist?("package.json")

  # Detect package manager
  cmd = if File.exist?("yarn.lock")
    "yarn add"
  elsif File.exist?("bun.lockb")
    "bun add"
  else
    "npm install"
  end

  run "#{cmd} #{packages_to_add.join(" ")}"
end

#check_dependenciesObject



11
12
13
14
# File 'lib/generators/inertia_i18n/install_generator.rb', line 11

def check_dependencies
  @framework = detect_framework
  @locales = detect_locales
end

#create_directory_structureObject



16
17
18
19
20
21
# File 'lib/generators/inertia_i18n/install_generator.rb', line 16

def create_directory_structure
  say "Creating locale directory structure...", :green
  empty_directory "config/locales/frontend"
  empty_directory "config/locales/backend"
  empty_directory "app/frontend/locales"
end

#create_initializerObject



41
42
43
# File 'lib/generators/inertia_i18n/install_generator.rb', line 41

def create_initializer
  template "inertia_i18n.rb.tt", "config/initializers/inertia_i18n.rb"
end

#create_sample_localesObject



34
35
36
37
38
39
# File 'lib/generators/inertia_i18n/install_generator.rb', line 34

def create_sample_locales
  (@locales || [:en]).each do |locale|
    @current_locale = locale.to_s
    template "common.locale.yml.tt", "config/locales/frontend/common.#{locale}.yml"
  end
end

#move_existing_localesObject



23
24
25
26
27
28
29
30
31
32
# File 'lib/generators/inertia_i18n/install_generator.rb', line 23

def move_existing_locales
  # Move standard Rails locales to backend if they exist in the root
  %w[en.yml ru.yml].each do |file|
    source = "config/locales/#{file}"
    if File.exist?(source)
      say "Moving #{file} to config/locales/backend/", :yellow
      rename_file source, "config/locales/backend/#{file}"
    end
  end
end

#show_post_install_infoObject



73
74
75
76
77
78
79
80
# File 'lib/generators/inertia_i18n/install_generator.rb', line 73

def show_post_install_info
  say "\n✅ InertiaI18n installed successfully!", :green
  say "\nNext steps:", :bold
  say "1. Import and initialize i18next in your frontend application (e.g., app/frontend/i18n.js)."
  say "2. Use the generated locales in config/locales/frontend/."
  say "3. Run `bundle exec inertia_i18n convert` to generate the initial JSON files."
  say "4. Start your server and happy translating!\n\n"
end