001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2007, Geotools Project Managment Committee (PMC)
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: */
016: package org.geotools.resources.i18n;
017:
018: import java.util.Arrays;
019: import java.util.Locale;
020: import org.geotools.resources.Arguments;
021: import org.geotools.resources.XArray;
022:
023: /**
024: * Static i18n methods.
025: *
026: * @since 2.4
027: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/metadata/src/main/java/org/geotools/resources/i18n/Locales.java $
028: * @version $Id: Locales.java 26151 2007-07-04 18:54:48Z desruisseaux $
029: * @author Martin Desruisseaux
030: */
031: public final class Locales {
032: /**
033: * Do not allow instantiation of this class.
034: */
035: private Locales() {
036: }
037:
038: /**
039: * Returns available locales.
040: *
041: * @todo Current implementation returns a hard-coded list.
042: * Future implementations may perform a more intelligent work.
043: */
044: public static Locale[] getAvailableLanguages() {
045: return new Locale[] { Locale.ENGLISH, Locale.FRENCH,
046: Locale.GERMAN
047: // TODO: missing constants for SPANISH, PORTUGUES and GREEK
048: };
049: }
050:
051: /**
052: * Returns the list of available locales.
053: */
054: public static Locale[] getAvailableLocales() {
055: final Locale[] languages = getAvailableLanguages();
056: Locale[] locales = Locale.getAvailableLocales();
057: int count = 0;
058: for (int i = 0; i < locales.length; i++) {
059: final Locale locale = locales[i];
060: if (containsLanguage(languages, locale)) {
061: locales[count++] = locale;
062: }
063: }
064: locales = (Locale[]) XArray.resize(locales, count);
065: return locales;
066: }
067:
068: /**
069: * Returns {@code true} if the specified array of locales contains at least
070: * one element with the specified language.
071: */
072: private static boolean containsLanguage(final Locale[] locales,
073: final Locale language) {
074: final String code = language.getLanguage();
075: for (int i = 0; i < locales.length; i++) {
076: if (code.equals(locales[i].getLanguage())) {
077: return true;
078: }
079: }
080: return false;
081: }
082:
083: /**
084: * Returns the list of available locales formatted as string in the specified locale.
085: */
086: public static String[] getAvailableLocales(final Locale locale) {
087: final Locale[] locales = getAvailableLocales();
088: final String[] display = new String[locales.length];
089: for (int i = 0; i < locales.length; i++) {
090: display[i] = locales[i].getDisplayName(locale);
091: }
092: Arrays.sort(display);
093: return display;
094: }
095:
096: /**
097: * Prints the list of available locales.
098: */
099: public static void main(String[] args) {
100: final Arguments arguments = new Arguments(args);
101: args = arguments.getRemainingArguments(0);
102: final String[] locales = getAvailableLocales(arguments.locale);
103: for (int i = 0; i < locales.length; i++) {
104: arguments.out.println(locales[i]);
105: }
106: }
107: }
|