diff --git a/app/controllers/shopify_app/authenticated_controller.rb b/app/controllers/shopify_app/authenticated_controller.rb index 5c8920647..89b509f24 100644 --- a/app/controllers/shopify_app/authenticated_controller.rb +++ b/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 diff --git a/config/locales/de.yml b/config/locales/de.yml new file mode 100644 index 000000000..33da63ae7 --- /dev/null +++ b/config/locales/de.yml @@ -0,0 +1,4 @@ +de: + logged_in: 'Eingeloggt' + logged_out: 'Erfolgreich ausgelogt' + could_not_log_in: 'Shopify Store Login fehlgeschlagen' diff --git a/config/locales/ja.yml b/config/locales/ja.yml new file mode 100644 index 000000000..f9b8700fe --- /dev/null +++ b/config/locales/ja.yml @@ -0,0 +1,4 @@ +ja: + logged_in: 'ログイン' + logged_out: 'ログインに成功しました' + could_not_log_in: 'Shopifyストアにログインできませんでした' diff --git a/lib/shopify_app.rb b/lib/shopify_app.rb index 28dadcc53..45daed6ad 100644 --- a/lib/shopify_app.rb +++ b/lib/shopify_app.rb @@ -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' diff --git a/lib/shopify_app/localization.rb b/lib/shopify_app/localization.rb new file mode 100644 index 000000000..1fa2a3d27 --- /dev/null +++ b/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 diff --git a/test/shopify_app/localization_test.rb b/test/shopify_app/localization_test.rb new file mode 100644 index 000000000..ec771c97c --- /dev/null +++ b/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