Changeset 15501 in josm for trunk


Ignore:
Timestamp:
2019-11-03T16:31:40+01:00 (4 years ago)
Author:
Don-vip
Message:

fix #18284 - Add name:ko-Latn|ja_rm|sr-Latn|zh_pinyin|ja_kana|ja-Latn|ja-Hira to the list of known language codes

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

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/dialogs/properties/PropertiesCellRenderer.java

    r14185 r15501  
    1010import java.awt.Font;
    1111import java.util.Collection;
    12 import java.util.Locale;
    1312import java.util.Map;
    1413import java.util.Objects;
    1514import java.util.Optional;
    1615import java.util.concurrent.CopyOnWriteArrayList;
    17 import java.util.regex.Matcher;
    18 import java.util.regex.Pattern;
    1916
    2017import javax.swing.JLabel;
     
    2825import org.openstreetmap.josm.data.preferences.CachingProperty;
    2926import org.openstreetmap.josm.data.preferences.NamedColorProperty;
     27import org.openstreetmap.josm.tools.I18n;
     28import org.openstreetmap.josm.tools.Pair;
    3029
    3130/**
     
    4342    private static final CachingProperty<Boolean> DISCARDABLE
    4443            = new BooleanProperty("display.discardable-keys", false).cached();
    45 
    46     // Matches ISO-639 two and three letters language codes
    47     private static final Pattern LANGUAGE_NAMES = Pattern.compile("name:(\\p{Lower}{2,3})");
    4844
    4945    static {
     
    118114            boolean knownNameKey = false;
    119115            if (column == 0 && str != null) {
    120                 Matcher m = LANGUAGE_NAMES.matcher(str);
    121                 if (m.matches()) {
    122                     String code = m.group(1);
    123                     String label = new Locale(code).getDisplayLanguage();
    124                     knownNameKey = !code.equals(label);
     116                Pair<String, Boolean> label = I18n.getLocalizedLanguageName(str);
     117                if (label != null) {
     118                    knownNameKey = label.b;
    125119                    if (knownNameKey) {
    126120                        str = new StringBuilder("<html><body>").append(str)
    127                                 .append(" <i>&lt;").append(label).append("&gt;</i></body></html>").toString();
     121                                .append(" <i>&lt;").append(label.a).append("&gt;</i></body></html>").toString();
    128122                    }
    129123                }
  • trunk/src/org/openstreetmap/josm/tools/I18n.java

    r15494 r15501  
    1919import java.util.Locale;
    2020import java.util.Map;
     21import java.util.regex.Matcher;
     22import java.util.regex.Pattern;
    2123import java.util.zip.ZipEntry;
    2224import java.util.zip.ZipFile;
     
    140142        languages.put("zh_TW", PluralMode.MODE_NONE);
    141143    }
     144
     145    private static final String HIRAGANA = "hira";
     146    private static final String KATAKANA = "kana";
     147    private static final String LATIN = "latn";
     148    private static final String PINYIN = "pinyin";
     149    private static final String ROMAJI = "rm";
     150
     151    // Matches ISO-639 two and three letters language codes + scripts
     152    private static final Pattern LANGUAGE_NAMES = Pattern.compile(
     153            "name:(\\p{Lower}{2,3})(?:[-_](?i:(" + String.join("|", HIRAGANA, KATAKANA, LATIN, PINYIN, ROMAJI) + ")))?");
    142154
    143155    private static String format(String text, Object... objects) {
     
    668680        return originalLocale;
    669681    }
     682
     683    /**
     684     * Returns the localized name of the given script. Only scripts used in the OSM database are known.
     685     * @param script Writing system
     686     * @return the localized name of the given script, or null
     687     * @since 15501
     688     */
     689    public static String getLocalizedScript(String script) {
     690        if (script != null) {
     691            switch (script.toLowerCase(Locale.ENGLISH)) {
     692                case HIRAGANA:
     693                    return tr("Hiragana");
     694                case KATAKANA:
     695                    return tr("Katakana");
     696                case LATIN:
     697                    return tr("Latin");
     698                case PINYIN:
     699                    return tr("Pinyin");
     700                case ROMAJI:
     701                    return tr("Rōmaji");
     702                default:
     703                    Logging.warn("Unsupported script: " + script);
     704            }
     705        }
     706        return null;
     707    }
     708
     709    /**
     710     * Returns the localized name of the given language and optional script.
     711     * @param language Language
     712     * @return the pair of localized name + known state of the given language, or null
     713     * @since 15501
     714     */
     715    public static Pair<String, Boolean> getLocalizedLanguageName(String language) {
     716        Matcher m = LANGUAGE_NAMES.matcher(language);
     717        if (m.matches()) {
     718            String code = m.group(1);
     719            String label = new Locale(code).getDisplayLanguage();
     720            boolean knownNameKey = !code.equals(label);
     721            String script = getLocalizedScript(m.group(2));
     722            if (script != null) {
     723                label += " (" + script + ")";
     724            }
     725            return new Pair<>(label, knownNameKey);
     726        }
     727        return null;
     728    }
    670729}
Note: See TracChangeset for help on using the changeset viewer.