Moved currently unused i18n code out of build path.

This commit is contained in:
akwizgran
2012-10-30 20:37:31 +00:00
parent e6ac5494fc
commit a66da73d37
11 changed files with 0 additions and 5 deletions

View File

@@ -1,23 +0,0 @@
package net.sf.briar.api.i18n;
import java.awt.Font;
import java.io.File;
import java.util.Locale;
public interface FontManager {
/**
* Initializes the FontManager for the given locale. Fonts are loaded from
* the given directory if they cannot be loaded from the running jar.
*/
void initialize(Locale locale, File dir);
/** Returns the appropriate font for the given language. */
Font getFontForLanguage(String language);
/** Returns the current user interface font. */
Font getUiFont();
/** Sets the user interface font appropriately for the given language. */
void setUiFontForLanguage(String language);
}

View File

@@ -1,53 +0,0 @@
package net.sf.briar.api.i18n;
import java.awt.ComponentOrientation;
import java.awt.Font;
import java.io.File;
import java.io.IOException;
import java.util.Locale;
public interface I18n {
/** Returns the named string, translated for the current i18n locale. */
String tr(String name);
/** Returns the i18n locale. This may not match the system locale. */
Locale getLocale();
/** Sets the i18n locale. */
void setLocale(Locale locale);
/** Loads the i18n locale from Briar/Data/locale.cfg. */
void loadLocale() throws IOException;
/** Saves the i18n locale to Briar/Data/locale.cfg. */
void saveLocale() throws IOException;
/** Loads the i18n locale from the given file. */
void loadLocale(File f) throws IOException;
/** Saves the i18n locale to the given file. */
void saveLocale(File f) throws IOException;
/** Returns the ComponentOrientation of the current i18n locale. */
ComponentOrientation getComponentOrientation();
/** Registers a listener for changes to the i18n locale. */
void addListener(Listener l);
/** Unregisters a listener for changes to the i18n locale. */
void removeListener(Listener l);
/**
* Implemented by classes that wish to be informed of changes to the i18n
* locale.
*/
public interface Listener {
/**
* Called whenever the i18n locale changes.
* @param uiFont The user interface font for the new locale.
*/
void localeChanged(Font uiFont);
}
}

View File

@@ -1,49 +0,0 @@
package net.sf.briar.api.i18n;
/** A named translatable string. */
public class Stri18ng {
private static final String HTML_OPEN_LEFT = "<html><body align='left'>";
private static final String HTML_OPEN_RIGHT = "<html><body align='right'>";
private static final String HTML_CLOSE = "</body></html>";
private static final String PARAGRAPH = "<p><p>"; // Yes, two of them
private final String name;
private final I18n i18n;
public Stri18ng(String name, I18n i18n) {
this.name = name;
this.i18n = i18n;
}
/** Returns the string translated for the current i18n locale. */
public String tr() {
return i18n.tr(name);
}
/** Returns the string, translated for the current i18n locale, as HTML. */
public String html() {
if(i18n.getComponentOrientation().isLeftToRight())
return HTML_OPEN_LEFT + i18n.tr(name) + HTML_CLOSE;
else return HTML_OPEN_RIGHT + i18n.tr(name) + HTML_CLOSE;
}
/**
* Returns the string, translated for the current locale, as HTML.
* @param paras Additional (pre-translated) paragraphs that should be
* appended to the HTML.
*/
public String html(String... paras) {
StringBuilder s = new StringBuilder();
if(i18n.getComponentOrientation().isLeftToRight())
s.append(HTML_OPEN_LEFT);
else s.append(HTML_OPEN_RIGHT);
s.append(tr());
for(String para : paras) {
s.append(PARAGRAPH);
s.append(para);
}
s.append(HTML_CLOSE);
return s.toString();
}
}

View File

@@ -1,108 +0,0 @@
package net.sf.briar.i18n;
import java.awt.Font;
import java.awt.FontFormatException;
import java.awt.font.TextAttribute;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.UIManager;
import net.sf.briar.api.i18n.FontManager;
// Needs to be public for installer
public class FontManagerImpl implements FontManager {
private static final Logger LOG =
Logger.getLogger(FontManagerImpl.class.getName());
/**
* Each bundled font is associated with a size, which is meant to occupy
* roughly the same amount of space as the default font (12 point sans),
* and a list of languages for which the bundled font should be used.
*/
private static final BundledFont[] BUNDLED_FONTS = {
// Use TibetanMachineUni for Tibetan
new BundledFont("TibetanMachineUni.ttf", 14f, new String[] { "bo" }),
// Use Padauk for Burmese
new BundledFont("Padauk.ttf", 14f, new String[] { "my" }),
// Use DroidSansFallback for Chinese, Japanese and Korean
new BundledFont("DroidSansFallback.ttf", 12f,
new String[] { "zh" , "ja", "ko" })
};
// Map from languages to fonts
private final Map<String, Font> fonts = new HashMap<String, Font>();
private volatile Font defaultFont = null, uiFont = null;
public void initialize(Locale locale, File dir) {
// Look for bundled fonts in the jar and the filesystem. If any fonts
// are missing or fail to load, fall back to the default font.
ClassLoader loader = getClass().getClassLoader();
for(BundledFont bf : BUNDLED_FONTS) {
try {
InputStream in = loader.getResourceAsStream(bf.filename);
if(in == null)
in = new FileInputStream(new File(dir, bf.filename));
Font font = Font.createFont(Font.TRUETYPE_FONT, in);
font = font.deriveFont(bf.size);
for(String language : bf.languages) fonts.put(language, font);
} catch(FontFormatException e) {
if(LOG.isLoggable(Level.WARNING)) LOG.warning(e.toString());
} catch(IOException e) {
if(LOG.isLoggable(Level.WARNING)) LOG.warning(e.toString());
}
}
defaultFont = getFont("Sans", 12f);
assert defaultFont != null; // FIXME: This is failing on Windows
setUiFontForLanguage(locale.getLanguage());
}
private Font getFont(String name, float size) {
Map<TextAttribute, Object> attr = new HashMap<TextAttribute, Object>();
attr.put(TextAttribute.FAMILY, name);
attr.put(TextAttribute.SIZE, Float.valueOf(size));
return Font.getFont(attr);
}
public Font getFontForLanguage(String language) {
assert defaultFont != null;
Font font = fonts.get(language);
return font == null ? defaultFont : font;
}
public Font getUiFont() {
return uiFont;
}
public void setUiFontForLanguage(String language) {
uiFont = getFontForLanguage(language);
Enumeration<Object> keys = UIManager.getDefaults().keys();
while(keys.hasMoreElements()) {
Object key = keys.nextElement();
if(UIManager.getFont(key) != null) UIManager.put(key, uiFont);
}
}
private static class BundledFont {
private final String filename;
private final float size;
private final String[] languages;
private BundledFont(String filename, float size, String[] languages) {
this.filename = filename;
this.size = size;
this.languages = languages;
}
}
}

View File

@@ -1,165 +0,0 @@
package net.sf.briar.i18n;
import java.awt.ComponentOrientation;
import java.awt.Font;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.util.Collection;
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
import java.util.Scanner;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.UIManager;
import net.sf.briar.api.i18n.FontManager;
import net.sf.briar.api.i18n.I18n;
import net.sf.briar.util.FileUtils;
import com.google.inject.Inject;
// Needs to be public for installer
public class I18nImpl implements I18n {
/**
* Property keys for strings used in the JRE's built-in dialogs. Values
* assigned to these keys in i18n properties files will override the
* built-in values.
*/
private static final String[] uiManagerKeys = {
"FileChooser.acceptAllFileFilterText",
"FileChooser.cancelButtonText",
"FileChooser.cancelButtonToolTipText",
"FileChooser.detailsViewButtonAccessibleName",
"FileChooser.detailsViewButtonToolTipText",
"FileChooser.directoryOpenButtonText",
"FileChooser.directoryOpenButtonToolTipText",
"FileChooser.fileAttrHeaderText",
"FileChooser.fileDateHeaderText",
"FileChooser.fileNameHeaderText",
"FileChooser.fileNameLabelText",
"FileChooser.fileSizeHeaderText",
"FileChooser.filesOfTypeLabelText",
"FileChooser.fileTypeHeaderText",
"FileChooser.helpButtonText",
"FileChooser.helpButtonToolTipText",
"FileChooser.homeFolderAccessibleName",
"FileChooser.homeFolderToolTipText",
"FileChooser.listViewButtonAccessibleName",
"FileChooser.listViewButtonToolTipText",
"FileChooser.lookInLabelText",
"FileChooser.newFolderErrorText",
"FileChooser.newFolderToolTipText",
"FileChooser.openButtonText",
"FileChooser.openButtonToolTipText",
"FileChooser.openDialogTitleText",
"FileChooser.saveButtonText",
"FileChooser.saveButtonToolTipText",
"FileChooser.saveDialogTitleText",
"FileChooser.saveInLabelText",
"FileChooser.updateButtonText",
"FileChooser.updateButtonToolTipText",
"FileChooser.upFolderAccessibleName",
"FileChooser.upFolderToolTipText",
"OptionPane.cancelButtonText",
"OptionPane.noButtonText",
"OptionPane.yesButtonText",
"ProgressMonitor.progressText"
};
private static final Logger LOG =
Logger.getLogger(I18nImpl.class.getName());
private final Object bundleLock = new Object();
private final ClassLoader loader = I18n.class.getClassLoader();
private final Collection<Listener> listeners =
new CopyOnWriteArrayList<Listener>();
private final FontManager fontManager;
private volatile Locale locale = Locale.getDefault();
private volatile ResourceBundle bundle = null;
@Inject
public I18nImpl(FontManager fontManager) {
this.fontManager = fontManager;
}
public String tr(String name) {
loadResourceBundle();
return bundle.getString(name);
}
private void loadResourceBundle() {
if(bundle == null) {
synchronized(bundleLock) {
if(bundle == null) {
bundle = ResourceBundle.getBundle("i18n", locale, loader);
assert bundle != null;
for(String key : uiManagerKeys) {
try {
UIManager.put(key, bundle.getString(key));
} catch(MissingResourceException e) {
if(LOG.isLoggable(Level.WARNING))
LOG.warning(e.toString());
}
}
}
}
}
}
public Locale getLocale() {
return locale;
}
public void setLocale(Locale locale) {
fontManager.setUiFontForLanguage(locale.getLanguage());
Font uiFont = fontManager.getUiFont();
synchronized(bundleLock) {
this.locale = locale;
Locale.setDefault(locale);
bundle = null;
for(Listener l : listeners) l.localeChanged(uiFont);
}
}
public void loadLocale() throws IOException {
loadLocale(new File(FileUtils.getBriarDirectory(), "Data/locale.cfg"));
}
public void loadLocale(File f) throws IOException {
Scanner s = new Scanner(f);
if(s.hasNextLine()) setLocale(new Locale(s.nextLine()));
s.close();
}
public void saveLocale() throws IOException {
saveLocale(new File(FileUtils.getBriarDirectory(), "Data/locale.cfg"));
}
public void saveLocale(File f) throws IOException {
FileOutputStream out = new FileOutputStream(f);
PrintStream print = new PrintStream(out);
print.println(locale);
print.flush();
print.close();
}
public ComponentOrientation getComponentOrientation() {
return ComponentOrientation.getOrientation(locale);
}
public void addListener(Listener l) {
l.localeChanged(fontManager.getUiFont());
listeners.add(l);
}
public void removeListener(Listener l) {
listeners.remove(l);
}
}

View File

@@ -1,16 +0,0 @@
package net.sf.briar.i18n;
import net.sf.briar.api.i18n.FontManager;
import net.sf.briar.api.i18n.I18n;
import com.google.inject.AbstractModule;
import com.google.inject.Singleton;
public class I18nModule extends AbstractModule {
@Override
protected void configure() {
bind(FontManager.class).to(FontManagerImpl.class).in(Singleton.class);
bind(I18n.class).to(I18nImpl.class).in(Singleton.class);
}
}