-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Load locales from I18n.available_locales
Also uses a Railtie to load i18n files instead of the engine (I was having some trouble with the Engine loading the files at all with Rails 4.2.x). Pulls code from [devise-i18n](https://github.com/tigrish/devise-i18n). [closes #8]
- Loading branch information
Showing
17 changed files
with
197 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1 @@ | ||
module DoorkeeperI18n | ||
class Engine < ::Rails::Engine | ||
end | ||
end | ||
require 'doorkeeper-i18n/railtie' if defined?(Rails) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
require 'rails' | ||
|
||
module DoorkeeperI18n | ||
class Railtie < ::Rails::Railtie #:nodoc: | ||
initializer 'doorkeeper-i18n' do |app| | ||
DoorkeeperI18n::Railtie.instance_eval do | ||
pattern = pattern_from app.config.i18n.available_locales | ||
|
||
add("rails/locales/#{pattern}.yml") | ||
end | ||
end | ||
|
||
protected | ||
|
||
def self.add(pattern) | ||
files = Dir[File.join(File.dirname(__FILE__), '../..', pattern)] | ||
|
||
I18n.load_path.concat(files) | ||
end | ||
|
||
def self.pattern_from(args) | ||
array = Array(args || []) | ||
array.blank? ? '*' : "{#{array.join ','}}" | ||
end | ||
end | ||
end |
Empty file.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
require File.expand_path(File.dirname(__FILE__) + '/spec_helper') | ||
|
||
Dir.glob('config/locales/*.yml').each do |locale_file| | ||
Dir.glob('rails/locales/*.yml').each do |locale_file| | ||
describe "a doorkeeper-i18n #{locale_file} locale file" do | ||
it_behaves_like 'a valid locale file', locale_file | ||
it { expect(locale_file).to be_a_subset_of 'config/locales/en.yml' } | ||
it { expect(locale_file).to be_a_subset_of 'rails/locales/en.yml' } | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
# encoding: utf-8 | ||
require File.expand_path(File.dirname(__FILE__) + '/spec_helper') | ||
|
||
describe 'Locale loading' do | ||
let(:app) do | ||
DoorkeeperI18n::Spec::FakeApp | ||
end | ||
|
||
let(:translate_stuff) do | ||
lambda do | ||
<<-EOS.gsub(/^ */, '') | ||
In French: #{I18n.t('doorkeeper.layouts.admin.nav.oauth2_provider', locale: :fr)} | ||
In Italian: #{I18n.t('doorkeeper.layouts.admin.nav.oauth2_provider', locale: :it)} | ||
In Japanese: #{I18n.t('doorkeeper.layouts.admin.nav.oauth2_provider', locale: :ja)} | ||
EOS | ||
end | ||
end | ||
|
||
context 'when i18n.available_locales are specified in config' do | ||
let(:translations) do | ||
app.run(translate_stuff) do |config| | ||
config.i18n.available_locales = [:fr, :it] | ||
end | ||
end | ||
|
||
it 'loads only specified locales' do | ||
expected_translations = <<-EOS.gsub(/^ */, '') | ||
In French: Fournisseur OAuth2 | ||
In Italian: OAuth2 Provider | ||
In Japanese: translation missing: ja.doorkeeper.layouts.admin.nav.oauth2_provider | ||
EOS | ||
|
||
expect(translations).to eq(expected_translations) | ||
end | ||
end | ||
|
||
context 'when single locale is assigned to i18n.available_locales' do | ||
let(:translations) do | ||
app.run(translate_stuff) do |config| | ||
config.i18n.available_locales = 'fr' | ||
end | ||
end | ||
|
||
it 'loads only this locale' do | ||
expected_translations = <<-EOS.gsub(/^ */, '') | ||
In French: Fournisseur OAuth2 | ||
In Italian: translation missing: it.doorkeeper.layouts.admin.nav.oauth2_provider | ||
In Japanese: translation missing: ja.doorkeeper.layouts.admin.nav.oauth2_provider | ||
EOS | ||
|
||
expect(translations).to eq(expected_translations) | ||
end | ||
end | ||
|
||
context 'when i18n.available_locales is not set' do | ||
let(:translations) { app.run(translate_stuff) } | ||
|
||
it 'loads all locales' do | ||
expected_translations = <<-EOS.gsub(/^ */, '') | ||
In French: Fournisseur OAuth2 | ||
In Italian: OAuth2 Provider | ||
In Japanese: OAuth2 プロバイダー | ||
EOS | ||
|
||
expect(translations).to eq(expected_translations) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,5 @@ | |
|
||
require "rspec" | ||
require "i18n-spec" | ||
|
||
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
module DoorkeeperI18n | ||
module Spec | ||
module FakeApp | ||
# | ||
# Starts a new Rails app and runs the given config block before | ||
# initializing it | ||
# | ||
def self.start | ||
require 'action_controller/railtie' | ||
require 'doorkeeper-i18n' | ||
|
||
app = Class.new(Rails::Application) | ||
app.config.eager_load = false | ||
app.config.i18n.enforce_available_locales = false | ||
|
||
yield(app.config) if block_given? | ||
|
||
app.initialize! | ||
end | ||
|
||
# | ||
# Initialize Rails app in a clean environment. | ||
# | ||
# @param test [Proc] which have to be run after app is initialized | ||
# @return [String] Result of calling +test+ after app was initialized | ||
# | ||
def self.run(test, &block) | ||
r, w = IO.pipe | ||
|
||
pid = fork do | ||
r.close | ||
|
||
start(&block) | ||
|
||
w.write(test.call) | ||
end | ||
|
||
w.close | ||
result = r.read | ||
Process.wait(pid) | ||
result | ||
end | ||
end | ||
end | ||
end |