Skip to content
This repository has been archived by the owner on Jan 7, 2018. It is now read-only.

Commit

Permalink
Fix admin localization not actually working. Oops. Add tests too.
Browse files Browse the repository at this point in the history
  • Loading branch information
GUI committed Feb 11, 2015
1 parent f390f5c commit af28d43
Show file tree
Hide file tree
Showing 11 changed files with 107 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,6 @@ config/environments/*.local.yml

# Test coverage reports
/coverage

# Generated assets
/public/test-assets
3 changes: 3 additions & 0 deletions app/assets/javascripts/admin/locales/de.js.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
//= depend_on "de.yml"
//= require polyglot
<%= JsLocaleHelper.output_locale(:de) %>
3 changes: 3 additions & 0 deletions app/assets/javascripts/admin/locales/es-419.js.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
//= depend_on "es-419.yml"
//= require polyglot
<%= JsLocaleHelper.output_locale(:"es-419") %>
3 changes: 3 additions & 0 deletions app/assets/javascripts/admin/locales/fi.js.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
//= depend_on "fi.yml"
//= require polyglot
<%= JsLocaleHelper.output_locale(:fi) %>
3 changes: 3 additions & 0 deletions app/assets/javascripts/admin/locales/fr.js.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
//= depend_on "fr.yml"
//= require polyglot
<%= JsLocaleHelper.output_locale(:fr) %>
3 changes: 3 additions & 0 deletions app/assets/javascripts/admin/locales/it.js.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
//= depend_on "it.yml"
//= require polyglot
<%= JsLocaleHelper.output_locale(:it) %>
3 changes: 3 additions & 0 deletions app/assets/javascripts/admin/locales/ru.js.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
//= depend_on "ru.yml"
//= require polyglot
<%= JsLocaleHelper.output_locale(:ru) %>
8 changes: 8 additions & 0 deletions app/controllers/admin/sessions_controller.rb
Original file line number Diff line number Diff line change
@@ -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
6 changes: 5 additions & 1 deletion config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"]

Expand Down
24 changes: 24 additions & 0 deletions config/environments/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
49 changes: 49 additions & 0 deletions spec/features/admin/locales_spec.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit af28d43

Please sign in to comment.