Translate: Add method to set language in standalone app context

i2psnark:
- add form to set language in standalone context
- add Ukrainian and Japanese translations
This commit is contained in:
zzz
2016-06-15 15:03:17 +00:00
parent 47f0bbb93a
commit 9ba8f53ec7
9 changed files with 3021 additions and 9 deletions

View File

@ -211,6 +211,9 @@
<zipfileset src="../../ministreaming/java/build/mstreaming.jar" />
<zipfileset src="../../streaming/java/build/streaming.jar" />
<zipfileset src="../../systray/java/build/systray.jar" />
<!-- Countries translations. The i2psnark translations are in the war but it's easier to put these here -->
<!-- 300KB just to translate "Brazil", but why not... -->
<fileset dir="../../routerconsole/java/build/obj" includes="net/i2p/router/countries/*.class" />
<manifest>
<attribute name="Main-Class" value="org.klomp.snark.standalone.RunStandalone"/>
<attribute name="Implementation-Version" value="${full.version}" />

View File

@ -42,6 +42,7 @@ import net.i2p.util.SecureDirectory;
import net.i2p.util.SecureFileOutputStream;
import net.i2p.util.SimpleTimer;
import net.i2p.util.SimpleTimer2;
import net.i2p.util.Translate;
import org.klomp.snark.dht.DHT;
import org.klomp.snark.dht.KRPC;
@ -127,6 +128,8 @@ public class SnarkManager implements CompleteListener {
public static final String PROP_PRIVATETRACKERS = "i2psnark.privatetrackers";
private static final String PROP_USE_DHT = "i2psnark.enableDHT";
private static final String PROP_SMART_SORT = "i2psnark.smartSort";
private static final String PROP_LANG = "i2psnark.lang";
private static final String PROP_COUNTRY = "i2psnark.country";
public static final int MIN_UP_BW = 10;
public static final int DEFAULT_MAX_UP_BW = 25;
@ -254,6 +257,13 @@ public class SnarkManager implements CompleteListener {
//_context.addShutdownTask(new SnarkManagerShutdown());
_idleChecker = new IdleChecker(this, _peerCoordinatorSet);
_idleChecker.schedule(5*60*1000);
if (!_context.isRouterContext()) {
String lang = _config.getProperty(PROP_LANG);
if (lang != null) {
String country = _config.getProperty(PROP_COUNTRY, "");
Translate.setLanguage(lang, country);
}
}
}
/**
@ -774,19 +784,22 @@ public class SnarkManager implements CompleteListener {
public void updateConfig(String dataDir, boolean filesPublic, boolean autoStart, boolean smartSort, String refreshDelay,
String startDelay, String pageSize, String seedPct, String eepHost,
String eepPort, String i2cpHost, String i2cpPort, String i2cpOpts,
String upLimit, String upBW, boolean useOpenTrackers, boolean useDHT, String theme) {
String upLimit, String upBW, boolean useOpenTrackers, boolean useDHT, String theme,
String lang) {
synchronized(_configLock) {
locked_updateConfig(dataDir, filesPublic, autoStart, smartSort,refreshDelay,
startDelay, pageSize, seedPct, eepHost,
eepPort, i2cpHost, i2cpPort, i2cpOpts,
upLimit, upBW, useOpenTrackers, useDHT, theme);
upLimit, upBW, useOpenTrackers, useDHT, theme,
lang);
}
}
private void locked_updateConfig(String dataDir, boolean filesPublic, boolean autoStart, boolean smartSort, String refreshDelay,
String startDelay, String pageSize, String seedPct, String eepHost,
String eepPort, String i2cpHost, String i2cpPort, String i2cpOpts,
String upLimit, String upBW, boolean useOpenTrackers, boolean useDHT, String theme) {
String upLimit, String upBW, boolean useOpenTrackers, boolean useDHT, String theme,
String lang) {
boolean changed = false;
boolean interruptMonitor = false;
//if (eepHost != null) {
@ -893,6 +906,32 @@ public class SnarkManager implements CompleteListener {
}
// Standalone (app context) language.
// lang will generally be null since it is hidden from the form if in router context.
if (lang != null && !_context.isRouterContext() &&
lang.length() >= 2 && lang.length() <= 6) {
int under = lang.indexOf('_');
String nlang, ncountry;
if (under > 0 && lang.length() > under + 1) {
nlang = lang.substring(0, under);
ncountry = lang.substring(under + 1);
} else {
nlang = lang;
ncountry = "";
}
String olang = _config.getProperty(PROP_LANG);
String ocountry = _config.getProperty(PROP_COUNTRY);
if (!nlang.equals(olang) || !ncountry.equals(ocountry)) {
changed = true;
_config.setProperty(PROP_LANG, nlang);
_config.setProperty(PROP_COUNTRY, ncountry);
Translate.setLanguage(nlang, ncountry);
}
}
// Start of I2CP stuff.
// i2cpHost will generally be null since it is hidden from the form if in router context.

View File

@ -0,0 +1,122 @@
package org.klomp.snark.standalone;
import net.i2p.I2PAppContext;
import net.i2p.util.Translate;
/**
* Standalone (app context) only.
* Copied from ConfigUIHelper.
* @since 0.9.27
*/
public class ConfigUIHelper {
private static final String CHECKED = " selected=\"selected\" ";
private static final String BUNDLE_NAME = "org.klomp.snark.web.messages";
private static final String COUNTRY_BUNDLE_NAME = "net.i2p.router.countries.messages";
/**
* Each language has the ISO code, the flag, the name, and the optional country name.
* Alphabetical by the ISO code please.
* See http://en.wikipedia.org/wiki/ISO_639-1 .
* Any language-specific flag added to the icon set must be
* added to the top-level build.xml for the updater.
* As of 0.9.12, ISO 639-2 three-letter codes are supported also.
*
* Note: we don't currently _x the language strings,
* we'll just rely on the JVM's translations for now.
* Country flag unused.
*/
private static final String langs[][] = {
{ "ar", "lang_ar", "Arabic", null },
{ "cs", "cz", "Czech", null },
//{ "da", "dk", "Danish", null },
{ "de", "de", "German", null },
//{ "et", "ee", "Estonian", null },
//{ "el", "gr", "Greek", null },
{ "en", "us", "English", null },
{ "es", "es", "Spanish", null },
{ "fi", "fi", "Finnish", null },
{ "fr", "fr", "French", null },
{ "hu", "hu", "Hungarian", null },
{ "it", "it", "Italian", null },
{ "ja", "jp", "Japanese", null },
//{ "mg", "mg", "Malagasy", null },
{ "nl", "nl", "Dutch", null },
{ "nb", "no", "Norwegian Bokmaal", null },
{ "pl", "pl", "Polish", null },
{ "pt", "pt", "Portuguese", null },
{ "pt_BR", "br", "Portuguese", "Brazil" },
{ "ro", "ro", "Romanian", null },
{ "ru", "ru", "Russian", null },
{ "sk", "sk", "Slovak", null },
{ "sv", "se", "Swedish", null },
{ "tr", "tr", "Turkish", null },
{ "uk", "ua", "Ukrainian", null },
{ "vi", "vn", "Vietnamese", null },
{ "zh", "cn", "Chinese", null },
//{ "zh_TW", "tw", "Chinese", "Taiwan" },
{ "xx", "a1", "Debug: Find untagged strings", null },
};
/**
* Standalone (app context) only.
* Copied from ConfigUIHelper.
* @return HTML
* @since 0.9.27
*/
public static String getLangSettings(I2PAppContext ctx) {
String clang = Translate.getLanguage(ctx);
String current = clang;
String country = Translate.getCountry(ctx);
if (country != null && country.length() > 0)
current += '_' + country;
// find best match
boolean found = false;
for (int i = 0; i < langs.length; i++) {
if (langs[i][0].equals(current)) {
found = true;
break;
}
}
if (!found) {
if (country != null && country.length() > 0) {
current = clang;
for (int i = 0; i < langs.length; i++) {
if (langs[i][0].equals(current)) {
found = true;
break;
}
}
}
if (!found)
current = "en";
}
StringBuilder buf = new StringBuilder(512);
buf.append("<select name=\"lang\">\n");
for (int i = 0; i < langs.length; i++) {
String lang = langs[i][0];
if (lang.equals("xx") && !isAdvanced())
continue;
buf.append("<option ");
if (lang.equals(current))
buf.append(CHECKED);
buf.append("value=\"").append(lang).append("\">");
int under = lang.indexOf('_');
String slang = (under > 0) ? lang.substring(0, under) : lang;
// we don't actually have translations for these, see above
buf.append(Translate.getDisplayLanguage(slang, langs[i][2], ctx, BUNDLE_NAME));
String name = langs[i][3];
if (name != null) {
buf.append(" (")
.append(Translate.getString(name, ctx, COUNTRY_BUNDLE_NAME))
.append(')');
}
buf.append("</option>\n");
}
buf.append("</select>\n");
return buf.toString();
}
/** if necessary */
private static boolean isAdvanced() { return false; }
}

View File

@ -44,6 +44,7 @@ import org.klomp.snark.Storage;
import org.klomp.snark.Tracker;
import org.klomp.snark.TrackerClient;
import org.klomp.snark.dht.DHT;
import org.klomp.snark.standalone.ConfigUIHelper;
/**
* Refactored to eliminate Jetty dependencies.
@ -954,6 +955,7 @@ public class I2PSnarkServlet extends BasicServlet {
} else
*****/
if (newURL != null) {
newURL = newURL.trim();
String newDir = req.getParameter("nofilter_newDir");
File dir = null;
if (newDir != null) {
@ -1141,9 +1143,10 @@ public class I2PSnarkServlet extends BasicServlet {
boolean useDHT = req.getParameter("useDHT") != null;
//String openTrackers = req.getParameter("openTrackers");
String theme = req.getParameter("theme");
String lang = req.getParameter("lang");
_manager.updateConfig(dataDir, filesPublic, autoStart, smartSort, refreshDel, startupDel, pageSize,
seedPct, eepHost, eepPort, i2cpHost, i2cpPort, i2cpOpts,
upLimit, upBW, useOpenTrackers, useDHT, theme);
upLimit, upBW, useOpenTrackers, useDHT, theme, lang);
// update servlet
try {
setResourceBase(_manager.getDataDir());
@ -2198,9 +2201,19 @@ public class I2PSnarkServlet extends BasicServlet {
+ (smartSort ? "checked " : "")
+ "title=\"");
out.write(_t("If checked, ignore words such as 'the' when sorting"));
out.write("\" >" +
out.write("\" >");
"<tr><td>");
if (!_context.isRouterContext()) {
try {
out.write("<tr><td>");
out.write(_t("Language"));
out.write(": <td>");
// class only in standalone builds
out.write(ConfigUIHelper.getLangSettings(_context));
} catch (Throwable t) {}
}
out.write("<tr><td>");
out.write(_t("Theme"));
out.write(": <td><select name='theme'>");
String theme = _manager.getTheme();
@ -2358,7 +2371,7 @@ public class I2PSnarkServlet extends BasicServlet {
"<tr><td colspan=\"2\">&nbsp;\n" + // spacer
"</table></div></div></form>");
}
/** @since 0.9 */
private void writeTrackerForm(PrintWriter out, HttpServletRequest req) throws IOException {
StringBuilder buf = new StringBuilder(1024);