Changeset 32001 in osm for applications/editors/josm


Ignore:
Timestamp:
2016-01-18T12:43:17+01:00 (9 years ago)
Author:
simon04
Message:

JOSM/wikipedia: Wikidata title should be displayed in user's language - #josm12397

Location:
applications/editors/josm/plugins/wikipedia
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/wikipedia/src/org/wikipedia/WikidataTagCellRenderer.java

    r31915 r32001  
    77import java.util.Collections;
    88import java.util.List;
     9import java.util.Locale;
    910import java.util.Map;
    1011import java.util.concurrent.Callable;
     
    3738        @Override
    3839        public String call() throws Exception {
    39             final String label = WikipediaApp.getLabelForWikidata(id, LanguageInfo.getJOSMLocaleCode());
     40            final String label = WikipediaApp.getLabelForWikidata(id, Locale.getDefault());
    4041            table.repaint();
    4142            table = null;
  • applications/editors/josm/plugins/wikipedia/src/org/wikipedia/WikipediaApp.java

    r31907 r32001  
    1212import java.util.HashMap;
    1313import java.util.List;
     14import java.util.Locale;
    1415import java.util.Map;
    1516import java.util.Objects;
     
    4748
    4849    private WikipediaApp() {
     50    }
     51
     52    static String getMediawikiLocale(Locale locale) {
     53        if (!locale.getCountry().isEmpty()) {
     54            return locale.getLanguage() + "-" + locale.getCountry().toLowerCase();
     55        } else {
     56            return locale.getLanguage();
     57        }
    4958    }
    5059
     
    206215    }
    207216
    208     static String getLabelForWikidata(String wikidataId, String preferredLanguage) {
     217    static String getLabelForWikidata(String wikidataId, Locale locale, String ... preferredLanguage) {
    209218        try {
    210219            CheckParameterUtil.ensureThat(WIKIDATA_PATTERN.matcher(wikidataId).matches(), "Invalid Wikidata ID given");
     
    213222                    "&props=labels" +
    214223                    "&ids=" + wikidataId +
    215                     "&format=xml" +
    216                     (preferredLanguage != null ? "&languages=" + preferredLanguage + "&languagefallback=en" : "");
     224                    "&format=xml";
     225            final Collection<String> languages = new ArrayList<>();
     226            if (locale != null) {
     227                languages.add(getMediawikiLocale(locale));
     228                languages.add(getMediawikiLocale(new Locale(locale.getLanguage())));
     229            }
     230            languages.addAll(Arrays.asList(preferredLanguage));
     231            languages.add("en");
    217232            try (final InputStream in = HttpClient.create(new URL(url)).setReasonForRequest("Wikipedia").connect().getContent()) {
    218233                final Document xml = DOCUMENT_BUILDER.parse(in);
    219                 final Node label = (Node) X_PATH.compile("//label").evaluate(xml, XPathConstants.NODE);
    220                 if (label == null && preferredLanguage != null) {
    221                     return getLabelForWikidata(wikidataId, null);
    222                 } else if (label == null) {
    223                     return null;
    224                 } else {
    225                     return (String) X_PATH.compile("./@value").evaluate(label, XPathConstants.STRING);
    226                 }
     234                for (String language : languages) {
     235                    final String label = (String) X_PATH.compile("//label[@language='" + language + "']/@value")
     236                            .evaluate(xml, XPathConstants.STRING);
     237                    if (label != null && !label.isEmpty()) {
     238                         return label;
     239                    }
     240                }
     241                return null;
    227242            }
    228243        } catch (Exception ex) {
  • applications/editors/josm/plugins/wikipedia/test/unit/org/wikipedia/WikipediaAppTest.java

    r31907 r32001  
    1313import java.util.Collection;
    1414import java.util.List;
     15import java.util.Locale;
    1516import java.util.Map;
    1617
     
    2526    public void setUp() throws Exception {
    2627        Main.initApplicationPreferences();
     28    }
     29
     30    @Test
     31    public void testMediawikiLocale() throws Exception {
     32        assertThat(WikipediaApp.getMediawikiLocale(Locale.GERMANY), is("de-de"));
     33        assertThat(WikipediaApp.getMediawikiLocale(Locale.GERMAN), is("de"));
     34        assertThat(WikipediaApp.getMediawikiLocale(Locale.UK), is("en-gb"));
     35        assertThat(WikipediaApp.getMediawikiLocale(Locale.CANADA), is("en-ca"));
    2736    }
    2837
     
    146155    @Test
    147156    public void testGetLabelForWikidata() throws Exception {
    148         assertThat(WikipediaApp.getLabelForWikidata("Q1741", "de"), is("Wien"));
    149         assertThat(WikipediaApp.getLabelForWikidata("Q1741", "en"), is("Vienna"));
    150         assertThat(WikipediaApp.getLabelForWikidata("Q" + Long.MAX_VALUE, "en"), nullValue());
     157        assertThat(WikipediaApp.getLabelForWikidata("Q1741", Locale.GERMAN), is("Wien"));
     158        assertThat(WikipediaApp.getLabelForWikidata("Q1741", Locale.ENGLISH), is("Vienna"));
     159        assertThat(WikipediaApp.getLabelForWikidata("Q" + Long.MAX_VALUE, Locale.ENGLISH), nullValue());
    151160    }
    152161
    153162    @Test(expected = RuntimeException.class)
    154163    public void testGetLabelForWikidataInvalidId() throws Exception {
    155         WikipediaApp.getLabelForWikidata("Qxyz", "en");
     164        WikipediaApp.getLabelForWikidata("Qxyz", Locale.ENGLISH);
    156165    }
    157166
Note: See TracChangeset for help on using the changeset viewer.