Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
520865c
commit f8467a9
Showing
4 changed files
with
78 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,8 +1,12 @@ | ||
module ShopifyApp | ||
class AuthenticatedController < ApplicationController | ||
include ShopifyApp::Localization | ||
include ShopifyApp::LoginProtection | ||
|
||
before_action :set_locale | ||
before_action :login_again_if_different_shop | ||
around_action :shopify_session | ||
|
||
layout ShopifyApp.configuration.embedded_app? ? 'embedded_app' : 'application' | ||
end | ||
end |
This file contains 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 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,16 @@ | ||
module ShopifyApp | ||
module Localization | ||
extend ActiveSupport::Concern | ||
|
||
def set_locale | ||
if params[:locale] | ||
session[:locale] = params[:locale] | ||
else | ||
session[:locale] ||= I18n.default_locale | ||
end | ||
I18n.locale = session[:locale] | ||
rescue I18n::InvalidLocale | ||
I18n.locale = I18n.default_locale | ||
end | ||
end | ||
end |
This file contains 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,57 @@ | ||
require 'test_helper' | ||
require 'action_controller' | ||
require 'action_controller/base' | ||
|
||
class LocalizationController < ActionController::Base | ||
include ShopifyApp::Localization | ||
|
||
before_action :set_locale | ||
|
||
def index | ||
render text: I18n.locale | ||
end | ||
end | ||
|
||
class LocalizationTest < ActionController::TestCase | ||
tests LocalizationController | ||
|
||
test "falls back to I18n.default if locale param is not present" do | ||
I18n.available_locales = [:en, :de, :es, :ja, :fr] | ||
I18n.default_locale = :ja | ||
|
||
with_test_routes do | ||
get :index | ||
assert 'ja', response.body | ||
end | ||
end | ||
|
||
test "set I18n.locale to passed locale param" do | ||
I18n.available_locales = [:en, :de, :es, :ja, :fr] | ||
|
||
with_test_routes do | ||
get :index, locale: 'de' | ||
assert 'de', response.body | ||
end | ||
end | ||
|
||
test "falls back to I18n.default if locale is not supported" do | ||
I18n.available_locales = [:en, :de, :es, :ja, :fr] | ||
I18n.default_locale = :en | ||
|
||
with_test_routes do | ||
get :index, locale: 'cu' | ||
assert 'en', response.body | ||
end | ||
end | ||
|
||
private | ||
|
||
def with_test_routes | ||
with_routing do |set| | ||
set.draw do | ||
get '/locale' => 'localization#index' | ||
end | ||
yield | ||
end | ||
end | ||
end |