Move javascript translations to a separate key based on the path to
authorTom Hughes <tom@compton.nu>
Fri, 2 Oct 2009 23:33:17 +0000 (23:33 +0000)
committerTom Hughes <tom@compton.nu>
Fri, 2 Oct 2009 23:33:17 +0000 (23:33 +0000)
the javascript file that uses them and amend the layout to automatically
find and output all javascript strings.

app/helpers/application_helper.rb
app/views/layouts/site.html.erb
config/locales/en.yml
public/javascripts/map.js
public/javascripts/site.js

index b28ab7c1a6eca2dabe18a38d1b90e694bc9e5f6d..0107372459a7e0a4a98f450b556178e818259392 100644 (file)
@@ -10,4 +10,32 @@ module ApplicationHelper
   def atom_link_to(*args)
     return link_to(image_tag("RSS.gif", :size => "16x16", :border => 0), Hash[*args], { :class => "rsssmall" });
   end
+
+  def javascript_strings
+    js = ""
+
+    js << "<script type='text/javascript'>\n"
+    js << "rails_i18n = new Array();\n"
+    js << javascript_strings_for_key("javascripts")
+    js << "</script>\n"
+
+    return js
+  end
+
+private
+
+  def javascript_strings_for_key(key)
+    js = ""
+    value = t(key)
+
+    if value.is_a?(String)
+      js << "rails_i18n['#{key}'] = '" << escape_javascript(value) << "';\n"
+    else
+      value.each_key do |k|
+        js << javascript_strings_for_key("#{key}.#{k}")
+      end
+    end
+
+    return js
+  end
 end
index 46a3d9d1143677054d030a848bda66330a7dc078..1e4e56e6acf9e14de9807ed526bdcd1987fda2c4 100644 (file)
@@ -1,18 +1,7 @@
 <!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="<%= I18n.locale %>" lang="<%= I18n.locale %>" dir="<%= t'html.dir' %>">
   <head>
-    <script type="text/javascript">
-      /* This is a hack so we don't have to dynamically generate some of our JavaScript files */
-      var rails_i18n = new Array();
-      rails_i18n["layouts.edit_zoom_alert"]    = '<%= escape_javascript(t('layouts.edit_zoom_alert')) %>';
-      rails_i18n["layouts.history_zoom_alert"] = '<%= escape_javascript(t('layouts.history_zoom_alert')) %>';
-      // Layer switcher
-      rails_i18n["site.layers.base.mapnik"]        = '<%= escape_javascript(t('site.layers.base.mapnik')) %>';
-      rails_i18n["site.layers.base.osmarender"]    = '<%= escape_javascript(t('site.layers.base.osmarender')) %>';
-      rails_i18n["site.layers.base.cycle_map"]     = '<%= escape_javascript(t('site.layers.base.cycle_map')) %>';
-      rails_i18n["site.layers.base.noname"]        = '<%= escape_javascript(t('site.layers.base.noname')) %>';
-      rails_i18n["site.layers.overlays.maplint"]   = '<%= escape_javascript(t('site.layers.overlays.maplint')) %>';
-    </script>
+    <%= javascript_strings %>
     <%= javascript_include_tag 'prototype' %>
     <%= javascript_include_tag 'site' %>
     <!--[if lt IE 7]><%= javascript_include_tag 'pngfix' %><![endif]--> <!-- thanks, microsoft! -->
index fb433d8afd166fee68152a846c19c71b1003914e..bf1b58857ec1150cbe41e8567617a8d023d565d3 100644 (file)
@@ -429,10 +429,8 @@ en:
     view_tooltip: View maps
     edit: Edit
     edit_tooltip: Edit maps
-    edit_zoom_alert: You must zoom in to edit the map
     history: History
     history_tooltip: Changeset history
-    history_zoom_alert: You must zoom in to see the editing history
     export: Export
     export_tooltip: Export map data
     gps_traces: GPS Traces
@@ -614,14 +612,6 @@ en:
     delete:
       deleted: "Message deleted"
   site:
-    layers:
-      base:
-        mapnik: Mapnik
-        osmarender: Osmarender
-        cycle_map: Cycle Map
-        noname: NoName
-      overlays:
-        maplint: Maplint
     index:
       js_1: "You are either using a browser that doesn't support JavaScript, or you have disabled JavaScript."
       js_2: "OpenStreetMap uses JavaScript for its slippy map."
@@ -1008,3 +998,15 @@ en:
     remove_friend:
       success: "{{name}} was removed from your friends."
       not_a_friend: "{{name}} is not one of your friends."
+  javascripts:
+    map:
+      base:
+        mapnik: Mapnik
+        osmarender: Osmarender
+        cycle_map: Cycle Map
+        noname: NoName
+      overlays:
+        maplint: Maplint
+    site:
+      edit_zoom_alert: You must zoom in to edit the map
+      history_zoom_alert: You must zoom in to see the editing history
index 1afd3eef901a6c7e31d06aec248989c49c207dd7..8ae2349eaa41ee518eb3cf5daab670638b12dad7 100644 (file)
@@ -36,26 +36,26 @@ function createMap(divName, options) {
       displayProjection: new OpenLayers.Projection("EPSG:4326")
    });
 
-   var mapnik = new OpenLayers.Layer.OSM.Mapnik(rails_i18n["site.layers.base.mapnik"], {
+   var mapnik = new OpenLayers.Layer.OSM.Mapnik(rails_i18n["javascripts.map.base.mapnik"], {
       displayOutsideMaxExtent: true,
       wrapDateLine: true
    });
    map.addLayer(mapnik);
 
-   var osmarender = new OpenLayers.Layer.OSM.Osmarender(rails_i18n["site.layers.base.osmarender"], {
+   var osmarender = new OpenLayers.Layer.OSM.Osmarender(rails_i18n["javascripts.map.base.osmarender"], {
       displayOutsideMaxExtent: true,
       wrapDateLine: true
    });
    map.addLayer(osmarender);
 
-   var cyclemap = new OpenLayers.Layer.OSM.CycleMap(rails_i18n["site.layers.base.cycle_map"], {
+   var cyclemap = new OpenLayers.Layer.OSM.CycleMap(rails_i18n["javascripts.map.base.cycle_map"], {
       displayOutsideMaxExtent: true,
       wrapDateLine: true
    });
    map.addLayer(cyclemap);
 
    var nonamekey = nonamekeys[document.domain];
-   var noname = new OpenLayers.Layer.OSM(rails_i18n["site.layers.base.noname"], [
+   var noname = new OpenLayers.Layer.OSM(rails_i18n["javascripts.map.base.noname"], [
       "http://a.tile.cloudmade.com/" + nonamekey + "/3/256/${z}/${x}/${y}.png",
       "http://b.tile.cloudmade.com/" + nonamekey + "/3/256/${z}/${x}/${y}.png",
       "http://c.tile.cloudmade.com/" + nonamekey + "/3/256/${z}/${x}/${y}.png"
@@ -66,7 +66,7 @@ function createMap(divName, options) {
    });
    map.addLayer(noname);
 
-   var maplint = new OpenLayers.Layer.OSM.Maplint(rails_i18n["site.layers.overlays.maplint"], {
+   var maplint = new OpenLayers.Layer.OSM.Maplint(rails_i18n["javascripts.map.overlays.maplint"], {
       displayOutsideMaxExtent: true,
       wrapDateLine: true
    });
index 17b2f066875c1ed487c9b15c29b1b106bb9031ae..9dbb2a6404862f9c4a27d0ddcc6e8572c92aef0c 100644 (file)
@@ -62,7 +62,7 @@ function updatelinks(lon,lat,zoom,layers,minlon,minlat,maxlon,maxlat,objtype,obj
       node.href = setArgs("/edit", args);
       node.style.fontStyle = 'normal';
     } else {
-      node.href = 'javascript:alert(rails_i18n["layouts.edit_zoom_alert"]);';
+      node.href = 'javascript:alert(rails_i18n["javascripts.site.edit_zoom_alert"]);';
       node.style.fontStyle = 'italic';
     }
   }
@@ -87,7 +87,7 @@ function updatelinks(lon,lat,zoom,layers,minlon,minlat,maxlon,maxlat,objtype,obj
       node.href = setArgs("/history", args);
       node.style.fontStyle = 'normal';
     } else {
-      node.href = 'javascript:alert(rails_i18n["layouts.history_zoom_alert"]);';
+      node.href = 'javascript:alert(rails_i18n["javascripts.site.history_zoom_alert"]);';
       node.style.fontStyle = 'italic';
     }
   }