Commit 48e940b0f8b7e523dcfd8686fc77130e270b378a
1 parent
88f677d1
Exists in
master
and in
29 other branches
ActionItem166: translating date format
git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@1524 3f533792-8f58-4932-b0fe-aaf55b0a4547
Showing
4 changed files
with
41 additions
and
8 deletions
Show diff stats
app/controllers/application.rb
| @@ -27,6 +27,7 @@ class ApplicationController < ActionController::Base | @@ -27,6 +27,7 @@ class ApplicationController < ActionController::Base | ||
| 27 | 27 | ||
| 28 | init_gettext 'noosfero' | 28 | init_gettext 'noosfero' |
| 29 | before_init_gettext :force_language | 29 | before_init_gettext :force_language |
| 30 | + after_init_gettext :set_system_locale | ||
| 30 | 31 | ||
| 31 | include NeedsProfile | 32 | include NeedsProfile |
| 32 | 33 | ||
| @@ -72,7 +73,20 @@ class ApplicationController < ActionController::Base | @@ -72,7 +73,20 @@ class ApplicationController < ActionController::Base | ||
| 72 | cookies[:lang] = lang | 73 | cookies[:lang] = lang |
| 73 | end | 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 | end | 90 | end |
| 77 | 91 | ||
| 78 | end | 92 | end |
app/helpers/application_helper.rb
| @@ -394,10 +394,12 @@ module ApplicationHelper | @@ -394,10 +394,12 @@ module ApplicationHelper | ||
| 394 | 394 | ||
| 395 | end | 395 | end |
| 396 | 396 | ||
| 397 | + # formats a date for displaying. | ||
| 397 | def show_date(date) | 398 | def show_date(date) |
| 398 | date.strftime(_('%d %B %Y')) | 399 | date.strftime(_('%d %B %Y')) |
| 399 | end | 400 | end |
| 400 | 401 | ||
| 402 | + # formats a datetime for displaying. | ||
| 401 | def show_time(time) | 403 | def show_time(time) |
| 402 | time.strftime(_('%d %B %Y, %H:%m')) | 404 | time.strftime(_('%d %B %Y, %H:%m')) |
| 403 | end | 405 | end |
config/environment.rb
test/integration/locale_setting_test.rb
| @@ -11,18 +11,18 @@ class LocaleSettingTest < ActionController::IntegrationTest | @@ -11,18 +11,18 @@ class LocaleSettingTest < ActionController::IntegrationTest | ||
| 11 | Noosfero.expects(:default_locale).returns('pt_BR').at_least_once | 11 | Noosfero.expects(:default_locale).returns('pt_BR').at_least_once |
| 12 | 12 | ||
| 13 | get '/' | 13 | get '/' |
| 14 | - assert_equal 'pt_BR', GetText.locale.to_s | 14 | + assert_locale 'pt_BR' |
| 15 | end | 15 | end |
| 16 | 16 | ||
| 17 | should 'detect locale from the browser' do | 17 | should 'detect locale from the browser' do |
| 18 | 18 | ||
| 19 | # user has pt_BR | 19 | # user has pt_BR |
| 20 | get '/', { }, { 'HTTP_ACCEPT_LANGUAGE' => 'pt-br, en' } | 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 | # user now wants en | 23 | # user now wants en |
| 24 | get '/', { }, { 'HTTP_ACCEPT_LANGUAGE' => 'en' } | 24 | get '/', { }, { 'HTTP_ACCEPT_LANGUAGE' => 'en' } |
| 25 | - assert_equal 'en', GetText.locale.to_s | 25 | + assert_locale 'en' |
| 26 | 26 | ||
| 27 | end | 27 | end |
| 28 | 28 | ||
| @@ -30,22 +30,38 @@ class LocaleSettingTest < ActionController::IntegrationTest | @@ -30,22 +30,38 @@ class LocaleSettingTest < ActionController::IntegrationTest | ||
| 30 | 30 | ||
| 31 | # set locale to pt_BR | 31 | # set locale to pt_BR |
| 32 | get '/', :lang => 'pt_BR' | 32 | get '/', :lang => 'pt_BR' |
| 33 | - assert_equal 'pt_BR', GetText.locale.to_s | 33 | + assert_locale 'pt_BR' |
| 34 | 34 | ||
| 35 | # locale is kept | 35 | # locale is kept |
| 36 | get '/' | 36 | get '/' |
| 37 | - assert_equal 'pt_BR', GetText.locale.to_s | 37 | + assert_locale 'pt_BR' |
| 38 | 38 | ||
| 39 | # changing back | 39 | # changing back |
| 40 | get '/', :lang => 'en' | 40 | get '/', :lang => 'en' |
| 41 | - assert_equal 'en', GetText.locale.to_s | 41 | + assert_locale 'en' |
| 42 | 42 | ||
| 43 | # locale is kept again | 43 | # locale is kept again |
| 44 | get '/' | 44 | get '/' |
| 45 | - assert_equal 'en', GetText.locale.to_s | 45 | + assert_locale 'en' |
| 46 | 46 | ||
| 47 | end | 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 | end | 67 | end |