spree_change_locale 0.4 → 0.5

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -20,3 +20,5 @@ else
20
20
  end
21
21
 
22
22
  gemspec
23
+
24
+ gem 'http_accept_language', '~> 1.0'
data/Gemfile.lock CHANGED
@@ -1,8 +1,9 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- spree_change_locale (0.70.1)
5
- spree_core (>= 0.70.1)
4
+ spree_change_locale (0.5)
5
+ http_accept_language (~> 1.0)
6
+ spree_core (>= 0.70.0)
6
7
 
7
8
  GEM
8
9
  remote: http://rubygems.org/
@@ -81,6 +82,7 @@ GEM
81
82
  json (>= 1.4.6)
82
83
  highline (1.5.1)
83
84
  hike (1.2.1)
85
+ http_accept_language (1.0.2)
84
86
  i18n (0.6.0)
85
87
  jquery-rails (1.0.16)
86
88
  railties (~> 3.0)
@@ -203,7 +205,7 @@ GEM
203
205
  treetop (1.4.10)
204
206
  polyglot
205
207
  polyglot (>= 0.3.1)
206
- tzinfo (0.3.30)
208
+ tzinfo (0.3.31)
207
209
  xpath (0.1.4)
208
210
  nokogiri (~> 1.3)
209
211
 
@@ -214,6 +216,7 @@ DEPENDENCIES
214
216
  capybara (= 1.0.1)
215
217
  cucumber-rails (= 1.0.0)
216
218
  database_cleaner (= 0.6.7)
219
+ http_accept_language (~> 1.0)
217
220
  nokogiri
218
221
  rspec-rails (= 2.6.1)
219
222
  ruby-debug19
data/README.md CHANGED
@@ -6,6 +6,9 @@ Easy locale change in Spree.
6
6
 
7
7
  Add the following to your Gemfile
8
8
 
9
+ gem 'spree_change_locale'
10
+
11
+ Or if you want to use edge:
9
12
  gem 'spree_change_locale', :git => 'git://github.com/humancopy/spree-change-locale.git'
10
13
 
11
14
  Run:
data/Versionfile CHANGED
@@ -1 +1 @@
1
- "0.70.x" => { :version => "0.4" }
1
+ "0.70.x" => { :version => "0.5" }
@@ -1,28 +1,8 @@
1
1
  Spree::BaseController.class_eval do
2
- helper_method :enabled_locales
3
2
  before_filter :set_locale, :if => proc { !session[:locale] }
4
3
 
5
4
  protected
6
- def enabled_locales
7
- @enabled_locales ||= (Spree::Config.get(:enabled_locales) || get_locales || I18n.available_locales || [I18n.default_locale]).collect(&:intern)
8
- end
9
-
10
- # inspired by: https://github.com/greendog/spree_locales_dropdown
11
- def get_locales
12
- path = Rails.root.join('config', 'locales')
13
- items = Dir.glob(Rails.root.join('config', 'locales', '*.yml')).collect { |filename| File.basename(filename.gsub(/\.yml$/, '')) }
14
- items unless items.blank?
15
- # Dir.glob(Rails.root.join('config', 'locales', '*.yml')).inject([]) do |mem, filename|
16
- # locale_file = YAML.load_file(path.join(filename))
17
- # str = File.basename(filename.gsub(/\.yml$/, ''))
18
- # str = str.gsub("_spree", "")
19
- # mem << str if locale_file.has_key?(str)
20
- #
21
- # mem
22
- # end
23
- end
24
-
25
5
  def set_locale
26
- session[:locale] = I18n.locale = (request.preferred_language_from(enabled_locales) || request.compatible_language_from(enabled_locales) || I18n.default_locale).intern
6
+ session[:locale] = I18n.locale = (request.preferred_language_from(I18n.enabled_locales) || request.compatible_language_from(I18n.enabled_locales) || I18n.default_locale).intern
27
7
  end
28
8
  end
@@ -1,10 +1,12 @@
1
1
  module LocaleHelper
2
+ # mirror I18n extension
3
+ def enabled_locales; I18n.enabled_locales end
4
+ def locale_enabled?(locale); I18n.locale_enabled?(locale) end
5
+
2
6
  def link_to_locale(locale, name = nil)
3
- link_to name || locale_name(locale), "/#{locale}#{request.path}"
7
+ link_to name || locale_name(locale), "/#{locale}#{request && request.path}" if locale_enabled?(locale)
4
8
  end
5
9
  def locale_name(locale)
6
- I18n.t :this_file_language, :locale => locale
7
- #
8
- # t("locale.#{locale.to_s.sub('-', '_')}")
10
+ I18n.t(:this_file_language, :locale => locale) if locale_enabled?(locale)
9
11
  end
10
12
  end
data/config/routes.rb CHANGED
@@ -1,4 +1,4 @@
1
1
  Rails.application.routes.draw do
2
2
  # Add your extension routes here
3
- match '/:locale/(*return_path)' => 'locale#set', :constraints => {:locale => /\b#{Spree::Config.get(:enabled_locales)}\b/}, :as => :change_locale
3
+ match '/:locale/(*return_path)' => 'locale#set', :constraints => {:locale => /\b#{I18n.enabled_locales.join('|')}\b/}, :as => :change_locale
4
4
  end
@@ -17,6 +17,8 @@ module SpreeChangeLocale
17
17
  Dir.glob(File.join(File.dirname(__FILE__), "../../app/overrides/*.rb")).each do |c|
18
18
  Rails.application.config.cache_classes ? require(c) : load(c)
19
19
  end
20
+
21
+ I18n.send :extend, I18nExtension
20
22
  end
21
23
 
22
24
  config.to_prepare &method(:activate).to_proc
@@ -0,0 +1,16 @@
1
+ module I18nExtension
2
+ def enabled_locales
3
+ @@enabled_locales ||= (Spree::Config.get(:enabled_locales) || get_locales || I18n.available_locales || [I18n.default_locale]).collect(&:intern)
4
+ end
5
+ def locale_enabled?(locale)
6
+ enabled_locales.include?(locale.intern)
7
+ end
8
+
9
+ private
10
+ # inspired by: https://github.com/greendog/spree_locales_dropdown
11
+ def get_locales
12
+ path = Rails.root.join('config', 'locales')
13
+ items = Dir.glob(Rails.root.join('config', 'locales', '*.yml')).collect { |filename| File.basename(filename.gsub(/\.yml$/, '')) }
14
+ items unless items.blank?
15
+ end
16
+ end
@@ -0,0 +1,28 @@
1
+ module SpreeChangeLocale
2
+ module InstanceMethods
3
+ def enabled_locales
4
+ @enabled_locales ||= (Spree::Config.get(:enabled_locales) || get_locales || I18n.available_locales || [I18n.default_locale]).collect(&:intern)
5
+ end
6
+
7
+ # inspired by: https://github.com/greendog/spree_locales_dropdown
8
+ def get_locales
9
+ path = Rails.root.join('config', 'locales')
10
+ items = Dir.glob(Rails.root.join('config', 'locales', '*.yml')).collect { |filename| File.basename(filename.gsub(/\.yml$/, '')) }
11
+ items unless items.blank?
12
+ # Dir.glob(Rails.root.join('config', 'locales', '*.yml')).inject([]) do |mem, filename|
13
+ # locale_file = YAML.load_file(path.join(filename))
14
+ # str = File.basename(filename.gsub(/\.yml$/, ''))
15
+ # str = str.gsub("_spree", "")
16
+ # mem << str if locale_file.has_key?(str)
17
+ #
18
+ # mem
19
+ # end
20
+ end
21
+ end
22
+
23
+ module Methods
24
+ class << self
25
+ include InstanceMethods
26
+ end
27
+ end
28
+ end
@@ -1,3 +1,4 @@
1
1
  require 'spree_core'
2
2
  require 'spree_change_locale/engine'
3
+ require 'spree_change_locale/i18n_extension'
3
4
 
@@ -1,5 +1,26 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe LocaleController do
4
+ # it "should use Spree::BaseController" do
5
+ # controller.should be_an_instance_of(Spree::BaseController)
6
+ # end
4
7
 
8
+ it "should redirect to index with session[:locale] set to :es" do
9
+ session[:locale].should be_nil
10
+ get 'set', :locale => 'es'
11
+ session[:locale].should == :es
12
+ response.should redirect_to(root_path)
13
+ end
14
+
15
+ it "should redirect to index with session[:locale] set to :en and not :ru" do
16
+ get 'set', :locale => 'ru'
17
+ session[:locale].should_not == :ru
18
+ session[:locale].should == :en
19
+ response.should redirect_to(root_path)
20
+ end
21
+
22
+ it "should give url for Spanish" do
23
+ { :get => "/es" }.should route_to(:controller => "locale", :action => "set", :locale => "es")
24
+ { :get => "/ru" }.should_not route_to(:controller => "locale", :action => "set", :locale => "es")
25
+ end
5
26
  end
@@ -11,5 +11,16 @@ require 'spec_helper'
11
11
  # end
12
12
  # end
13
13
  describe LocaleHelper do
14
- pending "add some examples to (or delete) #{__FILE__}"
14
+ it "should return Spanish" do
15
+ locale_name(:es).should == 'Spanish'
16
+ end
17
+ it "should not return Russian" do
18
+ locale_name(:ru).should_not == 'Russian'
19
+ end
20
+ it "should link to Spanish locale" do
21
+ link_to_locale(:es).should == '<a href="/es">Spanish</a>'
22
+ end
23
+ it "should not link to Russian locale" do
24
+ link_to_locale(:ru).should_not == '<a href="/ru">translation missing: ru.this_file_language</a>'
25
+ end
15
26
  end
data/spec/spec_helper.rb CHANGED
@@ -3,7 +3,7 @@ ENV["RAILS_ENV"] = "test"
3
3
 
4
4
 
5
5
  require File.expand_path("../dummy/config/environment.rb", __FILE__)
6
-
6
+ require 'http_accept_language'
7
7
 
8
8
  require 'rspec/rails'
9
9
 
@@ -2,7 +2,7 @@
2
2
  Gem::Specification.new do |s|
3
3
  s.platform = Gem::Platform::RUBY
4
4
  s.name = 'spree_change_locale'
5
- s.version = '0.4'
5
+ s.version = '0.5'
6
6
  s.summary = 'Easy locale change for Spree'
7
7
  s.description = 'Easy locale change for Spree'
8
8
  s.required_ruby_version = '>= 1.8.7'
@@ -18,7 +18,7 @@ Gem::Specification.new do |s|
18
18
  s.requirements << 'none'
19
19
 
20
20
  s.add_dependency 'spree_core', '>= 0.70.0'
21
- # s.add_dependency 'http_accept_language', '~> 1.0'
21
+ s.add_dependency 'http_accept_language', '~> 1.0'
22
22
  s.add_development_dependency 'rspec-rails'
23
23
  end
24
24
 
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: spree_change_locale
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: "0.4"
5
+ version: "0.5"
6
6
  platform: ruby
7
7
  authors:
8
8
  - humancopy
@@ -24,16 +24,27 @@ dependencies:
24
24
  type: :runtime
25
25
  version_requirements: *id001
26
26
  - !ruby/object:Gem::Dependency
27
- name: rspec-rails
27
+ name: http_accept_language
28
28
  prerelease: false
29
29
  requirement: &id002 !ruby/object:Gem::Requirement
30
+ none: false
31
+ requirements:
32
+ - - ~>
33
+ - !ruby/object:Gem::Version
34
+ version: "1.0"
35
+ type: :runtime
36
+ version_requirements: *id002
37
+ - !ruby/object:Gem::Dependency
38
+ name: rspec-rails
39
+ prerelease: false
40
+ requirement: &id003 !ruby/object:Gem::Requirement
30
41
  none: false
31
42
  requirements:
32
43
  - - ">="
33
44
  - !ruby/object:Gem::Version
34
45
  version: "0"
35
46
  type: :development
36
- version_requirements: *id002
47
+ version_requirements: *id003
37
48
  description: Easy locale change for Spree
38
49
  email: info@humancopy.net
39
50
  executables: []
@@ -65,6 +76,8 @@ files:
65
76
  - lib/generators/spree_change_locale/install/install_generator.rb
66
77
  - lib/spree_change_locale.rb
67
78
  - lib/spree_change_locale/engine.rb
79
+ - lib/spree_change_locale/i18n_extension.rb
80
+ - lib/spree_change_locale/instance_methods.rb
68
81
  - script/rails
69
82
  - spec/controllers/locale_controller_spec.rb
70
83
  - spec/helpers/locale_helper_spec.rb