๐ŸŒInternationalization I18n

A quick guide to outline how to install and configure internationalization support in your Lightning Rails project for multi-language applications.

๐Ÿš€ Generate I18N config

If you haven't generated the I18n config on setup, you can add it to your Lightning Rails project by running this command:

Features

โœ… What's Included

  1. Multi-language Support: English, Spanish, French, German

  2. Devise Integration: Complete Devise translations for all languages

  3. Fixed Position Language Switcher: Always accessible in the bottom-right corner

  4. Session-based Locale Switching: No authentication required

  5. Comprehensive Translations: Common UI elements, navigation, forms, errors

๐ŸŽจ Language Switcher

The language switcher is positioned as a fixed floating button in the bottom-right corner of the screen:

  • Location: Fixed position, always visible

  • Style: DaisyUI primary button with shadow effects

  • Accessibility: Available on all pages without authentication

  • Animation: Smooth hover transitions

๐Ÿ”ง Technical Implementation

This is coded automatically by the generator; feel free to look at the code to understand it and modify it to your own use.

ApplicationController Integration (Created by the generator)

๐Ÿ”— URL Structure

The setup automatically creates clean, locale-prefixed URLs:

  • English: yoursite.com/en/ (default)

  • Spanish: yoursite.com/es/

  • French: yoursite.com/fr/

  • German: yoursite.com/de/

  • Language switching: yoursite.com/locale/fr (redirects to /fr/)

Automatic Redirects

  • yoursite.com/ โ†’ yoursite.com/en/

  • yoursite.com/pages โ†’ yoursite.com/en/pages

  • yoursite.com/locale/fr โ†’ yoursite.com/fr/

๐Ÿ“ Using Translations

In Views

In Controllers

In Models

๐ŸŽจ Customizing the Language Switcher

Position

The switcher is positioned with:

Styling

  • Button: btn-circle btn-primary shadow-lg

  • Dropdown: shadow-2xl bg-base-100 rounded-box

  • Hover: hover:shadow-xl transition-shadow duration-200

Customization

Edit app/views/components/_language_switcher.html.erb to:

  • Change position (e.g., top-4 instead of bottom-4)

  • Modify styling (e.g., different button colors)

  • Add animations

  • Change icon

๐ŸŒ Adding New Languages

  1. Create locale file:

  2. Create Devise locale file:

  3. Update configuration in config/application.rb:

  4. Add translations to the new locale files

๐Ÿ”ง Troubleshooting

Language switcher not showing

  • Check that the component is rendered in app/views/layouts/application.html.erb

  • Verify the component file exists at app/views/components/_language_switcher.html.erb

Translations not working

  • Ensure locale files are in config/locales/

  • Check that the locale is in config.i18n.available_locales

  • Verify the translation key exists in the locale file

Routes not working

  • Check that the locale change route is properly added

  • Ensure LocaleController exists and has skip_before_action :authenticate_user!

๐Ÿ“š Best Practices

  1. Use translation keys consistently

  2. Group related translations (e.g., pages.home.title, pages.about.title)

  3. Use interpolation for dynamic content

  4. Test all languages after adding new translations

  5. Keep translations organized in logical sections

๐Ÿš€ Advanced Features

RTL Support

For right-to-left languages, add CSS classes:

Number/Date Formatting

And voilร ! We've given you a good start, but it's now up to you to continue with the internationalization of your app. Make sure to go page by page, adding the dynamic translation tags, and don't hesitate to use AI to help you. It's worth doing this operation in a branch on a client like Cursor, as it will make this lengthy process much faster.

Happy translating! แฌ

Last updated

Was this helpful?