/*
* Created on 09-ene-2006
*
* TODO To change the template for this generated file go to Window -
* Preferences - Java - Code Style - Code Templates
*/
package org.herac.tuxguitar.gui.system.language;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import org.apache.log4j.Logger;
import org.herac.tuxguitar.gui.editors.chord.ChordSelector;
import org.herac.tuxguitar.gui.util.TGFileUtils;
/**
* @author julian
*
*/
public class LanguageManager {
public static final String EXTENSION = ".properties";
public static final String PACKAGE = "lang";
public static final String PREFIX = "messages";
private String[] languages;
private List<LanguageLoader> loaders;
private TGResourceBundle resources;
public LanguageManager() {
this.loaders = new ArrayList<LanguageLoader>();
this.loadLanguages();
}
public void addLoader(LanguageLoader loader) {
if (!this.loaders.contains(loader)) {
this.loaders.add(loader);
}
}
private void fireChanges() {
for (final LanguageLoader loader : this.loaders) {
loader.loadProperties();
}
}
public String getLanguage() {
if (this.resources != null) {
Locale locale = this.resources.getLocale();
boolean language = (locale.getLanguage() != null && locale.getLanguage()
.length() > 0);
boolean country = (locale.getCountry() != null && locale.getCountry()
.length() > 0);
boolean variant = (locale.getVariant() != null && locale.getVariant()
.length() > 0);
String localeId = new String();
if (language) {
localeId += locale.getLanguage();
}
if (country) {
localeId += "_" + locale.getCountry();
}
if (variant) {
localeId += "_"
+ (country ? locale.getVariant() : ("_" + locale.getVariant()));
}
return localeId;
}
return null;
}
public String[] getLanguages() {
return this.languages;
}
private Locale getLocale(String lang) {
if (this.isSupportedLanguage(lang)) {
String[] locale = lang.split("_");
String language = (locale.length > 0 ? locale[0] : "");
String country = (locale.length > 1 ? locale[1] : "");
String variant = (locale.length > 2 ? locale[2] : "");
return new Locale(language, country, variant);
}
return Locale.getDefault();
}
public String getProperty(String key) {
return this.getProperty(key, key);
}
public String getProperty(String key, Object[] arguments) {
return getProperty(key, key, arguments);
}
public String getProperty(String key, String value) {
try {
String property = this.resources.getString(key);
return (property == null ? value : property);
} catch (Throwable throwable) {
return value;
}
}
public String getProperty(String key, String value, Object[] arguments) {
String property = this.getProperty(key, value);
return (arguments != null ? MessageFormat.format(property, arguments)
: property);
}
private boolean isSupportedLanguage(String lang) {
if (lang != null && lang.length() > 0 && this.languages != null) {
for (int i = 0; i < this.languages.length; i++) {
if (this.languages[i].equals(lang)) {
return true;
}
}
}
return false;
}
/**
* Load language files from lang folder
*
*/
private void loadLanguages() {
List<String> availableList = new ArrayList<String>();
String[] fileNames = TGFileUtils.getFileNames("lang");
if (fileNames != null) {
// now iterate over them
for (int i = 0; i < fileNames.length; i++) {
if (fileNames[i].indexOf("messages_") == 0) {
int prefixIndex = fileNames[i].indexOf(PREFIX + "_");
int extensionIndex = fileNames[i].indexOf(EXTENSION);
if (prefixIndex == 0 && extensionIndex > (PREFIX + "_").length()) {
availableList.add(fileNames[i].substring((PREFIX + "_").length(),
extensionIndex));
}
}
}
}
this.languages = new String[availableList.size()];
for (int i = 0; i < this.languages.length; i++) {
this.languages[i] = (String) availableList.get(i);
}
}
public void removeLoader(LanguageLoader loader) {
if (this.loaders.contains(loader)) {
this.loaders.remove(loader);
}
}
/** The Logger for this class. */
public static final transient Logger LOG = Logger
.getLogger(LanguageManager.class);
public void setLanguage(String lang) {
try {
String baseName = (PACKAGE + "." + PREFIX);
Locale locale = getLocale(lang);
this.resources = TGResourceBundle.getBundle(baseName, locale);
this.fireChanges();
} catch (Exception e) {
LOG.error(e);
}
}
}