From 242b74319bc15585ca32cd6f252107118f05c5c0 Mon Sep 17 00:00:00 2001 From: AntonioTerceiro Date: Wed, 6 Feb 2008 14:58:02 +0000 Subject: [PATCH] ActionItem137: displaying a list of languages to switch languages instead of the flags (like e.g. gnu.org or debian.org) --- app/controllers/application.rb | 14 ++++++++++++++ app/helpers/language_helper.rb | 6 +----- app/views/layouts/application.rhtml | 5 ++++- app/views/shared/language_chooser.rhtml | 7 +++++++ config/environment.rb | 7 ++++--- lib/noosfero.rb | 4 ++++ public/stylesheets/common.css | 4 ++++ test/functional/application_controller_test.rb | 1 + test/functional/search_controller_test.rb | 2 +- test/integration/locale_setting_test.rb | 26 ++++++++++++++++++++++++++ 10 files changed, 66 insertions(+), 10 deletions(-) create mode 100644 app/views/shared/language_chooser.rhtml create mode 100644 test/integration/locale_setting_test.rb diff --git a/app/controllers/application.rb b/app/controllers/application.rb index 212c008..1d2d454 100644 --- a/app/controllers/application.rb +++ b/app/controllers/application.rb @@ -24,7 +24,9 @@ class ApplicationController < ActionController::Base # Be sure to include AuthenticationSystem in Application Controller instead include AuthenticatedSystem include PermissionCheck + init_gettext 'noosfero' + before_init_gettext :set_locale include NeedsProfile @@ -60,4 +62,16 @@ class ApplicationController < ActionController::Base current_user.person if logged_in? end + def set_locale + locale = cookies[:locale] + unless params[:locale].blank? + locale = params[:locale] + end + + if locale + cookies[:locale] = locale + GetText.locale = locale + end + end + end diff --git a/app/helpers/language_helper.rb b/app/helpers/language_helper.rb index 1628dc2..26e867b 100644 --- a/app/helpers/language_helper.rb +++ b/app/helpers/language_helper.rb @@ -1,10 +1,6 @@ module LanguageHelper def language code = GetText.locale.to_s.downcase - if code == 'en_us' - 'en' - else - code - end + (code == 'en_us') ? 'en' : code end end diff --git a/app/views/layouts/application.rhtml b/app/views/layouts/application.rhtml index 920aa10..1cfe2c0 100644 --- a/app/views/layouts/application.rhtml +++ b/app/views/layouts/application.rhtml @@ -117,8 +117,11 @@ diff --git a/app/views/shared/language_chooser.rhtml b/app/views/shared/language_chooser.rhtml new file mode 100644 index 0000000..28847d4 --- /dev/null +++ b/app/views/shared/language_chooser.rhtml @@ -0,0 +1,7 @@ +<% Noosfero.locales.each do |locale_code,locale_name| %> + <% if GetText.locale.to_s == locale_code %> + <%= locale_name %> + <% else %> + <%= link_to locale_name, { :locale => locale_code }%> + <% end %> +<% end %> diff --git a/config/environment.rb b/config/environment.rb index 53a9df3..df95e39 100644 --- a/config/environment.rb +++ b/config/environment.rb @@ -69,9 +69,10 @@ end require 'gettext/rails' -Localist.supported_locales = %w[en-US pt-BR] -Localist.default_locale = "pt-BR" -Localist.callback = lambda { |l| GetText.locale= l } +Noosfero.locales = { + 'en' => 'English', + 'pt_BR' => 'Português Brasileiro', +} Tag.hierarchical = true diff --git a/lib/noosfero.rb b/lib/noosfero.rb index 5a94a07..78f140e 100644 --- a/lib/noosfero.rb +++ b/lib/noosfero.rb @@ -16,6 +16,10 @@ module Noosfero Regexp.new(items.blank? ? '' : ('(' + items + ')')) end + class << self + attr_accessor :locales + end + private def self.controllers_in_directory(dir) diff --git a/public/stylesheets/common.css b/public/stylesheets/common.css index 6c747a0..51d3e05 100644 --- a/public/stylesheets/common.css +++ b/public/stylesheets/common.css @@ -429,3 +429,7 @@ body.category4 #content h4, body.category4 #content h5, body.category4 #content color: #AAA; } +#language-chooser { + margin-bottom: 1em; + text-align: center; +} diff --git a/test/functional/application_controller_test.rb b/test/functional/application_controller_test.rb index 94fafd5..16209f6 100644 --- a/test/functional/application_controller_test.rb +++ b/test/functional/application_controller_test.rb @@ -118,4 +118,5 @@ class ApplicationControllerTest < Test::Unit::TestCase get :index assert_no_tag :tag => 'div', :attributes => { :id => 'boxes', :class => 'boxes' } end + end diff --git a/test/functional/search_controller_test.rb b/test/functional/search_controller_test.rb index e578f87..cee8ef0 100644 --- a/test/functional/search_controller_test.rb +++ b/test/functional/search_controller_test.rb @@ -22,7 +22,7 @@ class SearchControllerTest < Test::Unit::TestCase end should 'filter stop words' do - Locale.current = Locale::Object.new('pt_BR') + GetText.locale = 'pt_BR' get 'index', :query => 'a carne da vaca' assert_response :success assert_template 'index' diff --git a/test/integration/locale_setting_test.rb b/test/integration/locale_setting_test.rb new file mode 100644 index 0000000..ddbc6c6 --- /dev/null +++ b/test/integration/locale_setting_test.rb @@ -0,0 +1,26 @@ +require "#{File.dirname(__FILE__)}/../test_helper" + +class LocaleSettingTest < ActionController::IntegrationTest + + should 'set locale properly' do + + # set locale to pt_BR + get '/', :locale => 'pt_BR' + assert_equal 'pt_BR', GetText.locale.to_s + + # locale is kept + get '/' + assert_equal 'pt_BR', GetText.locale.to_s + + # changing back + get '/', :locale => 'en' + assert_equal 'en', GetText.locale.to_s + + # locale is kept again + get '/' + assert_equal 'en', GetText.locale.to_s + + end + + +end -- libgit2 0.21.2