myabc / merb_global

Localization (L10n) and Internationalization (i18n) support for the Merb MVC Framework

This URL has Read+Write access

fbettag (author)
Tue Sep 01 05:33:09 -0700 2009
myabc (committer)
Tue Sep 01 05:33:09 -0700 2009
commit  6e58d387d700322de45dd5fff3f63cd446793c53
tree    33fc4fe028e981f9384dcbbdfc0fa75f4d9dc62a
parent  1ffc3f880a9c3a3edd3d3a57a97f23a1425b4771
merb_global / spec / controller_spec.rb
100644 96 lines (84 sloc) 3.139 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
require 'spec_helper'
 
class TestController < Merb::Controller
  def index
    'index'
  end
end
 
class FrTestController < Merb::Controller
  language {'fr'}
  def index
    "index"
  end
end
 
class SettableTestController < Merb::Controller
  attr_accessor :current_lang
  language {@current_lang}
  def index
    'index'
  end
end
 
describe Merb::Controller do
  it 'should set language to english by default' do
    controller = dispatch_to(TestController, :index) do |controller|
      controller.request.env.delete 'HTTP_ACCEPT_LANGUAGE'
    end
    Merb::Global::Locale.current.should == Merb::Global::Locale.new('en')
  end
 
  it 'should set language according to the preferences' do
    Merb::Global::Locale.stubs(:support?).returns(true)
    controller = dispatch_to(TestController, :index) do |controller|
      controller.request.env['HTTP_ACCEPT_LANGUAGE'] = 'fr'
    end
    Merb::Global::Locale.current.should == Merb::Global::Locale.new('fr')
  end
 
  it 'should take the weights into account' do
    de = Merb::Global::Locale.new('de')
    Merb::Global.stubs(:config).with('locales', ['en']).returns(['de', 'es'])
    controller = dispatch_to(TestController, :index) do |controller|
      controller.request.env['HTTP_ACCEPT_LANGUAGE'] =
        'de;q=0.8,en;q=1.0,es;q=0.6'
    end
    Merb::Global::Locale.current.should == de
  end
 
  it 'should assume 1.0 as default weight' do
    it = Merb::Global::Locale.new('it')
    Merb::Global::Locale.stubs(:support?).returns(true)
    controller = dispatch_to(TestController, :index) do |controller|
      controller.request.env['HTTP_ACCEPT_LANGUAGE'] = 'it,en;q=0.7'
    end
    Merb::Global::Locale.current.should == it
  end
 
  it 'should choose language if \'*\' given' do
    fr = Merb::Global::Locale.new('fr')
    en = Merb::Global::Locale.new('en')
    Merb::Global.stubs(:config).with('locales', ['en']).returns(['en','fr'])
    controller = dispatch_to(TestController, :index) do |controller|
      controller.request.env['HTTP_ACCEPT_LANGUAGE'] = '*,en;q=0.7'
    end
    Merb::Global::Locale.current.should == fr
  end
 
  it "should have overriden settings by language block" do
    en = Merb::Global::Locale.new('en')
    fr = Merb::Global::Locale.new('fr')
    controller = dispatch_to(FrTestController, :index) do |controller|
      controller.request.env['HTTP_ACCEPT_LANGUAGE'] = 'en'
    end
    Merb::Global::Locale.current.should == fr
  end
 
  it 'should evaluate in the object context' do
    fr = Merb::Global::Locale.new('fr')
    controller = dispatch_to(SettableTestController, :index) do |controller|
      controller.current_lang = 'fr'
      controller.request.env['HTTP_ACCEPT_LANGUAGE'] = 'en'
    end
    Merb::Global::Locale.current.should == fr
  end
 
  it 'should fallback to lang if lang_REGION is not supported' do
    pt = Merb::Global::Locale.new('pt')
    Merb::Global.stubs(:config).with('locales', ['en']).returns(['pt'])
    controller = dispatch_to(TestController, :index) do |controller|
      controller.request.env['HTTP_ACCEPT_LANGUAGE'] = 'es-ES,pt-BR;q=0.7'
    end
    Merb::Global::Locale.current.should == pt
  end
end