Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Persist default locale #13

Merged
merged 6 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 6 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,21 +66,16 @@ And that's it! Run your server and visit `http://localhost:3000/rosetta` to chec

### The Default locale

The main convention Rosetta uses is that your codebase is written in the default locale. By default, it is set to English, as shown on the rosetta interface:
> [!NOTE]
> By convention in Rosetta, the default locale should be the locale your codebase is written into. This, in theory should not change once it is set.

![CleanShot 2024-09-19 at 18 01 55](https://github.com/user-attachments/assets/678fad1d-64cd-463d-8c21-9a166abca715)

If your codebase is written in another language - say French - you can update the default locale in the Rosetta initializer:
The first time you visit the Rosetta interface, you will be prompted to set the default locale:

```ruby
# config/initializers/rosetta.rb
![CleanShot 2024-09-23 at 15 36 40](https://github.com/user-attachments/assets/6d088837-89ac-4742-b5b7-00a7db8f9c2b)

Rosetta.configure do |config|
config.set_default_locale(name: "French", code: "fr")
end
```
Enter the name and the code for the locale your codebase is written into (probably English and `en` in most cases) and submit the form. You will be redirected to the rosetta homepage where your default locale will appear.

A locale is defined by a name and a code. The name is the human-readable name of the locale, while the code can be any combination of letters separated by a dash, like `en-GB`, `fr` and so on.
![CleanShot 2024-09-19 at 18 01 55](https://github.com/user-attachments/assets/678fad1d-64cd-463d-8c21-9a166abca715)

### Adding a locale

Expand Down Expand Up @@ -158,7 +153,6 @@ By default, Rosetta inherits from `ActionController::Base`. If you want to restr
# config/initializers/rosetta.rb

Rosetta.configure do |config|
# config.set_default_locale(name: "French", code: "fr")
config.parent_controller_class = "AdminController"
end
```
Expand Down
2 changes: 1 addition & 1 deletion app/assets/builds/rosetta/application.css

Large diffs are not rendered by default.

29 changes: 29 additions & 0 deletions app/controllers/rosetta/default_locales_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
module Rosetta
class DefaultLocalesController < ApplicationController
before_action :ensure_configuration_needed

def new
@locale = Locale.new
end

def create
@locale = Locale.build_as_default(locale_params)

if @locale.save
redirect_to locales_path
else
render :new, status: :unprocessable_entity
end
end

private

def ensure_configuration_needed
redirect_to root_path if Locale.default_locale
end

def locale_params
params.require(:locale).permit(:name, :code)
end
end
end
8 changes: 7 additions & 1 deletion app/controllers/rosetta/locales_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
module Rosetta
class LocalesController < ApplicationController
before_action :ensure_default_locale_exists, only: :index

def index
@locales = [ Locale.default_locale ] + Locale.all
@locales = Locale.order(default: :desc)
end

def new
Expand All @@ -24,6 +26,10 @@ def create

private

def ensure_default_locale_exists
redirect_to new_default_locale_path unless Locale.default_locale
end

def locale_params
params.require(:locale).permit(:name, :code)
end
Expand Down
2 changes: 1 addition & 1 deletion app/helpers/rosetta/translation_helper.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Rosetta
module TranslationHelper
def _(key, locale: Rosetta.locale)
return key if Rosetta.locale.default_locale?
return key if Rosetta.locale.default?

Rosetta.translate(key, locale: locale) || key
end
Expand Down
20 changes: 9 additions & 11 deletions app/models/rosetta/locale.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,20 @@ class Locale < ApplicationRecord

class << self
def available_locales
[ Locale.default_locale ] + all
all
end

def default_locale
@default_locale ||= new(Rosetta.config.default_locale.to_h).as_default
def build_as_default(params)
new(params.merge(default: true))
end
end

def default_locale?
@default
end
def default_locale
@default_locale ||= find_by(default: true)
end

def as_default
@default = true
readonly!
self
def default_locale=(locale)
@default_locale = locale
end
end
end
end
54 changes: 54 additions & 0 deletions app/views/rosetta/default_locales/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<h1 class="text-3xl font-bold">
Setup default locale
</h1>

<div class="rounded-md bg-blue-50 p-4 mt-8">
<div class="flex">
<div class="flex-shrink-0">
<svg class="h-5 w-5 text-blue-400" viewBox="0 0 20 20" fill="currentColor" aria-hidden="true">
<path fill-rule="evenodd" d="M18 10a8 8 0 11-16 0 8 8 0 0116 0zm-7-4a1 1 0 11-2 0 1 1 0 012 0zM9 9a.75.75 0 000 1.5h.253a.25.25 0 01.244.304l-.459 2.066A1.75 1.75 0 0010.747 15H11a.75.75 0 000-1.5h-.253a.25.25 0 01-.244-.304l.459-2.066A1.75 1.75 0 009.253 9H9z" clip-rule="evenodd" />
</svg>
</div>
<div class="ml-3">
<h3 class="font-medium text-blue-800">Rosetta needs a default locale set up to work properly</h3>

<p class="text-sm text-blue-700 mt-2">
The default locale is the locale the codebase is written in. It is not supposed to change in the future, though it can be edited if needed later on.
</p>
</div>
</div>
</div>

<%= form_with model: @locale, url: default_locale_path, class: "grid gap-2 mt-8" do |f| %>
<% if @locale.errors[:code].any? %>
<div class="rounded-md bg-red-50 p-4">
<div class="flex">
<div class="flex-shrink-0">
<svg class="h-5 w-5 text-red-400" viewBox="0 0 20 20" fill="currentColor" aria-hidden="true">
<path fill-rule="evenodd" d="M10 18a8 8 0 100-16 8 8 0 000 16zM8.28 7.22a.75.75 0 00-1.06 1.06L8.94 10l-1.72 1.72a.75.75 0 101.06 1.06L10 11.06l1.72 1.72a.75.75 0 101.06-1.06L11.06 10l1.72-1.72a.75.75 0 00-1.06-1.06L10 8.94 8.28 7.22z" clip-rule="evenodd" />
</svg>
</div>
<div class="ml-3">
<div class="text-sm text-red-700">
Please check the format of your locale code, and make sure it does not already exist.
</div>
</div>
</div>
</div>
<% end %>

<div>
<%= f.label :name, class: "label" %>
<%= f.text_field :name, placeholder: "E.g. English", class: "input max-w-xs", required: true %>
</div>

<div>
<%= f.label :code, class: "label" %>
<%= f.text_field :code, placeholder: "E.g. en", class: "input max-w-28", required: true %>
<p class="mt-1 text-sm leading-6 text-gray-600">
Downcase letters followed by an optional region specifier in uppercase letters, separated by a dash. E.g. <span class="badge">fr</span> or <span class="badge">en-GB</span>.
</p>
</div>

<%= f.submit "Setup default locale", class: "btn btn-primary mt-2" %>
<% end %>
4 changes: 2 additions & 2 deletions app/views/rosetta/locales/_locale.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
</span>
</td>
<td class="relative whitespace-nowrap py-4 pl-3 pr-4 text-right text-sm font-medium sm:pr-6">
<% if locale.default_locale? %>
<% if locale.default? %>
<span class="pill">Default locale</span>
<% end %>

<% unless locale.default_locale? %>
<% unless locale.default? %>
<%= link_to "Manage", locale_translations_path(locale), class: "text-indigo-600 hover:text-indigo-900" %>
<% end %>
</td>
Expand Down
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
Rosetta::Engine.routes.draw do
root to: "locales#index"

resource :default_locale, only: %i[new create]

resources :locales do
scope module: :locales do
resources :translations, only: :index
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20240923100651_add_default_to_rosetta_locales.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddDefaultToRosettaLocales < ActiveRecord::Migration[7.2]
def change
add_column :rosetta_locales, :default, :boolean, default: false
end
end
8 changes: 0 additions & 8 deletions lib/rosetta/configuration.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,11 @@
module Rosetta
class Configuration
attr_reader :default_locale
attr_accessor :parent_controller_class
attr_accessor :queues

DefaultLocale = Struct.new(:name, :code)

def initialize
set_default_locale(name: "English", code: "en")
@parent_controller_class = "ActionController::Base"
@queues = {}
end

def set_default_locale(name:, code:)
@default_locale = DefaultLocale.new(name, code)
end
end
end
36 changes: 36 additions & 0 deletions test/controllers/rosetta/default_locales_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
require "test_helper"

module Rosetta
class DefaultLocalesControllerTest < ActionDispatch::IntegrationTest
include Engine.routes.url_helpers

setup do
Locale.destroy_all
end

test "new" do
get new_default_locale_path
assert_response :success
assert_includes response.body, "Setup default locale"
end

test "create default locale" do
assert_difference("Locale.count", 1) do
post default_locale_path, params: { locale: { name: "English", code: "en" } }
end

assert_redirected_to locales_path
end

test "redirects if the default locale already exists" do
locale = Locale.create(name: "English", code: "en", default: true)
Locale.default_locale = locale

assert_no_difference("Locale.count") do
post default_locale_path, params: { locale: { name: "English", code: "en" } }
end

assert_redirected_to root_path
end
end
end
6 changes: 6 additions & 0 deletions test/controllers/rosetta/locales_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ class LocalesControllerTest < ActionDispatch::IntegrationTest
assert_includes response.body, "Locales"
end

test "redirects to the new default locale if no locale" do
Locale.destroy_all
get locales_path
assert_redirected_to new_default_locale_path
end

test "new" do
get new_locale_path
assert_response :success
Expand Down
9 changes: 8 additions & 1 deletion test/dummy/app/views/pages/home.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,18 @@
</div>
</div>

<div>
<h3 class="text-lg font-semibold"><%= _ "Default locale" %></h3>
<div>
<%= _ Rosetta::Locale.default_locale&.name %>
</div>
</div>

<div>
<h3 class="text-lg font-semibold"><%= _ "Available locales" %></h3>
<ul class="list-disc pl-4">
<% Rosetta::Locale.available_locales.each do |locale| %>
<li><%= _ locale.name %> <%= "(#{_ "default"})" if locale.default_locale? %> - <%= link_to _("Switch"), root_path(locale: locale.code), class: "text-blue-500 hover:underline" %></li>
<li><%= _ locale.name %> <%= "(#{_ "default"})" if locale.default? %> - <%= link_to _("Switch"), root_path(locale: locale.code), class: "text-blue-500 hover:underline" %></li>
<% end %>
</ul>
</div>
Expand Down
16 changes: 10 additions & 6 deletions test/dummy/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,25 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2024_08_30_123523) do
ActiveRecord::Schema.define(version: 2024_09_23_100651) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"

create_table "rosetta_locales", force: :cascade do |t|
t.string "name"
t.string "code"
t.boolean "published", default: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index [ "code" ], name: "index_rosetta_locales_on_code", unique: true
t.boolean "default", default: false
t.index ["code"], name: "index_rosetta_locales_on_code", unique: true
end

create_table "rosetta_translation_keys", force: :cascade do |t|
t.text "value"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index [ "value" ], name: "index_rosetta_translation_keys_on_value", unique: true
t.index ["value"], name: "index_rosetta_translation_keys_on_value", unique: true
end

create_table "rosetta_translations", force: :cascade do |t|
Expand All @@ -33,9 +37,9 @@
t.integer "translation_key_id", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index [ "locale_id", "translation_key_id" ], name: "index_rosetta_translations_on_locale_id_and_translation_key_id", unique: true
t.index [ "locale_id" ], name: "index_rosetta_translations_on_locale_id"
t.index [ "translation_key_id" ], name: "index_rosetta_translations_on_translation_key_id"
t.index ["locale_id", "translation_key_id"], name: "index_rosetta_translations_on_locale_id_and_translation_key_id", unique: true
t.index ["locale_id"], name: "index_rosetta_translations_on_locale_id"
t.index ["translation_key_id"], name: "index_rosetta_translations_on_translation_key_id"
end

add_foreign_key "rosetta_translations", "rosetta_locales", column: "locale_id"
Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/rosetta/locales.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
english:
name: English
code: en
default: true

french:
name: French
code: fr
Expand Down
2 changes: 1 addition & 1 deletion test/integration/translations_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class TranslationsTest < ActionDispatch::IntegrationTest
end

test "visiting the page syncs up the missing keys" do
assert_enqueued_jobs 18 do
assert_enqueued_jobs 19 do
get root_path(locale: "fr")
end
end
Expand Down
14 changes: 0 additions & 14 deletions test/lib/rosetta/configuration_test.rb

This file was deleted.

Loading