You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

265 lines
9.1 KiB

package gui;
import com.sun.javafx.collections.ObservableListWrapper;
import javafx.beans.binding.Bindings;
import javafx.beans.binding.ObjectBinding;
import javafx.beans.binding.StringBinding;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.Tooltip;
import java.io.UnsupportedEncodingException;
import java.text.MessageFormat;
import java.util.*;
import java.util.concurrent.Callable;
public final class I18N {
private static final ObjectProperty<Locale> locale;
static {
locale = new SimpleObjectProperty<>(getDefaultLocale());
locale.addListener((observable, oldValue, newValue) -> Locale.setDefault(newValue));
}
/**
* get the supported Locales.
*
* @return List of Locale objects.
*/
public static List<Locale> getSupportedLocales() {
return new ArrayList<>(Arrays.asList(new Locale.Builder().setLanguage("sl").setRegion("SI").build(), Locale.ENGLISH));
}
/**
* get the default locale. This is the systems default if contained in the supported locales, english otherwise.
*
* @return
*/
public static Locale getDefaultLocale() {
Locale sysDefault = Locale.getDefault();
return getSupportedLocales().contains(sysDefault) ? sysDefault : new Locale.Builder().setLanguage("sl").setRegion("SI").build();
}
public static Locale getLocale() {
return locale.get();
}
public static void setLocale(Locale locale) {
localeProperty().set(locale);
Locale.setDefault(locale);
}
public static ObjectProperty<Locale> localeProperty() {
return locale;
}
/**
* gets the string with the given key from the resource bundle for the current locale and uses it as first argument
* to MessageFormat.format, passing in the optional args and returning the result.
*
* @param key
* message key
* @param args
* optional arguments for the message
* @return localized formatted string
*/
public static String get(final String key, final Object... args) {
ResourceBundle bundle = ResourceBundle.getBundle("message", getLocale());
String val = bundle.getString(key);
try {
return MessageFormat.format(new String(val.getBytes("ISO-8859-1"), "UTF-8"), args);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return val;
}
public static String getDefaultLocaleItem(final String key, final Object... args) {
ResourceBundle bundle = ResourceBundle.getBundle("message", getDefaultLocale());
String val = bundle.getString(key);
try {
return MessageFormat.format(new String(val.getBytes("ISO-8859-1"), "UTF-8"), args);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return val;
}
public static ObservableList<String> getObject(final ArrayList<String> keys, final Object... args) {
ResourceBundle bundle = ResourceBundle.getBundle("message", getLocale());
ArrayList<String> results = new ArrayList<>();
for(String key : keys){
String val = bundle.getString(key);
try {
results.add(MessageFormat.format(new String(val.getBytes("ISO-8859-1"), "UTF-8"), args));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
return FXCollections.observableArrayList(results);
}
/**
* creates a String binding to a localized String for the given message bundle key
*
* @param key
* key
* @return String binding
*/
public static StringBinding createStringBinding(final String key, Object... args) {
return Bindings.createStringBinding(() -> get(key, args), locale);
}
// public static ObservableValue<ObservableList<String>> createListStringBinding(final String key, Object... args) {
// ObservableList<StringBinding> r = (ObservableList<StringBinding>) new ArrayList<StringBinding>();
// r.add(Bindings.createStringBinding(() -> get(key, args), locale));
// return r;
// }
/**
* creates a Object Binding to a localized Object that is computed by calling the given func
*
* @param func
* function called on every change
* @return StringBinding
*/
public static StringBinding createStringBinding(Callable<String> func) {
return Bindings.createStringBinding(func, locale);
}
/**
* creates a String binding to a localized String for the given message bundle key
*
* @param keys
* key
* @return ObjectBinding
*/
public static ObjectBinding createObjectBinding(final ArrayList<String> keys, Object... args) {
return Bindings.createObjectBinding(() -> getObject(keys, args), locale);
}
// public static ObservableValue<ObservableList<String>> createListStringBinding(final String key, Object... args) {
// ObservableList<StringBinding> r = (ObservableList<StringBinding>) new ArrayList<StringBinding>();
// r.add(Bindings.createStringBinding(() -> get(key, args), locale));
// return r;
// }
/**
* creates a String Binding to a localized String that is computed by calling the given func
*
* @param func
* function called on every change
* @return ObjectBinding
*/
public static ObjectBinding createObjectBinding(Callable<String> func) {
return Bindings.createObjectBinding(func, locale);
}
public static String getIndependent(final String key, Locale locale, final Object... args) {
ResourceBundle bundle = ResourceBundle.getBundle("message", locale);
String val = bundle.getString(key);
try {
return MessageFormat.format(new String(val.getBytes("ISO-8859-1"), "UTF-8"), args);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return val;
// return MessageFormat.format(bundle.getString(key), args);
}
public static String getRootValue(String oldValue, ArrayList<String> nGramComputeForLetters) {
Locale loc;
if(getLocale().equals(Locale.ENGLISH)) {
loc = new Locale.Builder().setLanguage("sl").setRegion("SI").build();
} else {
loc = Locale.ENGLISH;
}
for (String el : nGramComputeForLetters){
if (oldValue.equals(getIndependent(el, loc))){
return el;
}
}
// in case translated language doesn't contain specified word, try original language
for (String el : nGramComputeForLetters){
if (oldValue.equals(get(el))){
return el;
}
}
return null;
}
public static String getTranslatedValue(String oldValue, ArrayList<String> nGramComputeForLetters) {
Locale loc;
if(getLocale().equals(Locale.ENGLISH)) {
loc = new Locale.Builder().setLanguage("sl").setRegion("SI").build();
} else {
loc = Locale.ENGLISH;
}
for (String el : nGramComputeForLetters){
if (oldValue.equals(getIndependent(el, loc))){
return get(el);
}
}
// in case translated language doesn't contain specified word, try original language
for (String el : nGramComputeForLetters){
if (oldValue.equals(get(el))){
return get(el);
}
}
return null;
}
/**
* Returns translated FXCollection
*
* @param words
* function called on every change
* @return ObjectBinding
*/
public static ObservableList<String> translatedObservableList(ArrayList<String> words){
ArrayList<String> translatedWords = new ArrayList<>();
for (String word : words){
translatedWords.add(get(word));
}
return FXCollections.observableArrayList(translatedWords);
}
/**
* DUPLICATE OF toString()
* searches for possible values in translations and returns key of the string
* == .toString()
*
* @param w, prefix
* function called on every change
* @return ObjectBinding
*/
public static String findI18NString(String w, String prefix){
ResourceBundle bundle = ResourceBundle.getBundle("message", getLocale());
for (String key : bundle.keySet()){
if(prefix.length() > key.length() || !key.substring(0, prefix.length()).equals(prefix)){
continue;
}
String val = bundle.getString(key);
try {
String newVal = new String(val.getBytes("ISO-8859-1"), "UTF-8");
if (newVal.equals(w)){
return key;
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
return null;
}
}