001    /*
002     * Strings
003     *
004     * Copyright (c) 2001, 2002, 2003 Marco Schmidt.
005     * All rights reserved.
006     */
007    
008    package net.sourceforge.jiu.apps;
009    
010    import java.util.Hashtable;
011    import java.util.Locale;
012    
013    /**
014     * String resource for the various apps.
015     * Each index value from {@link StringIndexConstants} has a corresponding String value
016     * for all supported natural languages.
017     * @author Marco Schmidt
018     */
019    public class Strings implements StringIndexConstants
020    {
021            /**
022             * Constant int value for the natural language <em>English</em>.
023             * */
024            public static final Integer LANG_ENGLISH = new Integer(0);
025    
026            /**
027             * Constant int value for the natural language <em>German</em>.
028             */
029            public static final Integer LANG_GERMAN = new Integer(1);
030    
031            /**
032             * Constant int value for the natural language <em>Spanish</em>.
033             */
034            public static final Integer LANG_SPANISH = new Integer(2);
035    
036            /**
037             * Constant of the default language, {@link #LANG_ENGLISH}.
038             */
039            public static final Integer DEFAULT_LANGUAGE = LANG_ENGLISH;
040    
041            /**
042             * ISO 639 two-letter country codes for the supported languages, lower case.
043             */
044            private static final String[] ISO_639_LANGUAGE_CODES =
045            {
046                    "en",
047                    "de",
048                    "es",
049            };
050    
051            private static final Integer[] LANGUAGE_CONSTANTS =
052            {
053                    LANG_ENGLISH, 
054                    LANG_GERMAN, 
055                    LANG_SPANISH,
056            };
057    
058            /**
059             * The ISO 639 code for the default language {@link #DEFAULT_LANGUAGE}.
060             */
061            public static final String DEFAULT_LANGUAGE_ISO_639_CODE = ISO_639_LANGUAGE_CODES[DEFAULT_LANGUAGE.intValue()];
062    
063            /**
064             * A hashtable that maps from ISO 639 country codes to Integer
065             * objects with the corresponding LANG_xyz constant for that language.
066             */
067            private static Hashtable isoToConstant;
068    
069            static
070            {
071                    isoToConstant = new Hashtable(ISO_639_LANGUAGE_CODES.length);
072                    for (int i = 0; i < ISO_639_LANGUAGE_CODES.length; i++)
073                    {
074                            isoToConstant.put(ISO_639_LANGUAGE_CODES[i], LANGUAGE_CONSTANTS[i]);
075                    }
076            }
077    
078            private String[] data;
079            private Integer language;
080    
081            /**
082             * Create a new String object for the given language and fill it
083             * with the String array. 
084             */
085            public Strings(Integer languageConstant, String[] stringValues)
086            {
087                    set(languageConstant, stringValues);
088            }
089    
090            /**
091             * Determines an ISO 639 code of a language suitable for the environment
092             * in which the JVM is currently running.
093             * First calls {@link #determineIsoCodeFromDefaultLocale()}.
094             * If that yields null, the ISO code for {@link #DEFAULT_LANGUAGE} is returned.
095             * So different from {@link #determineIsoCodeFromDefaultLocale()}
096             * this method always returns a non-null value.
097             * @return String with ISO 639 code of a language that fits the JVM environment,
098             *  or the default language as fallback solution 
099             */
100            public static String determineSuitableIsoCode()
101            {
102                    String code = determineIsoCodeFromDefaultLocale();
103                    if (code != null)
104                    {
105                            return code;
106                    }
107                    else
108                    {
109                            return ISO_639_LANGUAGE_CODES[DEFAULT_LANGUAGE.intValue()];
110                    }
111            }
112    
113            public static String determineIsoCodeFromDefaultLocale()
114            {
115                    Locale locale = Locale.getDefault();
116                    if (locale == null)
117                    {
118                            return null;
119                    }
120                    return locale.getLanguage();
121            }
122    
123            public static Integer findLanguageCode(String iso639LanguageCode)
124            {
125                    if (iso639LanguageCode == null)
126                    {
127                            return null;
128                    }
129                    String code = iso639LanguageCode.toLowerCase();
130                    return (Integer)isoToConstant.get(code);
131            }
132    
133            /**
134             * Gets the String denoted by the argument index.
135             * This index must be one of the int constants defined in {@link StringIndexConstants}.
136             * @return String with given index in the current language
137             * @throws IllegalArgumentException is not a valid index from {@link StringIndexConstants}
138             */
139            public String get(int index)
140            {
141                    try
142                    {
143                            return data[index];
144                    }
145                    catch (ArrayIndexOutOfBoundsException aioobe)
146                    {
147                            throw new IllegalArgumentException("Not a valid String index: " + index);
148                    }
149            }
150    
151            /**
152             * Returns the language of this object as one of the LANG_xyz 
153             * constants of this class.
154             */
155            public Integer getLanguage()
156            {
157                    return language;
158            }
159    
160            public static String getFileName(int languageCode)
161            {
162                    if (languageCode >= 0 && languageCode < ISO_639_LANGUAGE_CODES.length)
163                    {
164                            return ISO_639_LANGUAGE_CODES[languageCode] + ".txt";
165                    }
166                    else
167                    {
168                            return null;
169                    }
170            }
171    
172            public void set(Integer languageConstant, String[] values)
173            {
174                    if (languageConstant == null || 
175                        languageConstant.intValue() < 0 || 
176                        languageConstant.intValue() >= ISO_639_LANGUAGE_CODES.length)
177                    {
178                            throw new IllegalArgumentException("Not a valid language constant: " + languageConstant);
179                    }
180                    if (values == null || values.length < 1)
181                    {
182                            throw new IllegalArgumentException("The values array argument must be non-null and have at least one element.");
183                    }
184                    language = languageConstant;
185                    data = values;
186            }
187    }