Skip to content

Commit

Permalink
Add i18n locale param support
Browse files Browse the repository at this point in the history
  • Loading branch information
SebastianSzturo committed May 9, 2017
1 parent 520865c commit f8467a9
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 0 deletions.
4 changes: 4 additions & 0 deletions app/controllers/shopify_app/authenticated_controller.rb
@@ -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
1 change: 1 addition & 0 deletions lib/shopify_app.rb
Expand Up @@ -18,6 +18,7 @@
require 'shopify_app/shop'
require 'shopify_app/session_storage'
require 'shopify_app/sessions_concern'
require 'shopify_app/localization'
require 'shopify_app/login_protection'
require 'shopify_app/webhooks_manager'
require 'shopify_app/scripttags_manager'
Expand Down
16 changes: 16 additions & 0 deletions lib/shopify_app/localization.rb
@@ -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
57 changes: 57 additions & 0 deletions test/shopify_app/localization_test.rb
@@ -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

0 comments on commit f8467a9

Please sign in to comment.