Skip to content
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

Add i18n locale param support #409

Merged
merged 3 commits into from
May 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions app/controllers/shopify_app/authenticated_controller.rb
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
4 changes: 4 additions & 0 deletions config/locales/de.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
de:
logged_in: 'Eingeloggt'
logged_out: 'Erfolgreich ausgelogt'
could_not_log_in: 'Shopify Store Login fehlgeschlagen'
4 changes: 4 additions & 0 deletions config/locales/ja.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
ja:
logged_in: 'ログイン'
logged_out: 'ログインに成功しました'
could_not_log_in: 'Shopifyストアにログインできませんでした'
1 change: 1 addition & 0 deletions lib/shopify_app.rb
Original file line number Diff line number Diff line change
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
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
57 changes: 57 additions & 0 deletions test/shopify_app/localization_test.rb
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