Make localization language list dynamic - fixes #807

- refactor LoadLanguageCommand so language loading can be reused
- add GetLanguagesCommand for the server
- change GUI to fetch language list and update selection list with it
This commit is contained in:
Tom Morris 2013-09-18 13:16:24 -04:00
parent ab0e36ab87
commit 4f2ebed676
7 changed files with 163 additions and 15 deletions

View File

@ -0,0 +1,114 @@
/*
Copyright 2010, Google Inc.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the
distribution.
* Neither the name of Google Inc. nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.google.refine.commands.lang;
import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.io.filefilter.WildcardFileFilter;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONWriter;
import com.google.refine.commands.Command;
import edu.mit.simile.butterfly.ButterflyModule;
public class GetLanguagesCommand extends Command {
public GetLanguagesCommand() {
super();
}
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String modname = request.getParameter("module");
if (modname == null) {
modname = "core";
}
ButterflyModule module = this.servlet.getModule(modname);
try {
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-Type", "application/json");
JSONWriter writer = new JSONWriter(response.getWriter());
writer.object();
writer.key("languages");
writer.array();
writeLangData(writer, "en", "English"); // we always have English and it's always first
FileFilter fileFilter = new WildcardFileFilter("translation-*.json");
for (File file : new File(module.getPath() + File.separator + "langs").listFiles(fileFilter)) {
String lang = file.getName().split("-")[1].split("\\.")[0];
if (!"en".equals(lang) && !"default".equals(lang)) {
JSONObject json = LoadLanguageCommand.loadLanguage(this.servlet, "core", lang);
if (json != null) {
String label = json.getString("name");
writeLangData(writer, lang, label);
}
}
}
writer.endArray();
writer.endObject();
} catch (JSONException e) {
respondException(response, e);
}
}
private void writeLangData(JSONWriter writer, String lang, String label)
throws JSONException {
writer.object();
writer.key("code"); writer.value(lang);
writer.key("label"); writer.value(label);
writer.endObject();
}
}

View File

@ -8,6 +8,7 @@ import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import javax.servlet.ServletException;
@ -19,6 +20,7 @@ import org.json.JSONObject;
import org.json.JSONTokener;
import com.google.refine.ProjectManager;
import com.google.refine.RefineServlet;
import com.google.refine.commands.Command;
import com.google.refine.preference.PreferenceStore;
@ -44,7 +46,6 @@ public class LoadLanguageCommand extends Command {
if (modname == null) {
modname = "core";
}
ButterflyModule module = this.servlet.getModule(modname);
String[] langs = request.getParameterValues("lang");
if (langs == null || "".equals(langs[0])) {
@ -53,34 +54,48 @@ public class LoadLanguageCommand extends Command {
langs = new String[] {(String) ps.get("userLang")};
}
}
// TODO: Switch this to just use English as the default language so we
// so we don't have to maintain a separate redundant file.
langs = Arrays.copyOf(langs, langs.length+1);
langs[langs.length-1] = "default";
JSONObject json = null;
boolean loaded = false;
for (String lang : langs) {
File langFile = new File(module.getPath(), "langs" + File.separator + "translation-" + lang + ".json");
try {
Reader reader = new BufferedReader(new InputStreamReader(new FileInputStream(langFile), "UTF-8"));
json = new JSONObject(new JSONTokener(reader));
json = loadLanguage(this.servlet, modname, lang);
if (json != null) {
response.setCharacterEncoding("UTF-8");
response.setContentType("application/json");
json.write(response.getWriter());
try {
json.write(response.getWriter());
} catch (JSONException e) {
logger.error("Error writing language labels to response stream");
}
response.getWriter().flush();
response.getWriter().close();
loaded = true;
break;
} catch (FileNotFoundException e1) {
json = null;
continue;
} catch (JSONException e) {
json = null;
logger.error("JSON error reading/writing language file", e);
continue;
}
}
if (!loaded) {
logger.error("Failed to load any language files");
}
}
static JSONObject loadLanguage(RefineServlet servlet, String modname, String lang) throws UnsupportedEncodingException {
ButterflyModule module = servlet.getModule(modname);
JSONObject json = null;
File langFile = new File(module.getPath(), "langs" + File.separator + "translation-" + lang + ".json");
try {
Reader reader = new BufferedReader(new InputStreamReader(new FileInputStream(langFile), "UTF-8"));
json = new JSONObject(new JSONTokener(reader));
} catch (FileNotFoundException e1) {
// Could be normal if we've got a list of languages as fallbacks
} catch (JSONException e) {
logger.error("JSON error reading/writing language file: " + langFile, e);
}
return json;
}
}

View File

@ -98,6 +98,7 @@ function registerCommands() {
RS.registerCommand(module, "transpose-rows-into-columns", new Packages.com.google.refine.commands.cell.TransposeRowsIntoColumnsCommand());
RS.registerCommand(module, "key-value-columnize", new Packages.com.google.refine.commands.cell.KeyValueColumnizeCommand());
RS.registerCommand(module, "get-languages", Packages.com.google.refine.commands.lang.GetLanguagesCommand());
RS.registerCommand(module, "load-language", Packages.com.google.refine.commands.lang.LoadLanguageCommand());
RS.registerCommand(module, "add-column", new Packages.com.google.refine.commands.column.AddColumnCommand());

View File

@ -1,4 +1,5 @@
{
"name" : "Default",
"core-index": {
"slogan": "A power tool for working with messy data",
"help": "Help",

View File

@ -1,4 +1,5 @@
{
"name" : "English",
"core-index": {
"slogan": "A power tool for working with messy data",
"help": "Help",

View File

@ -8,8 +8,6 @@
<tr>
<td></td>
<td><select id="langDD">
<option value="en" selected>English</option>
<option value="it">Italian</option>
</select></td>
</tr>
<tr>

View File

@ -9,6 +9,24 @@ Refine.SetLanguageUI = function(elmt) {
this._elmts.or_lang_label.text($.i18n._('core-index-lang')["label"]+":");
this._elmts.set_lan_btn.attr("value", $.i18n._('core-index-lang')["send-req"]);
$.ajax({
url : "/command/core/get-languages?",
type : "GET",
async : false,
data : {
name : "module",
value : "core"
},
success : function(data) {
for( var i = 0; i < data.languages.length; i++) {
var l = data.languages[i];
$('<option>').val(l.code).text(l.label).appendTo('#langDD');
}
}
});
this._elmts.set_lan_btn.bind('click', function(e) {
$.ajax({
url : "/command/core/set-preference?",