-
Notifications
You must be signed in to change notification settings - Fork 15
add language dropdown #127
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
Open
secretpray
wants to merge
5
commits into
XaoGao:develop
Choose a base branch
from
secretpray:issue_93
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
5ae649a
add language dropdown
secretpray 5103d68
fixed by git comment, ref current_local
secretpray b7e760d
add rspec test, check with linters
secretpray 483fc00
small refactoring, remove unused
secretpray fbfa30f
add user display name
secretpray File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,32 @@ | ||
| class ApplicationController < ActionController::Base | ||
| before_action :configure_permitted_parameters, if: :devise_controller? | ||
| before_action :set_current_locale | ||
| helper_method :current_locale | ||
|
|
||
| rescue_from ActionPolicy::Unauthorized do |_e| | ||
| redirect_to root_path | ||
| end | ||
|
|
||
| def set_current_locale | ||
| Current.locale = extract_locale_from_user || extract_locale_from_session || I18n.default_locale | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def configure_permitted_parameters | ||
| devise_parameter_sanitizer.permit(:sign_up, keys: %i[name avatar]) | ||
| devise_parameter_sanitizer.permit(:account_update, keys: %i[name avatar]) | ||
| end | ||
|
|
||
| def current_locale | ||
| Current.locale | ||
| end | ||
|
|
||
| def extract_locale_from_user | ||
| current_user&.locale if defined?(current_user) && current_user&.locale | ||
| end | ||
|
|
||
| def extract_locale_from_session | ||
| session[:locale] | ||
| end | ||
| end |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,33 @@ | ||
| class PagesController < ApplicationController | ||
| def welcome; end | ||
|
|
||
| def switch_locale | ||
| Current.locale = locale | ||
| I18n.locale = Current.locale | ||
| store_locale | ||
|
|
||
| redirect_back(fallback_location: root_path) | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def params_locale | ||
| params[:locale].to_sym | ||
| end | ||
|
|
||
| def default_locale | ||
| @default_locale ||= current_user&.locale || I18n.default_locale | ||
| end | ||
|
|
||
| def locale | ||
| I18n.available_locales.include?(params_locale) ? params_locale : default_locale | ||
| end | ||
|
|
||
| def store_locale | ||
| if user_signed_in? | ||
| current_user.update(locale: I18n.locale) | ||
| else | ||
| session[:locale] = I18n.locale | ||
| end | ||
| end | ||
| end | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| module NavbarHelper | ||
| def selected_language_icon | ||
| Rails.cache.fetch("selected_language_name/#{current_locale}", expires_in: 1.day) do | ||
| image_tag "icons/#{current_locale}.svg", size: "23", class: "rounded-2", title: t("navbar.language") | ||
| end | ||
| end | ||
|
|
||
| def next_locale | ||
| I18n.locale == :ru ? :en : :ru | ||
| end | ||
| end |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| class Current < ActiveSupport::CurrentAttributes | ||
| thread_mattr_accessor :locale | ||
| end |
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -31,3 +31,5 @@ | |
|
|
||
| en: | ||
| hello: "Hello world" | ||
| navbar: | ||
| language: Change language | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -269,3 +269,6 @@ ru: | |
| long: "%d %B %Y, %H:%M" | ||
| short: "%d %b, %H:%M" | ||
| pm: вечера | ||
| navbar: | ||
| language: Смена языка | ||
|
|
||
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| require "rails_helper" | ||
|
|
||
| describe PagesController, type: :request do | ||
| describe "GET /welcome" do | ||
| it "returns http status success" do | ||
| get root_url | ||
| expect(response).to have_http_status(:success) | ||
| end | ||
| end | ||
|
|
||
| describe "GET #switch_locale" do | ||
| let(:user) { create(:user) } | ||
|
|
||
| context "when user is signed in" do | ||
| before { sign_in user } | ||
|
|
||
| it "changes the I18n locale" do | ||
| get switch_locale_path(locale: "ru") | ||
| expect(I18n.locale).to eq(:ru) | ||
| end | ||
|
|
||
| it "changes the user locale" do | ||
| get switch_locale_path(locale: "ru") | ||
| expect(user.reload.locale).to eq("ru") | ||
| end | ||
|
|
||
| it "clears the session locale" do | ||
| get switch_locale_path(locale: "ru") | ||
| expect(session[:locale]).to be_nil | ||
| end | ||
|
|
||
| it "redirects back" do | ||
| get switch_locale_path(locale: "ru") | ||
| expect(response).to redirect_to(root_path) | ||
| end | ||
| end | ||
|
|
||
| context "when user is not signed in" do | ||
| it "changes the I18n locale" do | ||
| get switch_locale_path(locale: "ru") | ||
| expect(I18n.locale).to eq(:ru) | ||
| end | ||
|
|
||
| it "changes the session locale" do | ||
| get switch_locale_path(locale: "ru") | ||
| expect(session[:locale]).to eq(:ru) | ||
| end | ||
|
|
||
| it "redirects back" do | ||
| get switch_locale_path(locale: "ru") | ||
| expect(response).to redirect_to(root_path) | ||
| end | ||
| end | ||
|
|
||
| context "when the requested locale is not available" do | ||
| it "falls back to the default I18n locale" do | ||
| get switch_locale_path(locale: "invalid_locale") | ||
| expect(I18n.locale).to eq(:en) | ||
| end | ||
|
|
||
| it "falls back to the default session locale" do | ||
| get switch_locale_path(locale: "invalid_locale") | ||
| expect(session[:locale]).to eq(:en) | ||
| end | ||
|
|
||
| it "redirects back" do | ||
| get switch_locale_path(locale: "invalid_locale") | ||
| expect(response).to redirect_to(root_path) | ||
| end | ||
| end | ||
| end | ||
| end |
This file was deleted.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.