Changeset 8283 in josm


Ignore:
Timestamp:
2015-04-27T21:03:55+02:00 (9 years ago)
Author:
stoecker
Message:

some i18n code cleanups

Location:
trunk/src/org/openstreetmap/josm
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/osm/AbstractPrimitive.java

    r7783 r8283  
    1818import java.util.concurrent.atomic.AtomicLong;
    1919
     20import org.openstreetmap.josm.tools.LanguageInfo;
    2021import org.openstreetmap.josm.tools.Utils;
    2122
     
    694695
    695696    /**
    696      * Replies the a localized name for this primitive given by the value of the tags (in this order)
    697      * <ul>
    698      *   <li>name:lang_COUNTRY_Variant  of the current locale</li>
    699      *   <li>name:lang_COUNTRY of the current locale</li>
    700      *   <li>name:lang of the current locale</li>
    701      *   <li>name of the current locale</li>
    702      * </ul>
    703      *
    704      * null, if no such tag exists
    705      *
    706      * @return the name of this primitive
     697     * Replies a localized name for this primitive given by the value of the name tags
     698     * accessed from very specific (language variant) to more generic (default name).
     699     *
     700     * @see LanguageInfo#getLanguageCodes()
     701     * @return the name of this primitive, <code>null</code> if no name exists
    707702     */
    708703    @Override
    709704    public String getLocalName() {
    710         final Locale locale = Locale.getDefault();
    711         String key = "name:" + locale.toString();
    712         String val = get(key);
    713         if (val != null)
    714             return val;
    715 
    716         final String language = locale.getLanguage();
    717         key = "name:" + language + "_" + locale.getCountry();
    718         val = get(key);
    719         if (val != null)
    720             return val;
    721 
    722         key = "name:" + language;
    723         val = get(key);
    724         if (val != null)
    725             return val;
     705        for(String s : LanguageInfo.getLanguageCodes(null)) {
     706            String val = get("name:" + s);
     707            if (val != null)
     708                return val;
     709        }
    726710
    727711        return getName();
  • trunk/src/org/openstreetmap/josm/tools/I18n.java

    r8282 r8283  
    8383    private static volatile PluralMode pluralMode = PluralMode.MODE_NOTONE; /* english default */
    8484    private static volatile String loadedCode = "en";
     85    /** store the original system locale for further use */
     86    public static final Locale SystemLocale = Locale.getDefault();
    8587
    8688    /* Localization keys for file chooser (and color chooser). */
     
    494496        URL tr = getTranslationFile(l);
    495497        if (tr == null || !languages.containsKey(l)) {
    496             int i = l.indexOf('_');
    497             if (i > 0) {
    498                 l = l.substring(0, i);
    499             }
    500             tr = getTranslationFile(l);
    501             if (tr == null || !languages.containsKey(l))
    502                 return false;
     498            return false;
    503499        }
    504500        try (
     
    738734
    739735    /**
    740      * Setup special font for Khmer script, as the default Java font do not display these characters.
     736     * Setup special font for Khmer script, as the default Java fonts do not display these characters.
    741737     *
    742      * @since 8281
     738     * @since 8282
    743739     */
    744740    public static void setupLanguageFonts() {
  • trunk/src/org/openstreetmap/josm/tools/LanguageInfo.java

    r8241 r8283  
    44import static org.openstreetmap.josm.tools.I18n.trc;
    55
     6import java.util.Collection;
     7import java.util.LinkedList;
    68import java.util.Locale;
    79
     
    8688     * to identify the locale of a localized resource, but in some cases it may use the
    8789     * programmatic name for locales, as replied by {@link Locale#toString()}.
     90     *
     91     * For unknown country codes and variants this functuion already does fallback to
     92     * internally known translations.
    8893     *
    8994     * @param locale the locale. Replies "en" if null.
     
    9297    public static String getJOSMLocaleCode(Locale locale) {
    9398        if (locale == null) return "en";
    94         String full = locale.toString();
    95         if ("iw_IL".equals(full))
    96             return "he";
    97         else if ("in".equals(full))
    98             return "id";
    99         else if ("ca__valencia".equals(full))
    100             return "ca@valencia";
    101         else if (I18n.hasCode(full)) // catch all non-single codes
    102             return full;
    103 
    104         // return single code
     99        for(String full : getLanguageCodes(locale)) {
     100            if ("iw_IL".equals(full))
     101                return "he";
     102            else if ("in".equals(full))
     103                return "id";
     104            else if (I18n.hasCode(full)) // catch all non-single codes
     105                return full;
     106        }
     107
     108        // return single code as fallback
    105109        return locale.getLanguage();
    106110    }
     
    154158     */
    155159    public static Locale getLocale(String localeName) {
    156         localeName = getJavaLocaleCode(localeName);
    157         if ("ca__valencia".equals(localeName)) {
    158             return new Locale("ca", "", "valencia");
    159         }
     160        int country = localeName.indexOf("_");
     161        int variant = localeName.indexOf("@");
     162        if (variant < 0 && country >= 0)
     163            variant = localeName.indexOf("_", country+1);
    160164        Locale l;
    161         int i = localeName.indexOf('_');
    162         if (i > 0) {
    163             l = new Locale(localeName.substring(0, i), localeName.substring(i + 1));
     165        if (variant > 0 && country > 0) {
     166            l = new Locale(localeName.substring(0, country), localeName.substring(country+1, variant), localeName.substring(variant + 1));
     167        } else if (variant > 0) {
     168            l = new Locale(localeName.substring(0, variant), "", localeName.substring(variant + 1));
     169        } else if (country > 0) {
     170            l = new Locale(localeName.substring(0, country), localeName.substring(country + 1));
    164171        } else {
    165172            l = new Locale(localeName);
     
    208215        return code+"_";
    209216    }
     217
     218    /**
     219     * Replies a list of language codes for local names. Prefixes range from very specific
     220     * to more generic.
     221     * <ul>
     222     *   <li>lang_COUNTRY@variant  of the current locale</li>
     223     *   <li>lang@variant  of the current locale</li>
     224     *   <li>lang_COUNTRY of the current locale</li>
     225     *   <li>lang of the current locale</li>
     226     * </ul>
     227     *
     228     * @param locale the locale to use, <code>null</code> for default locale
     229     * @since 8283
     230     * @return list of codes
     231     */
     232    public static Collection<String> getLanguageCodes(Locale l) {
     233        Collection<String> list = new LinkedList<String>();
     234        if(l == null)
     235            l = Locale.getDefault();
     236        String lang = l.getLanguage();
     237        String c = l.getCountry();
     238        String v = l.getVariant();
     239        if(c.isEmpty())
     240            c = null;
     241        if(v != null && !v.isEmpty()) {
     242            if(c != null)
     243                list.add(lang+"_"+c+"@"+v);
     244            list.add(lang+"@"+v);
     245        }
     246        if(c != null)
     247            list.add(lang+"_"+c);
     248        list.add(lang);
     249        return list;
     250    }
    210251}
Note: See TracChangeset for help on using the changeset viewer.