001 ///////////////////////////////////////////////// 002 // This file is part of Sears project. 003 // Subtitle Editor And Re-Synch 004 // A tool to easily modify and resynch movies subtitles. 005 ///////////////////////////////////////////////// 006 //This program is free software; 007 //you can redistribute it and/or modify it under the terms 008 //of the GNU General Public License 009 //as published by the Free Software Foundation; 010 //either version 2 of the License, or (at your option) any later version. 011 ///////////////////////////////////////////////// 012 //Sears project is available under sourceforge 013 // at adress: http://sourceforge.net/projects/sears/ 014 //Copyright (C) 2005 Booba Skaya 015 //Mail: booba.skaya@gmail.com 016 //////////////////////////////////////////////// 017 package sears.tools; 018 019 import java.util.Locale; 020 import java.util.ResourceBundle; 021 022 import javax.swing.Icon; 023 024 import sears.gui.resources.SearsResources; 025 026 /** 027 * Class SearsResourceBundle. 028 * <br><b>Summary:</b><br> 029 * This class permits to access to the string resources in the choosen locale. 030 */ 031 public class SearsResourceBundle { 032 /** Languages supported by Sears, array used by the options dialog */ 033 public static final String[][] LOCALES = { 034 { "en_US", "English" }, 035 { "fr_FR", "Fran\u00e7ais" }, 036 { "it_IT", "Italiano" }, 037 { "es_LA", "Spanish" }, 038 { "nl_NL", "Nederlands" } 039 }; 040 041 /**The locale choosen*/ 042 private Locale locale; 043 /**The resource bundle that correspond to the locale.*/ 044 private ResourceBundle resourceBundle; 045 /**The instance of SearsResourceBundle.*/ 046 private static SearsResourceBundle instance; 047 /**The name of the resource bundles.*/ 048 private static final String RESOURCE_NAME = "MessageBundle"; 049 050 /** 051 * Constructor SearsResourceBundle. 052 * <br><b>Summary:</b><br> 053 * Constructor of the class. 054 */ 055 public SearsResourceBundle(){ 056 this(new Locale("en", "US")); 057 } 058 059 /** 060 * Constructor SearsResourceBundle. 061 * <br><b>Summary:</b><br> 062 * Constructor of the class. 063 */ 064 public SearsResourceBundle(Locale locale){ 065 instance = this; 066 setLocale(locale); 067 } 068 069 /** 070 * Method setLocale. 071 * <br><b>Summary:</b><br> 072 * Change the locale to the specified one. 073 * @param locale The new locale to set. 074 */ 075 public static void setLocale(Locale locale){ 076 instance.locale=locale; 077 instance.resourceBundle = ResourceBundle.getBundle(RESOURCE_NAME, locale); 078 } 079 080 /** 081 * Method getResource. 082 * <br><b>Summary:</b><br> 083 * This method permits to return a resources associated to the given key in the resource bundle. 084 * It returns "", if key is not found in resource bundle. 085 * @param key The key of the value to found. 086 * @return <b>String</b> The resources associated to the given key in the resource bundle. 087 */ 088 public static String getResource(String key){ 089 return getResource(key, ""); 090 } 091 092 /** 093 * Method getResource. 094 * <br><b>Summary:</b><br> 095 * This method permits to return a resources associated to the given key in the resource bundle. 096 * It returns the default value, if key is not found in resource bundle. 097 * @param key The key of the value to found. 098 * @param defaultValue The default value to return, if key is not found. 099 * @return <b>String</b> The resources associated to the given key in the resource bundle. 100 */ 101 public static String getResource(String key, String defaultValue){ 102 //The result of the method 103 String result = defaultValue; 104 //try to get the resource from resourceBundle. 105 result = instance.resourceBundle.getString(key); 106 //if we did not found the searched value, return default value. 107 if(result == null){ 108 result = defaultValue; 109 } 110 //return the result 111 return result; 112 } 113 114 /** 115 * Method getLocale. 116 * <br><b>Summary:</b><br> 117 * Return the Locale. 118 * @return <b>Locale</b> The Locale. 119 */ 120 public static Locale getLocale(){ 121 return instance.locale; 122 } 123 124 /** 125 * Converts a 'locale' string ("en_US") to a proper string to display 126 * ("English") using <tt>LOCALES</tt> class constant 127 * @param locale the string to convert 128 * @return the converted string or null if there's no entries in <tt>LOCALES</tt> class constant 129 */ 130 public static String getStringForLocaleString(String locale) { 131 String str = null; 132 if( locale != null ) { 133 for( int i=0;i<LOCALES.length;i++ ) { 134 if( locale.contentEquals(LOCALES[i][0])) { 135 str = LOCALES[i][1]; 136 } 137 } 138 } 139 return str; 140 } 141 142 /** 143 * Gets the icon which correspond to the locale string 144 * @param locale the locale string ("en_US) 145 * @return the corresponding icon or a blank icon if there's no resources for this locale 146 */ 147 public static Icon getIconForLocaleString(String locale) { 148 return SearsResources.getIcon(locale.substring(0, locale.indexOf("_"))); 149 } 150 151 /** 152 * Returns an array of string which contains all the language names 153 * available on Sears. 154 * <br>This method use the <tt>LOCALES</tt> constant to do that 155 * @return an array of language names 156 */ 157 public static String[] getAllAvailableLanguage() { 158 String[] locales = new String[LOCALES.length]; 159 for( int i=0;i<LOCALES.length;i++ ) { 160 locales[i] = LOCALES[i][0]; 161 } 162 return locales; 163 } 164 }