Commit 756e91dee4528a46f750e24877cc399715fe39ed

Authored by AntonioTerceiro
1 parent 242b7431

ActionItem137: adjustements in language choosing scheme


git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@1300 3f533792-8f58-4932-b0fe-aaf55b0a4547
app/controllers/application.rb
... ... @@ -26,7 +26,7 @@ class ApplicationController < ActionController::Base
26 26 include PermissionCheck
27 27  
28 28 init_gettext 'noosfero'
29   - before_init_gettext :set_locale
  29 + before_init_gettext :force_language
30 30  
31 31 include NeedsProfile
32 32  
... ... @@ -62,15 +62,11 @@ class ApplicationController < ActionController::Base
62 62 current_user.person if logged_in?
63 63 end
64 64  
65   - def set_locale
66   - locale = cookies[:locale]
67   - unless params[:locale].blank?
68   - locale = params[:locale]
69   - end
70   -
71   - if locale
72   - cookies[:locale] = locale
73   - GetText.locale = locale
  65 + def force_language
  66 + lang = params[:lang]
  67 + unless lang.blank?
  68 + cookies[:lang] = lang
  69 + GetText.locale = lang
74 70 end
75 71 end
76 72  
... ...
app/views/shared/language_chooser.rhtml
... ... @@ -2,6 +2,6 @@
2 2 <% if GetText.locale.to_s == locale_code %>
3 3 <strong><%= locale_name %></strong>
4 4 <% else %>
5   - <%= link_to locale_name, { :locale => locale_code }%>
  5 + <%= link_to locale_name, { :lang => locale_code }%>
6 6 <% end %>
7 7 <% end %>
... ...
test/integration/locale_setting_test.rb
... ... @@ -2,10 +2,27 @@ require &quot;#{File.dirname(__FILE__)}/../test_helper&quot;
2 2  
3 3 class LocaleSettingTest < ActionController::IntegrationTest
4 4  
5   - should 'set locale properly' do
  5 + def setup
  6 + # reset GetText before every test
  7 + GetText.locale = nil
  8 + end
  9 +
  10 + should 'detect locale from the browser' do
  11 +
  12 + # user has pt_BR
  13 + get '/', { }, { 'HTTP_ACCEPT_LANGUAGE' => 'pt-br, en' }
  14 + assert_equal 'pt_BR', GetText.locale.to_s
  15 +
  16 + # user now wants en
  17 + get '/', { }, { 'HTTP_ACCEPT_LANGUAGE' => 'en' }
  18 + assert_equal 'en', GetText.locale.to_s
  19 +
  20 + end
  21 +
  22 + should 'be able to force locale' do
6 23  
7 24 # set locale to pt_BR
8   - get '/', :locale => 'pt_BR'
  25 + get '/', :lang => 'pt_BR'
9 26 assert_equal 'pt_BR', GetText.locale.to_s
10 27  
11 28 # locale is kept
... ... @@ -13,7 +30,7 @@ class LocaleSettingTest &lt; ActionController::IntegrationTest
13 30 assert_equal 'pt_BR', GetText.locale.to_s
14 31  
15 32 # changing back
16   - get '/', :locale => 'en'
  33 + get '/', :lang => 'en'
17 34 assert_equal 'en', GetText.locale.to_s
18 35  
19 36 # locale is kept again
... ... @@ -23,4 +40,5 @@ class LocaleSettingTest &lt; ActionController::IntegrationTest
23 40 end
24 41  
25 42  
  43 +
26 44 end
... ...