devise-i18n Documentation

repository·master·Indexed 20 days ago

https://github.com/tigrish/devise-i18n

An extension for the Devise authentication gem for Rails that provides internationalized views and translation files. It includes generators for customizing views and locale files, automatic locale loading via a Railtie, and specialized view helpers for handling model name case sensitivity in error messages.

Tokens
1.3K
Snippets
6
Records
8
Agent score
73%

What's inside devise-i18n

  1. Customize translations

    master

    If you need to modify the default translations provided by the gem, you can generate a specific locale file into your project using the devise:i18n:locale generator. This creates a YAML file in config/locales/ that you can edit.

    # Example: Generate Italian (it) translation file
    rails g devise:i18n:locale it
  2. Install devise-i18n

    master

    To use devise-i18n, add it to your Gemfile immediately after the devise gem.

    If you have already generated Devise views in your project, you must regenerate them after installing this gem to ensure the internationalized views are applied, then re-apply any custom styling or logic you previously had.

    gem 'devise'
    gem 'devise-i18n'
  3. Set the locale for internationalized Devise messages

    master

    To ensure Devise error messages are correctly translated, you must set the locale in your application.

    Important: Do not use I18n.with_locale inside an around_action to set the locale per request, as a known Warden bug will prevent some error messages from being translated. Instead, use a before_action in your controller or a custom middleware.

    # Option 1: Using a before_action
    before_action do
      I18n.locale = :es # Or your dynamic logic
    end
    
    # Option 2: Using Middleware
    class LocaleMiddleware
      def initialize(app)
        @app = app
      end
    
      def call(env)
        I18n.locale = :es # Or your dynamic logic
        status, headers, body = @app.call(env)
      end
    end
    
    # In config/application.rb
    config.middleware.use ::LocaleMiddleware
  4. Customize Devise views

    master

    Use the devise:i18n:views generator to copy the devise-i18n views into your application. This allows you to modify the HTML/ERB files directly.

    • If simple_form is present in your Gemfile, the generator will automatically create views using the simple_form builder.
    • Note: Generating views means you will not receive automatic updates to these files if devise-i18n updates its default views. To revert to the default gem views, simply delete the generated files from your project.
    • To support scoped views (e.g., different views for User and Admin), pass the scope name to the generator.
    # Generate default views
    rails g devise:i18n:views
    
    # Generate scoped views (e.g., for 'user')
    rails g devise:i18n:views user
  5. Automatic locale loading in Rails

    master
    The devise-i18n gem includes a Railtie that automatically loads translation files based on your application's configured available locales. It looks for YAML files in rails/locales/ that match your config.i18n.available_locales setting. If no locales are configured, it defaults to loading all files in that directory using a wildcard pattern.
  6. Handle case sensitivity for model names in error messages

    master

    The devise_i18n_fix_model_name_case helper provides custom logic to adjust the casing of strings (like model names) based on the current locale and the specific I18n key being used.

    Specifically, for the errors.messages.not_saved key, the helper will downcase the text unless the current locale is German (de), as German nouns typically require capitalization.

    # Usage within a view or helper context
    devise_i18n_fix_model_name_case(text, i18n_key: 'errors.messages.not_saved')
  7. Generate Devise views with the ViewsGenerator

    master

    Use the ViewsGenerator to copy Devise views into your Rails application. This generator allows you to specify a scope for the views and choose which specific view directories to generate.

    Arguments

    • scope (optional): The scope to copy views to. If provided, the generator will attempt to pluralize and underscore this scope for the target path and replace devise/shared references with #{plural_scope}/shared within the view files.

    Options

    • --form-builder, -b: The form builder to be used. Defaults to i18n:simple_form_for if SimpleForm is defined, otherwise i18n:form_for.
    • --views, -v: An array of specific view directories to generate. Available directories include:
      • confirmations
      • passwords
      • registrations
      • sessions
      • unlocks
      • mailer
    # Example usage (conceptual CLI invocation):
    # rails generate devise:i18n:views my_scope --views confirmations sessions
    # rails generate devise:i18n:views --form-builder simple_form_for