From af28d43f6e9f46e87f7583eef63501e97cda7f65 Mon Sep 17 00:00:00 2001 From: Nick Muerdter Date: Wed, 11 Feb 2015 09:56:40 -0700 Subject: [PATCH] Fix admin localization not actually working. Oops. Add tests too. --- .gitignore | 3 ++ .../javascripts/admin/locales/de.js.erb | 3 ++ .../javascripts/admin/locales/es-419.js.erb | 3 ++ .../javascripts/admin/locales/fi.js.erb | 3 ++ .../javascripts/admin/locales/fr.js.erb | 3 ++ .../javascripts/admin/locales/it.js.erb | 3 ++ .../javascripts/admin/locales/ru.js.erb | 3 ++ app/controllers/admin/sessions_controller.rb | 8 +++ config/application.rb | 6 ++- config/environments/test.rb | 24 +++++++++ spec/features/admin/locales_spec.rb | 49 +++++++++++++++++++ 11 files changed, 107 insertions(+), 1 deletion(-) create mode 100644 app/assets/javascripts/admin/locales/de.js.erb create mode 100644 app/assets/javascripts/admin/locales/es-419.js.erb create mode 100644 app/assets/javascripts/admin/locales/fi.js.erb create mode 100644 app/assets/javascripts/admin/locales/fr.js.erb create mode 100644 app/assets/javascripts/admin/locales/it.js.erb create mode 100644 app/assets/javascripts/admin/locales/ru.js.erb create mode 100644 spec/features/admin/locales_spec.rb diff --git a/.gitignore b/.gitignore index 93d45ad9..f0dce261 100644 --- a/.gitignore +++ b/.gitignore @@ -33,3 +33,6 @@ config/environments/*.local.yml # Test coverage reports /coverage + +# Generated assets +/public/test-assets diff --git a/app/assets/javascripts/admin/locales/de.js.erb b/app/assets/javascripts/admin/locales/de.js.erb new file mode 100644 index 00000000..b77c2554 --- /dev/null +++ b/app/assets/javascripts/admin/locales/de.js.erb @@ -0,0 +1,3 @@ +//= depend_on "de.yml" +//= require polyglot +<%= JsLocaleHelper.output_locale(:de) %> diff --git a/app/assets/javascripts/admin/locales/es-419.js.erb b/app/assets/javascripts/admin/locales/es-419.js.erb new file mode 100644 index 00000000..c47343ef --- /dev/null +++ b/app/assets/javascripts/admin/locales/es-419.js.erb @@ -0,0 +1,3 @@ +//= depend_on "es-419.yml" +//= require polyglot +<%= JsLocaleHelper.output_locale(:"es-419") %> diff --git a/app/assets/javascripts/admin/locales/fi.js.erb b/app/assets/javascripts/admin/locales/fi.js.erb new file mode 100644 index 00000000..5c5423b0 --- /dev/null +++ b/app/assets/javascripts/admin/locales/fi.js.erb @@ -0,0 +1,3 @@ +//= depend_on "fi.yml" +//= require polyglot +<%= JsLocaleHelper.output_locale(:fi) %> diff --git a/app/assets/javascripts/admin/locales/fr.js.erb b/app/assets/javascripts/admin/locales/fr.js.erb new file mode 100644 index 00000000..0254c0fd --- /dev/null +++ b/app/assets/javascripts/admin/locales/fr.js.erb @@ -0,0 +1,3 @@ +//= depend_on "fr.yml" +//= require polyglot +<%= JsLocaleHelper.output_locale(:fr) %> diff --git a/app/assets/javascripts/admin/locales/it.js.erb b/app/assets/javascripts/admin/locales/it.js.erb new file mode 100644 index 00000000..c4cb7468 --- /dev/null +++ b/app/assets/javascripts/admin/locales/it.js.erb @@ -0,0 +1,3 @@ +//= depend_on "it.yml" +//= require polyglot +<%= JsLocaleHelper.output_locale(:it) %> diff --git a/app/assets/javascripts/admin/locales/ru.js.erb b/app/assets/javascripts/admin/locales/ru.js.erb new file mode 100644 index 00000000..142719c6 --- /dev/null +++ b/app/assets/javascripts/admin/locales/ru.js.erb @@ -0,0 +1,3 @@ +//= depend_on "ru.yml" +//= require polyglot +<%= JsLocaleHelper.output_locale(:ru) %> diff --git a/app/controllers/admin/sessions_controller.rb b/app/controllers/admin/sessions_controller.rb index 75def089..26d6d509 100644 --- a/app/controllers/admin/sessions_controller.rb +++ b/app/controllers/admin/sessions_controller.rb @@ -1,8 +1,16 @@ class Admin::SessionsController < Devise::SessionsController + before_filter :set_locale + def new end def after_sign_out_path_for(resource_or_scope) admin_path end + + private + + def set_locale + I18n.locale = http_accept_language.compatible_language_from(I18n.available_locales) + end end diff --git a/config/application.rb b/config/application.rb index 0255f0e8..97afe578 100644 --- a/config/application.rb +++ b/config/application.rb @@ -126,11 +126,15 @@ class Application < Rails::Application config.assets.precompile += %w( admin.css admin.js - admin/locales/en.js admin/stats.js ie_lt_9.js ) + # Detect and precompile all the locale assets. + Dir.glob("#{config.root}/app/assets/javascripts/admin/locales/*.js.erb").each do |path| + config.assets.precompile << path.gsub(%r{^.*/app/assets/javascripts/}, "").gsub(/\.erb$/, "") + end + config.ember.variant = :development config.handlebars.templates_root = ["admin/templates", "templates"] diff --git a/config/environments/test.rb b/config/environments/test.rb index 9387298c..4bb84c9a 100644 --- a/config/environments/test.rb +++ b/config/environments/test.rb @@ -31,4 +31,28 @@ # Print deprecation notices to the stderr config.active_support.deprecation = :stderr + + # Use precompiled assets in test mode so we can properly catch errors + # triggered by not having assets in the precompile list. + config.assets.compile = false + config.assets.digest = true + config.assets.prefix = "/test-assets" + + # Precompile additional assets for the test environment. + config.assets.precompile += %w( + admin_test.css + admin_test.js + ) + + config.before_initialize do |app| + # Run the asset precompile phase prior to running tests so that we can test + # with precompiled assets (so we're more properly testing for any potential + # missing precompiled assets). However, note that we have to call this + # here, instead of in something like an rspec before suite so that the + # precompile happens early enough so that the current environment can load + # the precompiled digests. + unless ENV["PRECOMPILE_TEST_ASSETS"] + system("RAILS_ENV=test PRECOMPILE_TEST_ASSETS=true bundle exec rake assets:precompile") + end + end end diff --git a/spec/features/admin/locales_spec.rb b/spec/features/admin/locales_spec.rb new file mode 100644 index 00000000..61438d35 --- /dev/null +++ b/spec/features/admin/locales_spec.rb @@ -0,0 +1,49 @@ +require "spec_helper" + +describe "locales", :js => true do + describe "login page" do + I18n.available_locales.each do |locale| + it "translates in #{locale}" do + page.driver.add_headers("Accept-Language" => locale.to_s) + visit "/admin/login" + page.should have_content(I18n.t("omniauth_providers.developer", :locale => locale)) + end + + it "falls back to english for unknown languages" do + page.driver.add_headers("Accept-Language" => "zz") + visit "/admin/login" + page.should have_content(I18n.t("omniauth_providers.developer", :locale => "en")) + end + end + end + + describe "admin" do + login_admin + + describe "server-side and client-side js translations" do + I18n.available_locales.each do |locale| + it "translates in #{locale}" do + page.driver.add_headers("Accept-Language" => locale.to_s) + visit "/admin/#/apis/new" + + # Server-side rendered i18n + page.should have_content(I18n.t("admin.nav.analytics", :locale => locale)) + + # Client-side rendered i18n + page.should have_content(I18n.t("admin.api.servers.add", :locale => locale)) + end + + it "falls back to english for unknown languages" do + page.driver.add_headers("Accept-Language" => "zz") + visit "/admin/#/apis/new" + + # Server-side rendered i18n + page.should have_content(I18n.t("admin.nav.analytics", :locale => "en")) + + # Client-side rendered i18n + page.should have_content(I18n.t("admin.api.servers.add", :locale => "en")) + end + end + end + end +end