Skip to content

I18n integration and translations for Rodauth authentication framework

License

Notifications You must be signed in to change notification settings

jsantos/rodauth-i18n

 
 

Repository files navigation

rodauth-i18n

Provides I18n integration for Rodauth authentication framework. It also includes built-in translations, which you are welcome to extend.

Installation

Add this line to your application's Gemfile:

gem "rodauth-i18n"

And then execute:

$ bundle install

Or install it yourself as:

$ gem install rodauth-i18n

Usage

Enable the i18n feature in your Rodauth configuration:

plugin :rodauth do
  enable :i18n
  # ...
end

If you're using Rails, the built-in translations will be automatically loaded based on your configured available locales. Otherwise, you need to manually add them to I18n's load path before translations have been loaded:

require "rodauth/i18n"

# adds built-in locale files to I18n's load path
Rodauth::I18n.add

See the Rails Internationalization Guide on how to set the locale.

Per configuration translations

If you want to translate differently for different Rodauth configurations, you can define translations under the namespace matching the configuration name:

plugin :rodauth, name: :admin do
  enable :i18n
  # ...
end
en:
  rodauth:
    admin:
      create_account_button: Create Admin Account

You can change the translation namespace via the i18n_namespace setting:

plugin :rodauth, name: :superadmin do
  enable :i18n
  i18n_namespace "admin"
end

Any translations that are not found under the configuration namespace will fall back to top-level. If you want to disable this behaviour, you can do so via the i18n_cascade? setting:

i18n_cascade? false

Copying translations

In Rails, you can copy built-in translations into your app via the rodauth:i18n:translations generator, which receives a list of locales to copy translations for:

$ rails generate rodauth:i18n:translations en hr
# create  config/locales/rodauth.en.yml
# create  config/locales/rodauth.hr.yml

Alternatively, you can copy the translation files directly from the locales/ directory.

Raising on missing translations

You can tell I18n to raise an error when a translation is missing:

i18n_raise_on_missing_translations? { Rails.env.test? }

Falling back to untranslated value

In some cases it can be useful to fall back to untranslated value when the translation is missing:

i18n_fallback_to_untranslated? { Rails.env.production? }

Overriding current locale

The current locale defaults to I18n.locale, but you can override that:

i18n_locale :en

Custom I18n options

You can pass any custom options to the I18n.translate method via i18n_options:

i18n_options { { exception_handler: -> (*args) { ... } } }

Localized routes

If you want to prefix your routes with current locale, you can do so as follows:

prefix do
  if I18n.locale == I18n.default_locale
    "/auth"
  else
    "/#{I18n.locale}/auth"
  end
end
route do |r|
  # ...
  all_locales = I18n.available_locales.map(&:to_s) - [I18n.default_locale.to_s]
  # routes requests starting with `(/:locale)/auth/*`
  r.on [*all_locales, true], "auth" do |locale|
    rails_request.params[:locale] = locale || I18n.default_locale # if using Rails
    r.rodauth
    break
  end
end

Development

Run tests with Rake:

$ bundle exec rake test

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/janko/rodauth-i18n. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the code of conduct.

License

The gem is available as open source under the terms of the MIT License.

Code of Conduct

Everyone interacting in the rodauth-i18n project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.

About

I18n integration and translations for Rodauth authentication framework

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Ruby 100.0%