Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added update_yaml task to create/update yaml files, suggested by Fran…

…cesc Esplugas

Added .gitignore
  • Loading branch information...
commit 1b62e9ac918dfeb150058ad4155eaf156e44956b 1 parent ba1fb1d
@raul authored
View
4 .gitignore
@@ -0,0 +1,4 @@
+.DS_Store
+SampleApp/log/*.log
+SampleApp/tmp/**/*
+SampleApp/db/test.sqlite3
View
3  ChangeLog
@@ -1,3 +1,6 @@
+-- 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
View
8 README.rdoc
@@ -83,7 +83,13 @@ We can see that resource controllers and actions are translated as well:
new_person_es GET /es/gente/crear {:lang=>"es", :controller=>"people", :action=>"new"}
new_person GET /people/new {:lang=>"en", :controller=>"people", :action=>"new"}
-We can get rid of that verbose translation hashes by using yaml files. Create a file inside your <tt>/config</tt> directory, called <tt>routes_es.yml</tt> and fill it with your desired translations, e.g:
+We can get rid of that verbose translation hashes by using yaml files, which can be generated/updated with the <tt>translate_routes:update_yaml</tt> rake task.
+Let's create a spanish and french translation files with <tt>translate_routes:update_yaml["es fr"]</tt>
+
+If it's the first time that we run the task, the files will contain a yaml skeleton with the strings to translate.
+If we already have translated some strings they will remain on the file after executing the task, so you can run it after any <tt>routes.rb</tt> edition.
+
+We can now translate the strings for any language
# /config/routes_es.yml
person: persona
View
3  SampleApp/config/routes_en.yml
@@ -0,0 +1,3 @@
+edit:
+new:
+users:
View
1  SampleApp/config/routes_es.yml
@@ -1,4 +1,3 @@
edit: editar
new: crear
users: usuarios
-show: mostrar
View
29 SampleApp/vendor/plugins/translate_routes/lib/translate_routes.rb
@@ -15,6 +15,8 @@ module Translator
mattr_accessor :lang_param_key
@@lang_param_key = :lang # e.g: :lang generates params[:lang] and @lang controller variable
+ mattr_accessor :original_routes
+
def self.translate
dictionaries = Hash.new
yield dictionaries
@@ -34,11 +36,22 @@ def self.translate_from_files
Translator.translate_current_routes dictionaries
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
+
private
def self.translate_current_routes(dictionaries)
# 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}
Routes.clear!
@@ -51,11 +64,11 @@ def self.translate_current_routes(dictionaries)
old_routes.each do |old_route|
old_name = old_names.index(old_route)
-
+
# process and add the translated ones
trans_routes, trans_named_routes = translate_route(old_route, dictionaries, old_name)
new_routes.concat(trans_routes)
-
+
# process the old route:
new_old_route = clone_with_deeply_copied_static_segments(old_route) # we need a fresh route to apply requirements
@@ -63,10 +76,10 @@ def self.translate_current_routes(dictionaries)
add_language_requirements(new_old_route, default_lang)
add_language_segment(new_old_route, default_lang) if prefix_on_default_lang
new_routes << new_old_route
-
+
# if it's a named one we append the lang suffix and replace the old helper by a language-based call
if old_name
-
+
trans_named_routes["#{old_name}_#{default_lang}"] = new_old_route
trans_named_routes[old_name] = new_old_route # keep the old name to use the helper on integration tests
@@ -83,19 +96,19 @@ def #{new_helper_name}(*args)
end
end
DEF_NEW_HELPER
-
+
[ActionController::Base, ActionView::Base].each { |d| d.module_eval(def_new_helper) }
ActionController::Routing::Routes.named_routes.helpers << new_helper_name.to_sym
end
end
-
- end
+ end
+
# apply all new routes
Routes.routes = new_routes
new_named_routes.each { |name, r| Routes.named_routes.add name, r }
-
+
end
def self.clone_with_deeply_copied_static_segments(route)
View
46 SampleApp/vendor/plugins/translate_routes/tasks/translate_routes_tasks.rake
@@ -1,4 +1,42 @@
-# desc "Explaining what the task does"
-# task :translate_routes do
-# # Task goes here
-# end
+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_lang
+ langs.split.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
+
+ segments.each do |s|
+ translation = translations[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
+
View
2  doc/created.rid
@@ -1 +1 @@
-Fri, 06 Jun 2008 16:34:03 +0200
+Tue, 10 Jun 2008 09:26:35 +0200
View
351 doc/files/README.html
@@ -1,351 +0,0 @@
-<?xml version="1.0" encoding="iso-8859-1"?>
-<!DOCTYPE html
- PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
- "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
-
-<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
-<head>
- <title>File: README</title>
- <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
- <meta http-equiv="Content-Script-Type" content="text/javascript" />
- <link rel="stylesheet" href=".././rdoc-style.css" type="text/css" media="screen" />
- <script type="text/javascript">
- // <![CDATA[
-
- function popupCode( url ) {
- window.open(url, "Code", "resizable=yes,scrollbars=yes,toolbar=no,status=no,height=150,width=400")
- }
-
- function toggleCode( id ) {
- if ( document.getElementById )
- elem = document.getElementById( id );
- else if ( document.all )
- elem = eval( "document.all." + id );
- else
- return false;
-
- elemStyle = elem.style;
-
- if ( elemStyle.display != "block" ) {
- elemStyle.display = "block"
- } else {
- elemStyle.display = "none"
- }
-
- return true;
- }
-
- // Make codeblocks hidden by default
- document.writeln( "<style type=\"text/css\">div.method-source-code { display: none }</style>" )
-
- // ]]>
- </script>
-
-</head>
-<body>
-
-
-
- <div id="fileHeader">
- <h1>README</h1>
- <table class="header-table">
- <tr class="top-aligned-row">
- <td><strong>Path:</strong></td>
- <td>README
- </td>
- </tr>
- <tr class="top-aligned-row">
- <td><strong>Last Update:</strong></td>
- <td>Fri Jun 06 11:05:04 +0200 2008</td>
- </tr>
- </table>
- </div>
- <!-- banner header -->
-
- <div id="bodyContent">
-
-
-
- <div id="contextContent">
-
- <div id="description">
- <h1>TranslateRoutes</h1>
-<p>
-This Rails plugin provides a simple way to translate your URLs to any
-number of languages.
-</p>
-<p>
-It&#8216;s designed to be extremely and easy to use and customize. As it
-works after routing processing, it plays nice with all kind of routing
-definitions, including RESTful and named routes. It also takes care of your
-current routing helpers by translating them transparently.
-</p>
-<h2>Installation</h2>
-<p>
-Just check it out on your app&#8216;s <tt>/vendor/plugins</tt> and see the
-examples below.
-</p>
-<h2>Usage</h2>
-<p>
-Let&#8216;s start with a tiny example. Of course you need to define your
-routes first, e.g:
-</p>
-<pre>
- ActionController::Routing::Routes.draw do |map|
- map.connect 'contact', :controller =&gt; 'contact', :action =&gt; 'index'
- end
-</pre>
-<p>
-After that, on the same routes.rb file, you can translate the generated
-route by doing:
-</p>
-<pre>
- ActionController::Routing::Translator.translate do |t|
- t['es'] = {
- 'contact' =&gt; 'contacto'
- }
- end
-</pre>
-<p>
-This will give you the next routes (check it out by doing &#8216;rake
-routes&#8217;):
-</p>
-<pre>
- /es/contacto {:lang=&gt;&quot;es&quot;, :controller=&gt;&quot;contact&quot;, :action=&gt;&quot;index&quot;}
- /contact {:lang=&gt;&quot;en&quot;, :controller=&gt;&quot;contact&quot;, :action=&gt;&quot;index&quot;}
-</pre>
-<p>
-As you can see:
-</p>
-<ul>
-<li>a <tt>lang</tt> param has been added to your routes, with the value of the
-used language (*)
-
-</li>
-<li><tt>en</tt> is the default language (*)
-
-</li>
-<li>the URLs associated with a language are always prefixed with the language
-code that you provided on the translation.
-
-</li>
-<li>the default language URLs have no language prefix (*)
-
-</li>
-</ul>
-<p>
-(*) You can customize this behaviour, see &quot;Configuration&quot; below.
-</p>
-<p>
-Let&#8216;s add some routes and translations to our example:
-</p>
-<pre>
- ActionController::Routing::Routes.draw do |map|
- map.connect 'contact', :controller =&gt; 'contact', :action =&gt; 'index'
- map.home '', :controller =&gt; 'main', :action =&gt; 'index'
- map.resources :people
- end
-
- ActionController::Routing::Translator.translate do |t|
-
- t['es'] = {
- 'contact' =&gt; 'contacto',
- 'people' =&gt; 'gente',
- 'new' =&gt; 'crear',
- 'edit' =&gt;'editar'
- }
-
- t['fr'] = {
- 'people' =&gt; 'personne',
- 'new' =&gt; 'neuf'
- }
- end
-</pre>
-<p>
-Again, you can use <tt>rake routes</tt> to see the generated URLs.
-I&#8216;ll only talk about some of them:
-</p>
-<p>
-We didn&#8216;t give any french translation for <tt>contact</tt>, so the
-french associated URLs use the untranslated text:
-</p>
-<pre>
- /fr/contact {:lang=&gt;&quot;fr&quot;, :controller=&gt;&quot;contact&quot;, :action=&gt;&quot;index&quot;}
- /es/contacto {:lang=&gt;&quot;es&quot;, :controller=&gt;&quot;contact&quot;, :action=&gt;&quot;index&quot;}
- /contact {:lang=&gt;&quot;en&quot;, :controller=&gt;&quot;contact&quot;, :action=&gt;&quot;index&quot;}
-</pre>
-<p>
-The named routes have generated not only translated URLs but also a helper
-for each language:
-</p>
-<pre>
- home_fr /fr {:lang=&gt;&quot;fr&quot;, :controller=&gt;&quot;main&quot;, :action=&gt;&quot;index&quot;}
- home_es /es {:lang=&gt;&quot;es&quot;, :controller=&gt;&quot;main&quot;, :action=&gt;&quot;index&quot;}
- home_en / {:lang=&gt;&quot;en&quot;, :controller=&gt;&quot;main&quot;, :action=&gt;&quot;index&quot;}
-</pre>
-<p>
-This means that you can use <tt>home_es_path</tt> and <tt>home_es_url</tt>
-on your controllers and views.
-</p>
-<p>
-No <tt>home</tt> named route is shown on the list so it looks like you
-can&#8216;t use a simple <tt>home_path</tt> helper, but relax: you have a
-<tt>home_path</tt> helper available for your controllers and views. It
-looks for the current <tt>lang</tt> value and redirects to the appropiate
-helper. This means that if you&#8216;re rendering the
-<tt>contact/index</tt> view when visiting <tt>/es/contacto</tt>, the
-<tt>home_path</tt> helper will call to <tt>home_es_path</tt> and return its
-result. The same applies to <tt>home_url</tt>, obviously.
-</p>
-<p>
-We can see that resource controllers and actions are translated as well:
-</p>
-<pre>
- new_person_fr GET /fr/personne/neuf {:lang=&gt;&quot;fr&quot;, :controller=&gt;&quot;people&quot;, :action=&gt;&quot;new&quot;}
- new_person_es GET /es/gente/crear {:lang=&gt;&quot;es&quot;, :controller=&gt;&quot;people&quot;, :action=&gt;&quot;new&quot;}
- new_person GET /people/new {:lang=&gt;&quot;en&quot;, :controller=&gt;&quot;people&quot;, :action=&gt;&quot;new&quot;}
-</pre>
-<p>
-Finally, we can get rid of that verbose translation hashes by using yaml
-files. Create a file inside your <tt>/config</tt> directory, called
-<tt>routes_es.yml</tt> and fill it with your desired translations, e.g:
-</p>
-<pre>
- # /config/routes_es.yml
- person: persona
-</pre>
-<p>
-and replace all the
-</p>
-<pre>
- ActionController::Routing::Translator.translate do
- ...
- end
-</pre>
-<p>
-stuff on <tt>routes.rb</tt> with this simple line:
-</p>
-<pre>
- ActionController::Routing::Translator.translate_from_files
-</pre>
-<h2>Configuration</h2>
-<p>
-You can customize some features by adding some lines before your
-translation code:
-</p>
-<ul>
-<li>You can change the language param key (<tt>:lang</tt> by default). To
-change this feature use:
-
-<pre>
- ActionController::Routing::Translator.lang_param_key = :foo
-</pre>
-</li>
-</ul>
-<p>
-This key should have the same name that the instance variable which keeps
-the language code on your controllers and views.
-</p>
-<ul>
-<li>You can change the default language of your application with:
-
-<pre>
- ActionController::Routing::Translator.default_lang = 'es'
-</pre>
-</li>
-</ul>
-<p>
-Of course, you can also translate your default language. If, as I do, all
-your controllers and actions have english names, you can have an english
-version on your site for free by doing:
-</p>
-<pre>
- ActionController::Routing::Translator.default_lang = 'es'
- ActionController::Routing::Translator.translate do |t|
- t['en'] = {}
- t['es'] = { # spanish translation here }
- end
-</pre>
-<p>
-or adding the equivalent <tt>/config/routes_es.yml</tt> and an empty
-<tt>/config/routes_en.yml</tt>
-</p>
-<ul>
-<li>You can add a prefix also on your default language routes by doing:
-
-<pre>
- ActionController::Routing::Translator.prefix_on_default_lang = true
-</pre>
-</li>
-</ul>
-<p>
-<b>WARNING!</b> this way you&#8216;ll disable your root -empty- route: if
-english is your default language, your empty route will be translated to
-<tt>/en</tt> so you must manually take care of it.
-</p>
-<h2>Suggestions, bug reports, questions</h2>
-<p>
-Feedback and comments will be always welcome at raul@murciano.net
-</p>
-<h2>Credits</h2>
-<ul>
-<li>Raul Murciano [<a href="http://raul.murciano.net">raul.murciano.net</a>] -
-code
-
-</li>
-<li>Domestika INTERNET S.L [<a href="http://domestika.org">domestika.org</a>] -
-incredible support, really nice people to work with!
-
-</li>
-</ul>
-<h2>Rails routing resources</h2>
-<ul>
-<li>David Black&#8216;s &#8216;Rails Routing&#8217; ebook rocks! - &#8216;Ruby
-for Rails&#8217; too, BTW.
-
-</li>
-<li>Obie Fernandez&#8216;s &#8216;The Rails Way&#8217; - the definitive RoR
-reference, great work Obie!
-
-</li>
-</ul>
-<h2>License</h2>
-<p>
-Copyright (c) 2007 Raul Murciano [<a
-href="http://raul.murciano.net">raul.murciano.net</a>] Domestika INTERNET
-S.L. [<a href="http://domestika.org">domestika.org</a>], released under the
-MIT license (see MIT-LICENSE)
-</p>
-
- </div>
-
-
- </div>
-
-
- </div>
-
-
- <!-- if includes -->
-
- <div id="section">
-
-
-
-
-
-
-
-
- <!-- if method_list -->
-
-
- </div>
-
-
-<div id="validator-badges">
- <p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p>
-</div>
-
-</body>
-</html>
View
19 doc/files/README_rdoc.html
@@ -56,7 +56,7 @@
</tr>
<tr class="top-aligned-row">
<td><strong>Last Update:</strong></td>
- <td>Fri Jun 06 16:25:58 +0200 2008</td>
+ <td>Tue Jun 10 09:26:30 +0200 2008</td>
</tr>
</table>
</div>
@@ -208,9 +208,20 @@
new_person GET /people/new {:lang=&gt;&quot;en&quot;, :controller=&gt;&quot;people&quot;, :action=&gt;&quot;new&quot;}
</pre>
<p>
-We can get rid of that verbose translation hashes by using yaml files.
-Create a file inside your <tt>/config</tt> directory, called
-<tt>routes_es.yml</tt> and fill it with your desired translations, e.g:
+We can get rid of that verbose translation hashes by using yaml files,
+which can be generated/updated with the
+<tt>translate_routes:update_yaml</tt> rake task. Let&#8216;s create a
+spanish and french translation files with
+<tt>translate_routes:update_yaml[&quot;es fr&quot;]</tt>
+</p>
+<p>
+If it&#8216;s the first time that we run the task, the files will contain a
+yaml skeleton with the strings to translate. If we already have translated
+some strings they will remain on the file after executing the task, so you
+can run it after any <tt>routes.rb</tt> edition.
+</p>
+<p>
+We can now translate the strings for any language
</p>
<pre>
# /config/routes_es.yml
View
29 lib/translate_routes.rb
@@ -15,6 +15,8 @@ module Translator
mattr_accessor :lang_param_key
@@lang_param_key = :lang # e.g: :lang generates params[:lang] and @lang controller variable
+ mattr_accessor :original_routes
+
def self.translate
dictionaries = Hash.new
yield dictionaries
@@ -34,11 +36,22 @@ def self.translate_from_files
Translator.translate_current_routes dictionaries
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
+
private
def self.translate_current_routes(dictionaries)
# 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}
Routes.clear!
@@ -51,11 +64,11 @@ def self.translate_current_routes(dictionaries)
old_routes.each do |old_route|
old_name = old_names.index(old_route)
-
+
# process and add the translated ones
trans_routes, trans_named_routes = translate_route(old_route, dictionaries, old_name)
new_routes.concat(trans_routes)
-
+
# process the old route:
new_old_route = clone_with_deeply_copied_static_segments(old_route) # we need a fresh route to apply requirements
@@ -63,10 +76,10 @@ def self.translate_current_routes(dictionaries)
add_language_requirements(new_old_route, default_lang)
add_language_segment(new_old_route, default_lang) if prefix_on_default_lang
new_routes << new_old_route
-
+
# if it's a named one we append the lang suffix and replace the old helper by a language-based call
if old_name
-
+
trans_named_routes["#{old_name}_#{default_lang}"] = new_old_route
trans_named_routes[old_name] = new_old_route # keep the old name to use the helper on integration tests
@@ -83,19 +96,19 @@ def #{new_helper_name}(*args)
end
end
DEF_NEW_HELPER
-
+
[ActionController::Base, ActionView::Base].each { |d| d.module_eval(def_new_helper) }
ActionController::Routing::Routes.named_routes.helpers << new_helper_name.to_sym
end
end
-
- end
+ end
+
# apply all new routes
Routes.routes = new_routes
new_named_routes.each { |name, r| Routes.named_routes.add name, r }
-
+
end
def self.clone_with_deeply_copied_static_segments(route)
View
46 tasks/translate_routes_tasks.rake
@@ -1,4 +1,42 @@
-# desc "Explaining what the task does"
-# task :translate_routes do
-# # Task goes here
-# end
+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_lang
+ langs.split.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
+
+ segments.each do |s|
+ translation = translations[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
+
Please sign in to comment.
Something went wrong with that request. Please try again.