Created
May 16, 2021 16:19
-
-
Save secretpray/d9448988fc6e8868957062ddbd010750 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1) | |
# Gemfile | |
gem 'rails-i18n' | |
bundle | |
2) | |
bin/rails g migration add_language_to_users language | |
class AddLanguageToUsers < ActiveRecord::Migration[6.1] | |
def change | |
add_column :users, :language, :string | |
end | |
end | |
bin/rails db:migrate | |
3) | |
config/application.rb | |
config.time_zone = "Kyiv" # 'Paris', 'Moscow' | |
config.i18n.available_locales = %i(en ru) | |
config.i18n.default_locale = :en | |
config.i18n.fallbacks = true | |
4) | |
app/controllers/application_controller.rb | |
before_action :set_locale | |
def set_locale | |
if user_signed_in? && !current_user.language.blank? | |
I18n.locale = current_user.language | |
else | |
I18n.locale = params[:lang] || locate_from_header || I18n.default_locale | |
end | |
end | |
### Alternative 'set_locale' method for non-login user ##### | |
def set_locale | |
if user_signed_in? && !current_user.language.blank? | |
I18n.locale = current_user.language | |
else | |
cookies[:my_locale].clear unless !cookies[:my_locale].blank? && I18n.available_locales.include?(cookies[:my_locale].to_sym) | |
I18n.locale = params[:lang] || cookies[:my_locale] || locate_from_header || I18n.default_locale | |
cookies.permanent[:my_locale] = I18n.locale | |
end | |
end | |
### Alternative for non-login user ##### | |
def default_url_options | |
return {} if I18n.locale == I18n.default_locale | |
{ lang: I18n.locale } # { lang: ((I18n.locale == I18n.default_locale) ? nil : I18n.locale) } | |
end | |
private | |
def locate_from_header | |
request.env.fetch('HTTP_ACCEPT_LANGUAGE', '').scan(/[a-z]{2}/).first | |
end | |
5) | |
Navigator menu | |
app/views/shared/_navigation.html.erb | |
<ul> | |
<% unless user_signed_in? %> | |
<% if I18n.locale == :ru %> | |
<li class="nav-item"><%= link_to '?lang=en'.prepend(request.path), class: 'nav-link' do %> | |
<i class="fas fa-toggle-on"><span class='fw-normal'> English</span></i> | |
<% end %> | |
</li> | |
<% else %> | |
<li class="nav-item"><%= link_to '?lang=ru'.prepend(request.path), class: 'nav-link' do %> | |
<i class="fas fa-toggle-on"><span class='fw-normal'> Русский</span></i> | |
<% end %> | |
</li> | |
<% end %> | |
</ul> | |
<!-- request.path + "?lang=ru" ? current_uri = request.env['PATH_INFO'] or request.path_info or request.url or request.path | |
or request.env['ORIGINAL_FULLPATH'] or request.env['REQUEST_URI']--> | |
<% end %> | |
6) | |
Add to: | |
config/locales | |
en.yml | |
ru.yml | |
.. | |
PS | |
Pluralization Rules: | |
for En (simple) - | |
en: | |
global: | |
forms: | |
submit: Submit | |
messages: | |
errors: | |
one: "One error prohibited this feedback from being saved" | |
other: "%{count} errors prohibited this feedback from being saved" | |
for Russian - | |
ru: | |
global: | |
forms: | |
submit: Отправить | |
messages: | |
errors: | |
one: "Не удалось сохранить отзыв! Найдена одна ошибка:" | |
few: "Не удалось сохранить отзыв! Найдены %{count} ошибки:" | |
many: "Не удалось сохранить отзыв! Найдено %{count} ошибок:" | |
other: "Не удалось сохранить отзыв! Найдена %{count} ошибка:" | |
Sample: | |
<h2><%= t 'global.forms.messages.errors', count: feedback.errors.count %></h2> | |
Date | |
<!-- views/feedbacks/_feedback.html.erb --> | |
<article> | |
<em> | |
<%= tag.time l(feedback.created_at, format: :long), datetime: feedback.created_at %><br> | |
<%= t 'global.feedback.posted_by', author: feedback.author %> | |
</em> | |
<!--... --> | |
</article> | |
More | |
def extract_locale | |
parsed_locale = params[:locale] | |
I18n.available_locales.map(&:to_s).include?(parsed_locale) ? parsed_locale : nil | |
end | |
def change_locale | |
l = params[:locale].to_s.strip.to_sym | |
l = I18n.default_locale unless I18n.available_locales.include?(l) | |
cookies.permanent[:my_locale] = l | |
redirect_to request.referer || root_url | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment