Skip to content

Commit

Permalink
Translations.
Browse files Browse the repository at this point in the history
  • Loading branch information
xet7 committed Nov 2, 2024
1 parent 8fe53f6 commit 5814d40
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 3 deletions.
7 changes: 6 additions & 1 deletion app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ class ApplicationController < ActionController::Base

rescue_from Pundit::NotAuthorizedError, with: :user_not_authorized

def update_locale
I18n.locale = params[:locale] || I18n.default_locale
redirect_back(fallback_location: root_path)
end

private
def authenticate
if (session_record = Session.find_by(id: cookies.signed[:session_token]))
Expand All @@ -33,7 +38,7 @@ def user_not_authorized
end

def set_locale
I18n.locale = params[:locale] || I18n.default_locale
I18n.locale = current_user&.language || I18n.default_locale
end

def set_current_user
Expand Down
9 changes: 9 additions & 0 deletions app/controllers/languages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@ def edit
@language = Language.find(params[:id])
end

def update_locale
if current_user.update(language: params[:locale])
I18n.locale = params[:locale]
redirect_to languages_path, notice: "Locale successfully updated."
else
redirect_to languages_path, alert: "Failed to update locale."
end
end

def set_locale
I18n.locale = params[:locale] || I18n.default_locale
end
Expand Down
2 changes: 1 addition & 1 deletion app/models/concerns/role_enum.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module RoleEnum
extend ActiveSupport::Concern

included do
enum role: { owner: 0, admin: 1, editor: 2, viewer: 3 }
enum :role, [:owner, :admin, :editor, :viewer]

validates :role, inclusion: { in: roles.keys }
end
Expand Down
3 changes: 3 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,7 @@ def others
after_update if: :password_digest_previously_changed? do
sessions.where.not(id: Current.session).delete_all
end

# Add locale attribute (ensure a migration is created to add this column)
# attr_accessor :locale
end
2 changes: 1 addition & 1 deletion app/views/languages/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
<h1>Available Languages</h1>
<ul>
<% @languages.each do |locale| %>
<li><%= link_to locale, url_for(locale: locale) %></li>
<li><%= link_to locale, update_locale_path(locale: locale), method: :post %></li>
<% end %>
</ul>
3 changes: 3 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@

# Route to change locale
patch "change_locale", to: "settings#change_locale"
post "update_locale", to: "languages#update_locale"
get "/update_locale", to: "application#update_locale"
# get 'update_locale', to: 'application#update_locale'

# Defines the root path route ("/")
# root "posts#index"
Expand Down
7 changes: 7 additions & 0 deletions db/migrate/xxxx_add_locale_to_users.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

class AddLocaleToUsers < ActiveRecord::Migration[6.0]
def change
add_column :users, :locale, :string, default: "en"
end
end

0 comments on commit 5814d40

Please sign in to comment.