- private Trans() {
- map = new HashMap<StringId, String>();
-
- // TODO: get from a file instead?
- map.put(StringId.NULL, "");
- map.put(StringId.DUMMY, "[dummy]");
- // we could use: " ", "┃", "│"...
- map.put(StringId.DEAULT_FIELD_SEPARATOR, "┃");
- map.put(StringId.DEAULT_FIELD_SEPARATOR_NOUTF, "|");
- map.put(StringId.KEY_ACTION_BACK, "Back");
- map.put(StringId.KEY_ACTION_HELP, "Help");
- map.put(StringId.KEY_ACTION_VIEW_CONTACT, "Open");
- map.put(StringId.KEY_ACTION_VIEW_CARD, "Open");
- map.put(StringId.KEY_ACTION_EDIT_CONTACT, "Edit");
- map.put(StringId.KEY_ACTION_DELETE_CONTACT, "Delete");
- map.put(StringId.KEY_ACTION_SWITCH_FORMAT, "Change view");
- map.put(StringId.KEY_ACTION_INVERT, "Invert colours");
- map.put(StringId.KEY_ACTION_FULLSCREEN, "Fullscreen");
- map.put(StringId.KEY_ACTION_SEARCH, "Search");
+ /**
+ * Initialise the translation mappings for the given language.
+ *
+ * @param lang
+ * the language to initialise
+ */
+ private void setLanguage(String lang) {
+ Locale locale = null;
+
+ if (lang == null) {
+ locale = Locale.getDefault();
+ } else {
+ locale = Locale.forLanguageTag(lang);
+ }
+
+ map = Bundles.getBundle("resources", locale);
+ }
+
+ /**
+ * Create/update the translation .properties files. Will use the most likely
+ * candidate as base if the file does not already exists (for instance,
+ * "en_US" will use "en" as a base).
+ *
+ * @param args
+ * the path where the .properties files are, then the languages
+ * to create/update
+ *
+ * @throws IOException
+ * in case of IO errors
+ */
+ public static void main(String[] args) throws IOException {
+ String path = args[0];
+ for (int i = 1; i < args.length; i++) {
+ Locale locale = Locale.forLanguageTag(args[i].replaceAll("_", "-"));
+ String code = locale.toString();
+ Trans trans = new Trans(code);
+
+ File file = null;
+ if (code.length() > 0) {
+ file = new File(path + "resources_" + code + ".properties");
+ } else {
+ // Default properties file:
+ file = new File(path + "resources.properties");
+ }
+
+ BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(
+ new FileOutputStream(file), "UTF-8"));
+
+ String name = locale.getDisplayCountry(locale);
+ if (name.length() == 0)
+ name = locale.getDisplayLanguage(locale);
+ if (name.length() == 0)
+ name = "default";
+
+ if (code.length() > 0) {
+ name = name + " (" + code + ")";
+ }
+
+ writer.append("# " + name + " translation file (UTF-8)\n");
+ writer.append("# \n");
+ writer.append("# Note that any key can be doubled with a _NOUTF suffix\n");
+ writer.append("# to use when the flag --noutf is passed\n");
+ writer.append("# \n");
+ writer.append("# Also, the comments always refer to the key below them.\n");
+ writer.append("# \n");
+ writer.append("\n");
+
+ for (Field field : StringId.class.getDeclaredFields()) {
+ Meta meta = field.getAnnotation(Meta.class);
+ if (meta != null) {
+ StringId id = StringId.valueOf(field.getName());
+ String info = getMetaInfo(meta);
+ if (info != null) {
+ writer.append(info);
+ writer.append("\n");
+ }
+
+ writer.append(id.name());
+ writer.append(" = ");
+ if (!trans.trans(id).equals(id.name()))
+ writer.append(trans.trans(id));
+ writer.append("\n");
+ }
+ }
+
+ writer.close();
+ }
+ }
+
+ /**
+ * Return formated, display-able information from the {@link Meta} field
+ * given. Each line will always starts with a "#" character.
+ *
+ * @param meta
+ * the {@link Meta} field
+ *
+ * @return the information to display or NULL if none
+ */
+ private static String getMetaInfo(Meta meta) {
+ String what = meta.what();
+ String where = meta.where();
+ String format = meta.format();
+ String info = meta.info();
+
+ int opt = what.length() + where.length() + format.length();
+ if (opt + info.length() == 0)
+ return null;
+
+ StringBuilder builder = new StringBuilder();
+ builder.append("# ");
+
+ if (opt > 0) {
+ builder.append("(");
+ if (what.length() > 0) {
+ builder.append("WHAT: " + what);
+ if (where.length() + format.length() > 0)
+ builder.append(", ");
+ }
+
+ if (where.length() > 0) {
+ builder.append("WHERE: " + where);
+ if (format.length() > 0)
+ builder.append(", ");
+ }
+
+ if (format.length() > 0) {
+ builder.append("FORMAT: " + format);
+ }
+
+ builder.append(")\n# ");
+ }
+
+ builder.append(info);
+
+ return builder.toString();