Switch to using i18n-js for handling translations in javascript

This commit is contained in:
Tom Hughes 2012-02-24 23:21:09 +00:00
parent 9638d6ea7f
commit 7ad58d1411
76 changed files with 552 additions and 563 deletions

View file

@ -22,7 +22,7 @@ function createMap(divName, options) {
theme: "<%= asset_path 'theme/openstreetmap/style.css' %>"
});
var mapnik = new OpenLayers.Layer.OSM.Mapnik(i18n("javascripts.map.base.standard"), {
var mapnik = new OpenLayers.Layer.OSM.Mapnik(I18n.t("javascripts.map.base.standard"), {
attribution: "",
keyid: "mapnik",
displayOutsideMaxExtent: true,
@ -31,7 +31,7 @@ function createMap(divName, options) {
});
map.addLayer(mapnik);
var cyclemap = new OpenLayers.Layer.OSM.CycleMap(i18n("javascripts.map.base.cycle_map"), {
var cyclemap = new OpenLayers.Layer.OSM.CycleMap(I18n.t("javascripts.map.base.cycle_map"), {
attribution: "Tiles courtesy of <a href='http://www.opencyclemap.org/' target='_blank'>Andy Allan</a>",
keyid: "cyclemap",
displayOutsideMaxExtent: true,
@ -40,7 +40,7 @@ function createMap(divName, options) {
});
map.addLayer(cyclemap);
var transportmap = new OpenLayers.Layer.OSM.TransportMap(i18n("javascripts.map.base.transport_map"), {
var transportmap = new OpenLayers.Layer.OSM.TransportMap(I18n.t("javascripts.map.base.transport_map"), {
attribution: "Tiles courtesy of <a href='http://www.opencyclemap.org/' target='_blank'>Andy Allan</a>",
keyid: "transportmap",
displayOutsideMaxExtent: true,
@ -49,7 +49,7 @@ function createMap(divName, options) {
});
map.addLayer(transportmap);
var mapquest = new OpenLayers.Layer.OSM(i18n("javascripts.map.base.mapquest"), [
var mapquest = new OpenLayers.Layer.OSM(I18n.t("javascripts.map.base.mapquest"), [
"http://otile1.mqcdn.com/tiles/1.0.0/osm/${z}/${x}/${y}.png",
"http://otile2.mqcdn.com/tiles/1.0.0/osm/${z}/${x}/${y}.png",
"http://otile3.mqcdn.com/tiles/1.0.0/osm/${z}/${x}/${y}.png",

View file

@ -2,6 +2,7 @@
//= require jquery_ujs
//= require jquery.autogrowtextarea
//= require jquery.timers
//= require i18n/translations
/*
* Called as the user scrolls/zooms around to aniplate hrefs of the
@ -50,11 +51,11 @@ function updatelinks(lon,lat,zoom,layers,minlon,minlat,maxlon,maxlat,objtype,obj
$(link).off("click.minzoom");
if (zoom >= minzoom) {
$(link).attr("title", i18n("javascripts.site." + name + "_tooltip"));
$(link).attr("title", I18n.t("javascripts.site." + name + "_tooltip"));
$(link).removeClass("disabled");
} else {
$(link).on("click.minzoom", function () { alert(i18n("javascripts.site." + name + "_zoom_alert")); return false; });
$(link).attr("title", i18n("javascripts.site." + name + "_disabled_tooltip"));
$(link).on("click.minzoom", function () { alert(I18n.t("javascripts.site." + name + "_zoom_alert")); return false; });
$(link).attr("title", I18n.t("javascripts.site." + name + "_disabled_tooltip"));
$(link).addClass("disabled");
}
}
@ -144,26 +145,6 @@ function setArgs(url, args) {
return url.replace(/\?.*$/, "") + "?" + queryitems.join("&");
}
/*
* Called to interpolate JavaScript variables in strings using a
* similar syntax to rails I18n string interpolation - the only
* difference is that [[foo]] is the placeholder syntax instead
* of {{foo}} which allows the same string to be processed by both
* rails and then later by javascript.
*/
function i18n(string, keys) {
string = i18n_strings[string] || string;
for (var key in keys) {
var re_key = '\\[\\[' + key + '\\]\\]';
var re = new RegExp(re_key, "g");
string = string.replace(re, keys[key]);
}
return string;
}
/*
* Called to interlace the bits in x and y, making a Morton code.
*/

View file

@ -25,17 +25,6 @@ module ApplicationHelper
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 << "i18n_strings = new Array();\n"
js << javascript_strings_for_key("javascripts")
js << "</script>\n"
return raw(js)
end
def style_rules
css = ""
@ -106,21 +95,4 @@ module ApplicationHelper
end
end
end
private
def javascript_strings_for_key(key)
js = ""
value = I18n.t(key, :locale => "en")
if value.is_a?(String)
js << "i18n_strings['#{key}'] = '" << escape_javascript(t(key)) << "';\n"
else
value.each_key do |k|
js << javascript_strings_for_key("#{key}.#{k}")
end
end
return js
end
end

View file

@ -155,7 +155,7 @@ function displayFeatureWarning(count, limit, callback) {
var div = document.createElement("div");
var p = document.createElement("p");
p.appendChild(document.createTextNode(i18n("<%=j t 'browse.start_rjs.loaded_an_area_with_num_features' %>", { num_features: count, max_features: limit })));
p.appendChild(document.createTextNode(I18n.t("browse.start_rjs.loaded_an_area_with_num_features", { num_features: count, max_features: limit })));
div.appendChild(p);
var input = document.createElement("input");
@ -194,7 +194,7 @@ function getData(bounds, reload) {
var size = projected.getWidth() * projected.getHeight();
if (size > <%= MAX_REQUEST_AREA %>) {
setStatus(i18n("<%=j t 'browse.start_rjs.unable_to_load_size', :max_bbox_size => MAX_REQUEST_AREA %>", { bbox_size: size }));
setStatus(I18n.t("browse.start_rjs.unable_to_load_size", { max_bbox_size: <%= MAX_REQUEST_AREA %>, bbox_size: size }));
} else {
loadData("/api/<%= API_VERSION %>/map?bbox=" + projected.toBBOX(), reload);
}
@ -441,7 +441,7 @@ function displayHistory(request) {
table.appendChild(tr);
var heading = document.createElement("td");
heading.appendChild(document.createTextNode(i18n("<%=j t 'browse.start_rjs.history_for_feature' %>", { feature: featureNameHistory(this.feature) })));
heading.appendChild(document.createTextNode(I18n.t("browse.start_rjs.history_for_feature", { feature: featureNameHistory(this.feature) })));
tr.appendChild(heading);
var td = document.createElement("td");
@ -462,7 +462,7 @@ function displayHistory(request) {
var user = nodes[i].getAttribute("user") || "<%=j t 'browse.start_rjs.private_user' %>";
var timestamp = nodes[i].getAttribute("timestamp");
var item = document.createElement("li");
item.appendChild(document.createTextNode(i18n("<%=j t 'browse.start_rjs.edited_by_user_at_timestamp' %>", { user: user, timestamp: timestamp })));
item.appendChild(document.createTextNode(I18n.t("browse.start_rjs.edited_by_user_at_timestamp", { user: user, timestamp: timestamp })));
history.appendChild(item);
}
div.appendChild(history);
@ -502,9 +502,9 @@ function featureNameSelect(feature) {
} else if (feature.attributes.name) {
return feature.attributes.name;
} else if (featureType(feature) == "node") {
return i18n("<%=j t 'browse.start_rjs.object_list.selected.type.node' %>", { id: feature.osm_id });
return I18n.t("browse.start_rjs.object_list.selected.type.node", { id: feature.osm_id });
} else if (featureType(feature) == "way") {
return i18n("<%=j t 'browse.start_rjs.object_list.selected.type.way' %>", { id: feature.osm_id });
return I18n.t("browse.start_rjs.object_list.selected.type.way", { id: feature.osm_id });
}
}
@ -514,9 +514,9 @@ function featureNameHistory(feature) {
} else if (feature.attributes.name) {
return feature.attributes.name;
} else if (featureType(feature) == "node") {
return i18n("<%=j t 'browse.start_rjs.object_list.history.type.node' %>", { id: feature.osm_id });
return I18n.t("browse.start_rjs.object_list.history.type.node", { id: feature.osm_id });
} else if (featureType(feature) == "way") {
return i18n("<%=j t 'browse.start_rjs.object_list.history.type.way' %>", { id: feature.osm_id });
return I18n.t("browse.start_rjs.object_list.history.type.way", { id: feature.osm_id });
}
}

View file

@ -1,6 +1,5 @@
<head>
<meta name="viewport" content="width=device-width, minimum-scale=1.0, maximum-scale=1.0"/>
<%= javascript_strings %>
<%= javascript_include_tag "site" %>
<%= javascript_include_tag "menu" %>
<!--[if lt IE 7]><%= javascript_include_tag "pngfix" %><![endif]--> <!-- thanks, microsoft! -->
@ -16,5 +15,10 @@
<%= style_rules %>
<%= yield :head %>
<%= csrf_meta_tag %>
<script type="text/javascript">
I18n.defaultLocale = "<%= I18n.default_locale %>";
I18n.locale = "<%= I18n.locale %>";
I18n.fallbacks = true;
</script>
<title><%= t 'layouts.project_name.title' %><%= ' | '+ @title if @title %></title>
</head>