diff options
18 files changed, 823 insertions, 0 deletions
diff --git a/config/i18n-routes.yml b/config/i18n-routes.yml new file mode 100644 index 000000000..1f9b3028c --- /dev/null +++ b/config/i18n-routes.yml @@ -0,0 +1,6 @@ +en: + # default from routes.rb +es: + search: busquedas + help: ayuda + about: sobre diff --git a/config/routes.rb b/config/routes.rb index 928bacf67..2d4220d2a 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -139,3 +139,8 @@ ActionController::Routing::Routes.draw do |map| # map.connect ':controller/service.wsdl', :action => 'wsdl' end +# XXX should do something like the following to load routes from separate files +# Dir.glob("config/routes_*yml").each do |f| +# ActionController::Routing::Translator.translate_from_file(f) +# end +ActionController::Routing::Translator.translate_from_file('config', 'i18n-routes.yml') diff --git a/vendor/plugins/translate_routes/.gitignore b/vendor/plugins/translate_routes/.gitignore new file mode 100644 index 000000000..943a463d9 --- /dev/null +++ b/vendor/plugins/translate_routes/.gitignore @@ -0,0 +1,4 @@ +.DS_Store +SampleApp/log/*.log +SampleApp/tmp/**/* +SampleApp/db/test.sqlite3
\ No newline at end of file diff --git a/vendor/plugins/translate_routes/ChangeLog b/vendor/plugins/translate_routes/ChangeLog new file mode 100755 index 000000000..75d0e76b1 --- /dev/null +++ b/vendor/plugins/translate_routes/ChangeLog @@ -0,0 +1,22 @@ +-- 0.98 + Accepted patch from hoelmer: Updated rake task to use I18n yaml format. +-- 0.97 + Accepted patch from Aitor Garay-Romero: root routes with prefix now doesn't set the locale parameter. + +-- rails2.2 branch -> master + +-- branch rails2.2 v0.9 (Oct 27th 2008) + * Developed after Rails2.2rc1 release, with i18n support. Beta, not backward compatible with Rails < 2.2 + +-- 0.96.1 (Aug 5th 2008) + * Fixed by Mathieu Fosse: helpers didn't worked as expected when locale_param_key is undefined + +-- 0.96 (Jun 10th 2008) + * Added update_yaml task, suggested by Francesc Esplugas + +-- 0.95 (Jan 21st 2008) + * Still beta version + * Added yaml files support for dictionaries + +-- 0.9 (Dec 27th 2007) + * Beta version diff --git a/vendor/plugins/translate_routes/MIT-LICENSE b/vendor/plugins/translate_routes/MIT-LICENSE new file mode 100755 index 000000000..fbce523f9 --- /dev/null +++ b/vendor/plugins/translate_routes/MIT-LICENSE @@ -0,0 +1,20 @@ +Copyright (c) 2007 Raul Murciano [http://raul.murciano.net], Domestika INTERNET S.L. [http://domestika.org] + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/vendor/plugins/translate_routes/README.markdown b/vendor/plugins/translate_routes/README.markdown new file mode 100755 index 000000000..1c0d0bf25 --- /dev/null +++ b/vendor/plugins/translate_routes/README.markdown @@ -0,0 +1,99 @@ +TranslateRoutes +=============== + +This Rails plugin provides a simple way to translate your URLs to any number of languages, even on a fully working application. + +It works fine with all kind of routing definitions, including RESTful and named routes. +**Your current code will remain untouched**: your current routing code, helpers and links will be translated transparently - even in your tests. +(Un)installing it is a very clean and simple process, so why don't you give it a chance? ;) + +This version works only with Rails 2.2.x. You can find all available versions in [the wiki](wiki.github.com/raul/translate_routes). + +Sample application +------------------ +There is a [sample application](http://github.com/raul/translate_routes_demo/tree/master) which can be very useful to see how to integrate this plugin on your Rails application. The application itself includes all the required steps: 3 lines, an optional filter and a yaml translations file were used. + + +Quick start +----------- + +Let's start with a tiny example. Of course you need to define your routes first, e.g: + + ActionController::Routing::Routes.draw do |map| + map.contact 'contact', :controller => 'contact', :action => 'index' + end + +1) Download the plugin to your app's `/vendor/plugins` directory. + +2) Write your translations on a standard YAML file (e.g: i18n-routes.yml), including the locales and it translations pairs: + + es: + contact: contacto + + +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') + +and if you want to keep the file separated (e.g: config/i18n-routes.yml), the line to append is: + + ActionController::Routing::Translator.translate_from_file('config','i18n-routes.yml') + +You can see it working by executing `rake routes` on the shell: + + + contact_es_es_path /es-ES/contacto {:locale=>"es", :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. + +4) Include this filter in your ApplicationController: + + before_filter :set_locale_from_url + +Now your application recognizes the different routes and sets the `I18n.locale` value on your controllers, +but what about the routes generation? As you can see on the previous `rake routes` execution, the +`contact_es_es_path` and `contact_en_us_path` routing helpers have been generated and are +available in your controllers and views. Additionally, a `contact_path` helper has been generated, which +generates the routes according to the current request's locale. This way your link + +This means that if you use named routes **you don't need to modify your application links** because the routing helpers are automatically adapted to the current locale. + +5) Hey, but what about my tests? + +Of course, your functional and integration testing involves some requests. +The plugin includes some code to add a default locale parameter so they can remain untouched. +Append it to your `test_helper` and it will be applied. + +Documentation +------------- +You can find additional information in [the translate_routes' wiki](http://wiki.github.com/raul/translate_routes). + +Questions, suggestions, bug reports... +-------------------------------------- +Feedback, questions and comments will be always welcome at raul@murciano.net + +Credits +------- +* Main development: + * Raul Murciano <http://raul.murciano.net> - code + * Domestika INTERNET S.L <http://domestika.org> - incredible support, really nice people to work with! + +* Contributors: + * Aitor Garay-Romero + * hoelmer (sorry mate, I can't find your real name) + +Rails routing resources +----------------------- +* David Black's 'Rails Routing' ebook rocks! - 'Ruby for Rails' too, BTW. +* Obie Fernandez's 'The Rails Way' - the definitive RoR reference, great work Obie! +* As a part of the impressive Rails Guides set there is an [awesome document about rails routing](http://guides.rails.info/routing_outside_in.html) by Mike Gunderloy: + + +License +------- + Copyright (c) 2007 Released under the MIT license (see MIT-LICENSE) + Raul Murciano <http://raul.murciano.net> + Domestika INTERNET S.L. <http://domestika.org>
\ No newline at end of file diff --git a/vendor/plugins/translate_routes/Rakefile b/vendor/plugins/translate_routes/Rakefile new file mode 100755 index 000000000..5b0a29cf5 --- /dev/null +++ b/vendor/plugins/translate_routes/Rakefile @@ -0,0 +1,22 @@ +require 'rake' +require 'rake/testtask' +require 'rake/rdoctask' + +desc 'Default: run unit tests.' +task :default => :test + +desc 'Test the translate_routes plugin.' +Rake::TestTask.new(:test) do |t| + t.libs << 'lib' + t.pattern = 'test/**/*_test.rb' + t.verbose = true +end + +desc 'Generate documentation for the translate_routes plugin.' +Rake::RDocTask.new(:rdoc) do |rdoc| + rdoc.rdoc_dir = 'rdoc' + rdoc.title = 'TranslateRoutes' + rdoc.options << '--line-numbers' << '--inline-source' + rdoc.rdoc_files.include('README') + rdoc.rdoc_files.include('lib/**/*.rb') +end diff --git a/vendor/plugins/translate_routes/config/routes_en-US.yml b/vendor/plugins/translate_routes/config/routes_en-US.yml new file mode 100755 index 000000000..e69de29bb --- /dev/null +++ b/vendor/plugins/translate_routes/config/routes_en-US.yml diff --git a/vendor/plugins/translate_routes/config/routes_es-ES.yml b/vendor/plugins/translate_routes/config/routes_es-ES.yml new file mode 100755 index 000000000..b0b1b80c9 --- /dev/null +++ b/vendor/plugins/translate_routes/config/routes_es-ES.yml @@ -0,0 +1 @@ +people: gente
\ No newline at end of file diff --git a/vendor/plugins/translate_routes/init.rb b/vendor/plugins/translate_routes/init.rb new file mode 100755 index 000000000..9d09228c3 --- /dev/null +++ b/vendor/plugins/translate_routes/init.rb @@ -0,0 +1 @@ +require 'translate_routes'
\ No newline at end of file diff --git a/vendor/plugins/translate_routes/install.rb b/vendor/plugins/translate_routes/install.rb new file mode 100755 index 000000000..f7732d379 --- /dev/null +++ b/vendor/plugins/translate_routes/install.rb @@ -0,0 +1 @@ +# Install hook code here diff --git a/vendor/plugins/translate_routes/lib/translate_routes.rb b/vendor/plugins/translate_routes/lib/translate_routes.rb new file mode 100755 index 000000000..b5ba09b3d --- /dev/null +++ b/vendor/plugins/translate_routes/lib/translate_routes.rb @@ -0,0 +1,219 @@ +from_# Author: Raul Murciano [http://raul.murciano.net] for Domestika [http://domestika.org] +# Copyright (c) 2007, Released under the MIT license (see MIT-LICENSE) + +module ActionController + + module Routing + + module Translator + + mattr_accessor :prefix_on_default_locale + @@prefix_on_default_locale = false + + mattr_accessor :locale_param_key + @@locale_param_key = :locale # set to :locale for params[:locale] + + mattr_accessor :original_routes, :original_named_routes, :original_names, :dictionaries + + def self.translate + init_dictionaries + yield @@dictionaries + @using_i18n = false + Translator.translate_current_routes + end + + def self.translate_from_file(*path) + init_dictionaries + path = %w(locales routes.yml) if path.blank? + file_path = File.join(RAILS_ROOT, path) + yaml = YAML.load_file(file_path) + yaml.each_pair{ |k,v| @@dictionaries[k.to_s] = v || {} } + @using_i18n = false + Translator.translate_current_routes + end + + def self.i18n(*locales) + init_dictionaries + locales = I18n.available_locales if locales.blank? && I18n.respond_to?(:available_locales) + locales.each{ |locale| @@dictionaries[locale] = {} } + @using_i18n = true + Translator.translate_current_routes + end + + private + + def self.default_locale + I18n.default_locale.to_s + end + + def self.init_dictionaries + @@dictionaries = { default_locale => {} } + end + + def self.available_locales + @@dictionaries.keys.map(&:to_s).uniq + end + + def self.original_static_segments + static_segments = [] + (@@original_routes || Routes.routes).each do |r| + r.segments.select do |s| + static_segments << s.value if s.instance_of?(ActionController::Routing::StaticSegment) + end + end + static_segments.uniq.sort + end + + # code shared by translation and application helpers, + # it generates a suffix code for a given locale: en-US -> en_us + def self.locale_suffix_code + 'locale.to_s.underscore' + end + + class_eval <<-FOO + def self.locale_suffix(locale) + #{self.locale_suffix_code} + end + FOO + def self.translate_current_routes + + 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 = {} + + @@original_routes.each do |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 + end + + new_routes.concat(trans_routes) + + end + + Routes.routes = new_routes + new_named_routes.each { |name, r| Routes.named_routes.add name, r } + + @@original_names.each{ |old_name| add_untranslated_helpers_to_controllers_and_views(old_name) } + end + + # 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) + send("#{old_name}_\#{locale_suffix(I18n.locale)}_#{suffix}", *args) + end + DEF_NEW_HELPER + + [ActionController::Base, ActionView::Base, ActionMailer::Base].each { |d| d.module_eval(def_new_helper) } + ActionController::Routing::Routes.named_routes.helpers << new_helper_name.to_sym + end + end + + def self.add_prefix?(lang) + @@prefix_on_default_locale || lang != default_locale + end + + def self.translate_static_segment(segment, locale) + if @using_i18n + tmp = I18n.locale + I18n.locale = locale + value = I18n.t segment.value, :default => segment.value.dup + I18n.locale = tmp + else + value = @@dictionaries[locale][segment.value] || segment.value.dup + end + StaticSegment.new(value, :raw => segment.raw, :optional => segment.optional?) + end + + def self.locale_segments(orig, locale) + segments = [] + + if add_prefix?(locale) # initial prefix i.e: /en-US + divider = DividerSegment.new(orig.segments.first.value, :optional => false) # divider ('/') + static = StaticSegment.new(locale, :optional => false) # static ('en-US') + segments += [divider, static] + end + + orig.segments.each do |s| + if s.instance_of?(StaticSegment) + new_segment = translate_static_segment(s, locale) + else + new_segment = s.dup # just reference the original + end + segments << new_segment + end + segments + end + + def self.locale_requirements(orig, locale) + orig.requirements.merge(@@locale_param_key => locale) + end + + def self.translate_route_by_locale(orig, locale, orig_name=nil) + segments = locale_segments(orig, locale) + requirements = locale_requirements(orig, locale) + conditions = orig.conditions + + Route.new(segments, requirements, conditions).freeze + end + + def self.root_route?(route) + route.segments.length == 1 + end + + def self.translate_route(route, route_name = nil) + new_routes = [] + new_named_routes = {} + + if root_route?(route) && prefix_on_default_locale + # add the root route "as is" in addition to the translated versions + new_routes << route + new_named_routes[route_name] = route + end + + available_locales.each do |locale| + translated = translate_route_by_locale(route, locale, route_name) + new_routes << translated + locale_suffix = locale_suffix(locale) + new_named_routes["#{route_name}_#{locale_suffix}".to_sym] = translated if route_name + end + [new_routes, new_named_routes] + end + + end + + end +end + +# Add set_locale_from_url to controllers +ActionController::Base.class_eval do + private + def set_locale_from_url + I18n.locale = params[ActionController::Routing::Translator.locale_param_key] + default_url_options({ActionController::Routing::Translator => I18n.locale }) + end +end + +# Add locale_suffix to controllers, views and mailers +[ActionController::Base, ActionView::Base, ActionMailer::Base].map do |klass| + klass.class_eval do + private + def locale_suffix(locale) + eval ActionController::Routing::Translator.locale_suffix_code + end + end +end diff --git a/vendor/plugins/translate_routes/lib/translate_routes_i18n_available_locales.rb b/vendor/plugins/translate_routes/lib/translate_routes_i18n_available_locales.rb new file mode 100644 index 000000000..644483059 --- /dev/null +++ b/vendor/plugins/translate_routes/lib/translate_routes_i18n_available_locales.rb @@ -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
\ No newline at end of file diff --git a/vendor/plugins/translate_routes/lib/translate_routes_test_helper.rb b/vendor/plugins/translate_routes/lib/translate_routes_test_helper.rb new file mode 100644 index 000000000..5f589de55 --- /dev/null +++ b/vendor/plugins/translate_routes/lib/translate_routes_test_helper.rb @@ -0,0 +1,33 @@ +# Author: Raul Murciano [http://raul.murciano.net] for Domestika [http://domestika.org] +# Copyright (c) 2007, Released under the MIT license (see MIT-LICENSE) + +require 'test_help' + +# Include default lang on your test requests (test requests doesn't support default_url_options): +ActionController::TestProcess.class_eval do + 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 + +# Add untranslated helper for named routes to integration tests +ActionController::Integration::Session.class_eval do + ['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 diff --git a/vendor/plugins/translate_routes/tasks/translate_routes_tasks.rake b/vendor/plugins/translate_routes/tasks/translate_routes_tasks.rake new file mode 100755 index 000000000..f94d7c9e4 --- /dev/null +++ b/vendor/plugins/translate_routes/tasks/translate_routes_tasks.rake @@ -0,0 +1,38 @@ +config_path = File.expand_path(File.join(RAILS_ROOT, 'config')) +require File.join(config_path, 'environment') + +namespace :translate_routes do + + desc "Updates yaml translation files for the given languages" + task :update_yaml, :langs do |task, args| + segments = ActionController::Routing::Translator.original_static_segments + + if args[:langs].is_a?(String) + langs = args[:langs] + ' ' + ActionController::Routing::Translator.default_locale + langs.split.uniq.each do |lang| + + file_path = File.join(config_path, "routes_#{lang}.yml"); + + if File.exists?(file_path) + puts "Updating #{file_path}" + translations = YAML.load_file(file_path) + f = File.open(file_path,'w') + else + puts "Creating #{file_path}" + translations = {} + f = File.new(file_path, 'w') + end + + f.write "#{lang}:\n" + segments.each do |s| + translation = translations[lang][s] rescue '' + f.write " #{s}: #{translation}\n" + end + f.close + end + + else + puts 'Missing parameters, usage example: rake translate_routes:update_yaml["fr de es"]' + end + end +end diff --git a/vendor/plugins/translate_routes/test/locales/routes.yml b/vendor/plugins/translate_routes/test/locales/routes.yml new file mode 100644 index 000000000..380949823 --- /dev/null +++ b/vendor/plugins/translate_routes/test/locales/routes.yml @@ -0,0 +1,5 @@ +es: + people: gente + +en: +
\ No newline at end of file diff --git a/vendor/plugins/translate_routes/test/translate_routes_test.rb b/vendor/plugins/translate_routes/test/translate_routes_test.rb new file mode 100755 index 000000000..9b8a2dfcf --- /dev/null +++ b/vendor/plugins/translate_routes/test/translate_routes_test.rb @@ -0,0 +1,323 @@ +require 'test/unit' +require 'rubygems' + +%w(actionpack activesupport actionmailer).each{ |gem_lib| gem gem_lib, '2.2.2' } + +%w( activesupport actionpack actionmailer action_controller action_controller/test_case + action_controller/test_process action_controller/assertions + ).each{ |lib| require lib } + +plugin_root = File.join(File.dirname(__FILE__), '..') +require "#{plugin_root}/lib/translate_routes" +RAILS_ROOT = plugin_root + +class PeopleController < ActionController::Base; end + +class TranslateRoutesTest < Test::Unit::TestCase + + def setup + @controller = ActionController::Base.new + @view = ActionView::Base.new + end + + + # Unnamed routes with prefix on default locale: + + def test_unnamed_empty_route_with_prefix + ActionController::Routing::Routes.draw { |map| map.connect '', :controller => 'people', :action => 'index' } + config_default_locale_settings('en', true) + ActionController::Routing::Translator.translate { |t| t['en'] = {}; t['es'] = {'people' => 'gente'} } + + assert_routing '/es', :controller => 'people', :action => 'index', :locale => 'es' + assert_routing '/en', :controller => 'people', :action => 'index', :locale => 'en' + end + + def test_unnamed_root_route_with_prefix + ActionController::Routing::Routes.draw { |map| map.connect '/', :controller => 'people', :action => 'index'} + config_default_locale_settings('es', true) + ActionController::Routing::Translator.translate_from_file 'test', 'locales', 'routes.yml' + + assert_routing '/', :controller => 'people', :action => 'index' + assert_routing '/es', :controller => 'people', :action => 'index', :locale => 'es' + assert_routing '/en', :controller => 'people', :action => 'index', :locale => 'en' + end + + def test_unnamed_untranslated_route_with_prefix + ActionController::Routing::Routes.draw { |map| map.connect 'foo', :controller => 'people', :action => 'index' } + config_default_locale_settings('en', true) + ActionController::Routing::Translator.translate { |t| t['en'] = {}; t['es'] = {'people' => 'gente'} } + + assert_routing '/es/foo', :controller => 'people', :action => 'index', :locale => 'es' + assert_routing '/en/foo', :controller => 'people', :action => 'index', :locale => 'en' + end + + def test_unnamed_translated_route_on_default_locale_with_prefix + ActionController::Routing::Routes.draw { |map| map.people 'people', :controller => 'people', :action => 'index'} + config_default_locale_settings('es', true) + ActionController::Routing::Translator.translate { |t| t['en'] = {}; t['es'] = {'people' => 'gente'} } + + assert_routing '/es/gente', :controller => 'people', :action => 'index', :locale => 'es' + end + + def test_unnamed_translated_route_on_non_default_locale_with_prefix + ActionController::Routing::Routes.draw { |map| map.connect 'people', :controller => 'people', :action => 'index' } + config_default_locale_settings('en', true) + ActionController::Routing::Translator.translate { |t| t['en'] = {}; t['es'] = {'people' => 'gente'} } + + assert_routing '/es/gente', :controller => 'people', :action => 'index', :locale => 'es' + assert_routing '/en/people', :controller => 'people', :action => 'index', :locale => 'en' + end + + + # Unnamed routes without prefix on default locale: + + def test_unnamed_empty_route_without_prefix + ActionController::Routing::Routes.draw { |map| map.connect '', :controller => 'people', :action => 'index' } + config_default_locale_settings('en', false) + ActionController::Routing::Translator.translate { |t| t['en'] = {}; t['es'] = {'people' => 'gente'} } + + assert_routing '/es', :controller => 'people', :action => 'index', :locale => 'es' + assert_routing '/', :controller => 'people', :action => 'index', :locale => 'en' + end + + def test_unnamed_root_route_without_prefix + ActionController::Routing::Routes.draw { |map| map.connect '/', :controller => 'people', :action => 'index'} + config_default_locale_settings('es', false) + ActionController::Routing::Translator.translate_from_file 'test', 'locales', 'routes.yml' + + assert_routing '/', :controller => 'people', :action => 'index', :locale => 'es' + assert_routing '/en', :controller => 'people', :action => 'index', :locale => 'en' + assert_unrecognized_route '/es', :controller => 'people', :action => 'index', :locale => 'es' + end + + def test_unnamed_untranslated_route_without_prefix + ActionController::Routing::Routes.draw { |map| map.connect 'foo', :controller => 'people', :action => 'index'} + config_default_locale_settings('en', false) + ActionController::Routing::Translator.translate { |t| t['en'] = {}; t['es'] = {'people' => 'gente'} } + + assert_routing '/es/foo', :controller => 'people', :action => 'index', :locale => 'es' + assert_routing '/foo', :controller => 'people', :action => 'index', :locale => 'en' + end + + def test_unnamed_translated_route_on_default_locale_without_prefix + ActionController::Routing::Routes.draw { |map| map.people 'people', :controller => 'people', :action => 'index'} + config_default_locale_settings('es', false) + ActionController::Routing::Translator.translate { |t| t['en'] = {}; t['es'] = {'people' => 'gente'} } + + assert_routing '/en/people', :controller => 'people', :action => 'index', :locale => 'en' + assert_routing 'gente', :controller => 'people', :action => 'index', :locale => 'es' + end + + def test_unnamed_translated_route_on_non_default_locale_without_prefix + ActionController::Routing::Routes.draw { |map| map.people 'people', :controller => 'people', :action => 'index'} + config_default_locale_settings('en', false) + ActionController::Routing::Translator.translate { |t| t['en'] = {}; t['es'] = {'people' => 'gente'} } + + assert_routing '/es/gente', :controller => 'people', :action => 'index', :locale => 'es' + assert_routing '/people', :controller => 'people', :action => 'index', :locale => 'en' + end + + + # Named routes with prefix on default locale: + + def test_named_empty_route_with_prefix + ActionController::Routing::Routes.draw { |map| map.people '', :controller => 'people', :action => 'index' } + config_default_locale_settings('en', true) + ActionController::Routing::Translator.translate { |t| t['en'] = {}; t['es'] = {'people' => 'gente'} } + + assert_routing '/es', :controller => 'people', :action => 'index', :locale => 'es' + assert_routing '/en', :controller => 'people', :action => 'index', :locale => 'en' + assert_helpers_include :people_en, :people_es, :people + end + + def test_named_root_route_with_prefix + ActionController::Routing::Routes.draw { |map| map.root :controller => 'people', :action => 'index'} + config_default_locale_settings('es', true) + ActionController::Routing::Translator.translate_from_file 'test', 'locales', 'routes.yml' + + assert_routing '/', :controller => 'people', :action => 'index' + assert_routing '/es', :controller => 'people', :action => 'index', :locale => 'es' + assert_routing '/en', :controller => 'people', :action => 'index', :locale => 'en' + end + + def test_named_untranslated_route_with_prefix + ActionController::Routing::Routes.draw { |map| map.people 'foo', :controller => 'people', :action => 'index'} + config_default_locale_settings('en', true) + ActionController::Routing::Translator.translate { |t| t['en'] = {}; t['es'] = {'people' => 'gente'} } + + assert_routing '/es/foo', :controller => 'people', :action => 'index', :locale => 'es' + assert_routing '/en/foo', :controller => 'people', :action => 'index', :locale => 'en' + assert_helpers_include :people_en, :people_es, :people + end + + def test_named_translated_route_on_default_locale_with_prefix + ActionController::Routing::Routes.draw { |map| map.people 'people', :controller => 'people', :action => 'index'} + config_default_locale_settings('es', true) + ActionController::Routing::Translator.translate { |t| t['en'] = {}; t['es'] = {'people' => 'gente'} } + + assert_routing '/en/people', :controller => 'people', :action => 'index', :locale => 'en' + assert_routing '/es/gente', :controller => 'people', :action => 'index', :locale => 'es' + assert_helpers_include :people_en, :people_es, :people + end + + def test_named_translated_route_on_non_default_locale_with_prefix + ActionController::Routing::Routes.draw { |map| map.people 'people', :controller => 'people', :action => 'index' } + config_default_locale_settings('en', true) + ActionController::Routing::Translator.translate { |t| t['en'] = {}; t['es'] = {'people' => 'gente'} } + + assert_routing '/es/gente', :controller => 'people', :action => 'index', :locale => 'es' + assert_routing '/en/people', :controller => 'people', :action => 'index', :locale => 'en' + assert_helpers_include :people_en, :people_es, :people + end + + # Named routes without prefix on default locale: + + def test_named_empty_route_without_prefix + ActionController::Routing::Routes.draw { |map| map.people '', :controller => 'people', :action => 'index'} + config_default_locale_settings('es', false) + ActionController::Routing::Translator.translate { |t| t['es'] = {}; t['en'] = {'people' => 'gente'}; } + + assert_routing '/en', :controller => 'people', :action => 'index', :locale => 'en' + assert_routing '/', :controller => 'people', :action => 'index', :locale => 'es' + assert_routing '', :controller => 'people', :action => 'index', :locale => 'es' + end + + def test_named_root_route_without_prefix + ActionController::Routing::Routes.draw { |map| map.root :controller => 'people', :action => 'index'} + config_default_locale_settings('es', false) + ActionController::Routing::Translator.translate_from_file 'test', 'locales', 'routes.yml' + + assert_routing '/', :controller => 'people', :action => 'index', :locale => 'es' + assert_routing '/en', :controller => 'people', :action => 'index', :locale => 'en' + assert_unrecognized_route '/es', :controller => 'people', :action => 'index', :locale => 'es' + end + + def test_named_untranslated_route_without_prefix + ActionController::Routing::Routes.draw { |map| map.people 'foo', :controller => 'people', :action => 'index'} + config_default_locale_settings('es', false) + ActionController::Routing::Translator.translate { |t| t['en'] = {}; t['es'] = {'people' => 'gente'} } + + assert_routing '/en/foo', :controller => 'people', :action => 'index', :locale => 'en' + assert_routing 'foo', :controller => 'people', :action => 'index', :locale => 'es' + assert_helpers_include :people_en, :people_es, :people + end + + def test_named_translated_route_on_default_locale_without_prefix + ActionController::Routing::Routes.draw { |map| map.people 'people', :controller => 'people', :action => 'index'} + config_default_locale_settings('es', false) + ActionController::Routing::Translator.translate { |t| t['en'] = {}; t['es'] = {'people' => 'gente'} } + + assert_routing '/en/people', :controller => 'people', :action => 'index', :locale => 'en' + assert_routing 'gente', :controller => 'people', :action => 'index', :locale => 'es' + assert_helpers_include :people_en, :people_es, :people + end + + def test_named_translated_route_on_non_default_locale_without_prefix + ActionController::Routing::Routes.draw { |map| map.people 'people', :controller => 'people', :action => 'index'} + config_default_locale_settings('en', false) + ActionController::Routing::Translator.translate { |t| t['en'] = {}; t['es'] = {'people' => 'gente'} } + + assert_routing '/people', :controller => 'people', :action => 'index', :locale => 'en' + assert_routing '/es/gente', :controller => 'people', :action => 'index', :locale => 'es' + assert_helpers_include :people_en, :people_es, :people + end + + def test_languages_load_from_file + ActionController::Routing::Routes.draw { |map| map.people 'people', :controller => 'people', :action => 'index'} + config_default_locale_settings('en', false) + ActionController::Routing::Translator.translate_from_file 'test', 'locales', 'routes.yml' + + assert_routing '/people', :controller => 'people', :action => 'index', :locale => 'en' + assert_routing '/es/gente', :controller => 'people', :action => 'index', :locale => 'es' + assert_helpers_include :people_en, :people_es, :people + end + + def test_languages_load_from_file_without_dictionary_for_default_locale + ActionController::Routing::Routes.draw { |map| map.people 'people', :controller => 'people', :action => 'index'} + config_default_locale_settings('fr', false) + ActionController::Routing::Translator.translate_from_file 'test', 'locales', 'routes.yml' + + assert_routing '/people', :controller => 'people', :action => 'index', :locale => 'fr' + assert_routing '/en/people', :controller => 'people', :action => 'index', :locale => 'en' + assert_routing '/es/gente', :controller => 'people', :action => 'index', :locale => 'es' + assert_helpers_include :people_fr, :people_en, :people_es, :people + end + + def test_i18n_based_translations_setting_locales + ActionController::Routing::Routes.draw { |map| map.people 'people', :controller => 'people', :action => 'index'} + config_default_locale_settings('en', false) + I18n.backend = StubbedI18nBackend + ActionController::Routing::Translator.i18n('es') + + assert_routing '/people', :controller => 'people', :action => 'index', :locale => 'en' + assert_routing '/es/gente', :controller => 'people', :action => 'index', :locale => 'es' + assert_helpers_include :people_en, :people_es, :people + end + + def test_i18n_based_translations_taking_i18n_available_locales + ActionController::Routing::Routes.draw { |map| map.people 'people', :controller => 'people', :action => 'index'} + config_default_locale_settings('en', false) + I18n.stubs(:available_locales).at_least_once.returns StubbedI18nBackend.available_locales + I18n.backend = StubbedI18nBackend + ActionController::Routing::Translator.i18n + + assert_routing '/people', :controller => 'people', :action => 'index', :locale => 'en' + assert_routing '/fr/people', :controller => 'people', :action => 'index', :locale => 'fr' + assert_routing '/es/gente', :controller => 'people', :action => 'index', :locale => 'es' + assert_helpers_include :people_fr, :people_en, :people_es, :people + end + + def test_action_controller_gets_locale_setter + ActionController::Base.instance_methods.include?('set_locale_from_url') + end + + def test_action_controller_gets_locale_suffix_helper + ActionController::Base.instance_methods.include?('locale_suffix') + end + + def test_action_view_gets_locale_suffix_helper + ActionView::Base.instance_methods.include?('locale_suffix') + end + + private + + def assert_helpers_include(*helpers) + helpers.each do |helper| + ['_url', '_path'].each do |suffix| + [@controller, @view].each { |obj| assert_respond_to obj, "#{helper}#{suffix}".to_sym } + end + end + end + + def assert_unrecognized_route(route_path, options) + assert_raise ActionController::RoutingError do + assert_routing route_path, options + end + end + + def config_default_locale_settings(locale, with_prefix) + I18n.default_locale = locale + ActionController::Routing::Translator.prefix_on_default_locale = with_prefix + end + + class StubbedI18nBackend + + + @@translations = { + 'es' => { 'people' => 'gente'}, + 'fr' => {} # empty on purpose to test behaviour on incompleteness scenarios + } + + def self.translate(locale, key, options) + @@translations[locale][key] || options[:default] + rescue + options[:default] + end + + def self.available_locales + @@translations.keys + end + + end + +end diff --git a/vendor/plugins/translate_routes/uninstall.rb b/vendor/plugins/translate_routes/uninstall.rb new file mode 100755 index 000000000..973833346 --- /dev/null +++ b/vendor/plugins/translate_routes/uninstall.rb @@ -0,0 +1 @@ +# Uninstall hook code here |