Permalink
Browse files

Improved support to rails testing framework, now including integratio…

…n tests.

Simplified integration on initializers and test_helper
Sample prefixes changed: 'en-US' -> 'en' and so on.
  • Loading branch information...
1 parent c14cc30 commit 0f4d3e8fcdfedc41425384062ac9dd9969cc390e @raul committed Feb 3, 2009
View
@@ -27,7 +27,7 @@ Let's start with a tiny example. Of course you need to define your routes first,
3) Append a line to your routes.rb file to activate the translations. If you loaded the translations file with
your other I18n translations files, the line will be:
- ActionController::Routing::Translator.i18n('es-ES')
+ ActionController::Routing::Translator.i18n('es')
and if you want to keep the file separated (e.g: config/i18n-routes.yml), the line to append is:
@@ -37,7 +37,7 @@ You can see it working by executing `rake routes` on the shell:
contact_es_es_path /es-ES/contacto {:locale=>"es-ES", :controller=>"contact", :action=>"index"}
- contact_en_us_path /contact {:locale=>"en-US", :controller=>"contact", :action=>"index"}
+ contact_en_us_path /contact {:locale=>"'en'", :controller=>"contact", :action=>"index"}
As we can see, a new spanish route has been setted up and a `locale` parameter has been added to the routes.
View
@@ -13,9 +13,7 @@ module Translator
mattr_accessor :locale_param_key
@@locale_param_key = :locale # set to :locale for params[:locale]
- mattr_accessor :original_routes
-
- mattr_accessor :dictionaries
+ mattr_accessor :original_routes, :original_named_routes, :original_names, :dictionaries
def self.translate
init_dictionaries
@@ -72,40 +70,31 @@ def self.locale_suffix_code
'locale.to_s.underscore'
end
- def self.add_locale_suffix_helper
- helper_code = <<-HELPER_CODE
- def locale_suffix(locale)
- #{locale_suffix_code}
- end
- HELPER_CODE
- [ActionController::Base, ActionView::Base].each { |d| d.module_eval(helper_code) }
- end
-
class_eval <<-FOO
def self.locale_suffix(locale)
#{self.locale_suffix_code}
end
FOO
-
def self.translate_current_routes
-
- # reset routes
- @@original_routes ||= Routes.routes.dup
- old_routes = Routes.routes.dup # Array [routeA, routeB, ...]
- old_names = Routes.named_routes.routes.dup # Hash {:name => :route}
+
+ RAILS_DEFAULT_LOGGER.info "Translating routes (default locale: #{default_locale})" if defined? RAILS_DEFAULT_LOGGER
+
+ @@original_routes = Routes.routes.dup # Array [routeA, routeB, ...]
+ @@original_named_routes = Routes.named_routes.routes.dup # Hash {:name => :route}
+ @@original_names = @@original_named_routes.keys
+
Routes.clear!
new_routes = []
new_named_routes = {}
- old_routes.each do |old_route|
+ @@original_routes.each do |old_route|
- old_name = old_names.index(old_route)
+ old_name = @@original_named_routes.index(old_route)
# process and add the translated ones
trans_routes, trans_named_routes = translate_route(old_route, old_name)
if old_name
new_named_routes.merge! trans_named_routes
- generate_helpers(old_name)
end
new_routes.concat(trans_routes)
@@ -115,15 +104,16 @@ def self.translate_current_routes
Routes.routes = new_routes
new_named_routes.each { |name, r| Routes.named_routes.add name, r }
- add_locale_suffix_helper
-
+ @@original_names.each{ |old_name| add_untranslated_helpers_to_controllers_and_views(old_name) }
end
- def self.generate_helpers(old_name)
+ # The untranslated helper (root_path instead root_en_path) redirects according to the current locale
+ def self.add_untranslated_helpers_to_controllers_and_views(old_name)
+
['path', 'url'].each do |suffix|
new_helper_name = "#{old_name}_#{suffix}"
def_new_helper = <<-DEF_NEW_HELPER
- def #{new_helper_name}(*args)
+ def #{new_helper_name}(*args)
send("#{old_name}_\#{locale_suffix(I18n.locale)}_#{suffix}", *args)
end
DEF_NEW_HELPER
@@ -209,20 +199,39 @@ def self.translate_route(route, route_name = nil)
end
end
-
-# Add set_locale_from_url to controllers
+# Add set_locale_from_url and locale_suffix to controllers
module ActionController
class Base
private
-
- def set_locale_from_url
- I18n.locale = params[ActionController::Routing::Translator.locale_param_key]
- default_url_options({ActionController::Routing::Translator => I18n.locale })
- end
+
+ def set_locale_from_url
+ I18n.locale = params[ActionController::Routing::Translator.locale_param_key]
+ default_url_options({ActionController::Routing::Translator => I18n.locale })
+ end
+
+ def locale_suffix(locale)
+ eval ActionController::Routing::Translator.locale_suffix_code
+ end
end
end
+# Add locale_suffix to views
+module ActionView
+
+ class Base
+
+ private
+
+ def locale_suffix(locale)
+ eval ActionController::Routing::Translator.locale_suffix_code
+ end
+
+ end
+
+end
+
+
@@ -0,0 +1,23 @@
+# monkeypatch I18n to get the available locales
+# (not strictly needed to use translate_routes, but recommended anyway)
+module I18n
+ class << self
+ def available_locales
+ backend.available_locales
+ end
+ end
+
+ module Backend
+ class Simple
+ def available_locales
+ init_translations unless initialized?
+ translations.keys
+ end
+ end
+ end
+end
+
+# load translation files from RAILS_ROOT/locales
+[:rb, :yml].each do |format|
+ I18n.load_path = Dir[File.join(RAILS_ROOT, 'locales', '*.{rb,yml}') ]
+end
@@ -0,0 +1,39 @@
+require 'test_help'
+
+# Author: Raul Murciano [http://raul.murciano.net] for Domestika [http://domestika.org]
+# Copyright (c) 2007, Released under the MIT license (see MIT-LICENSE)
+
+# Include default lang on your test requests (test requests doesn't support default_url_options):
+module ActionController
+ module TestProcess
+ unless method_defined?(:process_without_default_language)
+ def process_with_default_language(action, parameters = nil, session = nil, flash = nil)
+ lang_pair = {:locale, I18n.default_locale.to_s}
+ parameters = lang_pair.merge(parameters) rescue lang_pair
+ process_without_default_language(action, parameters, session, flash)
+ end
+
+ alias :process_without_default_language :process
+ alias :process :process_with_default_language
+ end
+ end
+end
+
+# Add untranslated helper for named routes to integration tests
+module ActionController
+ module Integration
+ class Session
+ ['path', 'url'].each do |suffix|
+ ActionController::Routing::Translator.original_names.each do |old_name|
+ new_helper_name = "#{old_name}_#{suffix}"
+ def_new_helper = <<-DEF_NEW_HELPER
+ def #{new_helper_name}(*args)
+ send("#{old_name}_#{ActionController::Routing::Translator.locale_suffix(I18n.default_locale)}_#{suffix}", *args)
+ end
+ DEF_NEW_HELPER
+ eval def_new_helper
+ end
+ end
+ end
+ end
+end
View
@@ -1,5 +1,5 @@
-"es-ES":
+es:
people: gente
-"en-US":
+en:
Oops, something went wrong.

0 comments on commit 0f4d3e8

Please sign in to comment.