Ruby on Rails

AI translation for Ruby on Rails with Lingo.dev CLI

What is Ruby on Rails?

Ruby on Rails is a server-side web application framework written in Ruby. It provides default structures for databases, web services, and web pages, following the model-view-controller (MVC) pattern.

What is Lingo.dev CLI?

Lingo.dev is an AI-powered translation platform. The Lingo.dev CLI reads source files, sends translatable content to large language models, and writes translated files back to your project.

About this guide

This guide explains how to set up Lingo.dev CLI in a Ruby on Rails application. You'll learn how to scaffold a project with Rails, configure a translation pipeline, and view the results.

Step 1. Set up a Rails project

  1. Create a Rails application:

    rails new rails-demo
    
  2. Navigate into the project directory:

    cd rails-demo
    
  3. Generate a controller with a view:

    bin/rails generate controller Home index
    

Step 2. Create source content

  1. Open the English locale file at config/locales/en.yml.

  2. Add some translatable content:

    en:
      home:
        title: "Welcome"
        greeting: "Hello from Rails + Lingo.dev"
    

Step 3. Configure the CLI

In the root of the project, create an i18n.json file:

{
  "$schema": "https://lingo.dev/schema/i18n.json",
  "version": 1.8,
  "locale": {
    "source": "en",
    "targets": ["es"]
  },
  "buckets": {
    "yaml-root-key": {
      "include": ["config/locales/[locale].yml"]
    }
  }
}

This file defines:

  • the files that Lingo.dev CLI should translate
  • the languages to translate between

In this case, the configuration translates YAML files from English to Spanish.

[locale] is a placeholder that's replaced at runtime. It ensures that content is read from one location (e.g., config/locales/en.yml) and written to a different location (e.g., config/locales/es.yml).

To learn more, see i18n.json configuration.

Step 4. Translate the content

  1. Sign up for a Lingo.dev account.

  2. Log in to Lingo.dev via the CLI:

    npx lingo.dev@latest login
    
  3. Run the translation pipeline:

    npx lingo.dev@latest run
    

    The CLI will create a config/locales/es.yml file for storing the translated content and an i18n.lock file for keeping track of what has been translated (to prevent unnecessary retranslations).

Step 5. Use the translations

  1. Configure locale-based routes in config/routes.rb:

    Rails.application.routes.draw do
      scope "(:locale)", locale: /en|es/ do
        root "home#index"
      end
    end
    
  2. Set up locale handling in app/controllers/application_controller.rb:

    class ApplicationController < ActionController::Base
      before_action :set_locale
    
      private
    
      def set_locale
        I18n.locale =
          params[:locale].presence_in(I18n.available_locales.map(&:to_s)) ||
          I18n.default_locale
      end
      
      def default_url_options
        { locale: I18n.locale }
      end
    end
    
  3. Use the t helper method to load localized strings within a view:

    <h1><%= t("home.title") %></h1>
    <p><%= t("home.greeting") %></p>
    
  4. Start the Rails server:

    bin/rails server
    
  5. Navigate to the following URLs: