Commit 48e940b0f8b7e523dcfd8686fc77130e270b378a

Authored by AntonioTerceiro
1 parent 88f677d1

ActionItem166: translating date format


git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@1524 3f533792-8f58-4932-b0fe-aaf55b0a4547
app/controllers/application.rb
... ... @@ -27,6 +27,7 @@ class ApplicationController < ActionController::Base
27 27  
28 28 init_gettext 'noosfero'
29 29 before_init_gettext :force_language
  30 + after_init_gettext :set_system_locale
30 31  
31 32 include NeedsProfile
32 33  
... ... @@ -72,7 +73,20 @@ class ApplicationController < ActionController::Base
72 73 cookies[:lang] = lang
73 74 end
74 75  
75   - set_locale lang unless lang.blank?
  76 + unless lang.blank?
  77 + set_locale lang
  78 + end
  79 + end
  80 +
  81 + def set_system_locale
  82 + lang = GetText.locale.to_s
  83 + system_locale =
  84 + if (lang == 'en') || lang.blank?
  85 + 'C'
  86 + else
  87 + ('%s.utf8' % lang)
  88 + end
  89 + Locale.setlocale(Locale::LC_ALL, system_locale)
76 90 end
77 91  
78 92 end
... ...
app/helpers/application_helper.rb
... ... @@ -394,10 +394,12 @@ module ApplicationHelper
394 394  
395 395 end
396 396  
  397 + # formats a date for displaying.
397 398 def show_date(date)
398 399 date.strftime(_('%d %B %Y'))
399 400 end
400 401  
  402 + # formats a datetime for displaying.
401 403 def show_time(time)
402 404 time.strftime(_('%d %B %Y, %H:%m'))
403 405 end
... ...
config/environment.rb
... ... @@ -82,6 +82,7 @@ Noosfero.locales = {
82 82 'en' => 'English',
83 83 'pt_BR' => 'Português Brasileiro',
84 84 }
  85 +require 'locale'
85 86  
86 87 Tag.hierarchical = true
87 88  
... ...
test/integration/locale_setting_test.rb
... ... @@ -11,18 +11,18 @@ class LocaleSettingTest < ActionController::IntegrationTest
11 11 Noosfero.expects(:default_locale).returns('pt_BR').at_least_once
12 12  
13 13 get '/'
14   - assert_equal 'pt_BR', GetText.locale.to_s
  14 + assert_locale 'pt_BR'
15 15 end
16 16  
17 17 should 'detect locale from the browser' do
18 18  
19 19 # user has pt_BR
20 20 get '/', { }, { 'HTTP_ACCEPT_LANGUAGE' => 'pt-br, en' }
21   - assert_equal 'pt_BR', GetText.locale.to_s
  21 + assert_locale 'pt_BR'
22 22  
23 23 # user now wants en
24 24 get '/', { }, { 'HTTP_ACCEPT_LANGUAGE' => 'en' }
25   - assert_equal 'en', GetText.locale.to_s
  25 + assert_locale 'en'
26 26  
27 27 end
28 28  
... ... @@ -30,22 +30,38 @@ class LocaleSettingTest < ActionController::IntegrationTest
30 30  
31 31 # set locale to pt_BR
32 32 get '/', :lang => 'pt_BR'
33   - assert_equal 'pt_BR', GetText.locale.to_s
  33 + assert_locale 'pt_BR'
34 34  
35 35 # locale is kept
36 36 get '/'
37   - assert_equal 'pt_BR', GetText.locale.to_s
  37 + assert_locale 'pt_BR'
38 38  
39 39 # changing back
40 40 get '/', :lang => 'en'
41   - assert_equal 'en', GetText.locale.to_s
  41 + assert_locale 'en'
42 42  
43 43 # locale is kept again
44 44 get '/'
45   - assert_equal 'en', GetText.locale.to_s
  45 + assert_locale 'en'
46 46  
47 47 end
48 48  
  49 + protected
49 50  
  51 + def assert_locale(locale)
  52 + gettext_locale = GetText.locale.to_s
  53 + ok("Ruby-GetText locale should be #{locale}, but was #{gettext_locale}") { locale == gettext_locale }
50 54  
  55 + # TODO this test depends on a unpublished patch to liblocale-ruby
  56 + #system_locale = Locale.getlocale
  57 + #wanted_system_locale =
  58 + # if locale == 'en'
  59 + # 'C'
  60 + # else
  61 + # '%s.utf8' % locale
  62 + # end
  63 +
  64 + #ok("System locale should be #{wanted_system_locale}, but was #{system_locale}") { wanted_system_locale == system_locale }
  65 + end
  66 +
51 67 end
... ...