Skip to content

Commit

Permalink
Import translations for available locales by default
Browse files Browse the repository at this point in the history
  • Loading branch information
janko committed Dec 26, 2021
1 parent a9c631b commit 883e8b8
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,15 @@ i18n_cascade? false

### Copying translations

In a Rails app, you can copy built-in translations into your app via the `rodauth:translations` generator, which receives a list of locales to copy translations for:
In a Rails app, you can copy built-in translations into your app via the `rodauth:translations` generator. By default, it will import translations for available locales, but you can also give it a list of locales:

```sh
$ rails generate rodauth:translations en hr
# create config/locales/rodauth.en.yml
# create config/locales/rodauth.hr.yml
$ rails generate rodauth:translations
# imports translations for available locales
```

On other web frameworks, you can copy the translation files directly from the `locales/` directory.
Expand Down
20 changes: 19 additions & 1 deletion lib/generators/rodauth/translations_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,31 @@ class TranslationsGenerator < ::Rails::Generators::Base
source_root File.expand_path("#{__dir__}/../../../locales")
namespace "rodauth:translations"

argument :locales, type: :array,
argument :selected_locales, type: :array, required: false,
desc: "List of locales to copy translation files for"

def copy_locales
locales.each do |locale|
copy_file("#{locale}.yml", "config/locales/rodauth.#{locale}.yml")
end

say "No locales specified!", :yellow if locales.empty?
end

private

def locales
selected_locales || available_locales
end

def available_locales
rodauths.inject([]) do |locales, rodauth|
locales |= rodauth.allocate.send(:i18n_available_locales) || []
end
end

def rodauths
Rodauth::Rails.app.opts[:rodauths].values
end
end
end
Expand Down

0 comments on commit 883e8b8

Please sign in to comment.