How to get Wikidata labels in more than one language?
Asked Answered
H

2

12

I'm traying to get the regions of Italy in both Italian and English. I can get then in one laguage with this query...

PREFIX wikibase: <http://wikiba.se/ontology#>
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>

SELECT DISTINCT ?RegionIT ?RegionITLabel ?ISO_code ?Geo
{
?RegionIT wdt:P31 wd:Q16110;
wdt:P300 ?ISO_code; 
wdt:P625 ?Geo 
           SERVICE wikibase:label { bd:serviceParam wikibase:language "it"  }
}

ORDER BY ?regionITLabel

... but adding another language using the standard SPARQL syntax doesn't work.

Hibachi answered 6/4, 2017 at 14:38 Comment(1)
Multiple labels using the label service: #49119202Jacobina
T
13

... but adding another language using the standard SPARQL syntax doesn't work.

How are you doing that? This works:

PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX wikibase: <http://wikiba.se/ontology#>
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>

SELECT DISTINCT ?RegionIT ?label (lang(?label) as ?label_lang)  ?ISO_code ?Geo
{
    ?RegionIT wdt:P31 wd:Q16110;
              wdt:P300 ?ISO_code; 
              wdt:P625 ?Geo ;
              rdfs:label ?label
}
order by ?RegionIT

Link to try query

To limit to just Italian and English filter on the lang:

PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX wikibase: <http://wikiba.se/ontology#>
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>

SELECT DISTINCT ?RegionIT ?label ?ISO_code ?Geo
{
    ?RegionIT wdt:P31 wd:Q16110;
              wdt:P300 ?ISO_code; 
              wdt:P625 ?Geo ;
              rdfs:label ?label
    filter(lang(?label) = 'it' || lang(?label) = 'en')
}
order by ?RegionIT

Link to try query

Obviously that multiplies the number of results, one for each language. If that's an issue you can do:

...
rdfs:label ?label_it , ?label_en
filter(lang(?label_it) = 'it' && lang(?label_en) = 'en')
...

which is effectively what the language service does.

Timepiece answered 6/4, 2017 at 16:27 Comment(2)
Thanks. This indeed answers the question. My intention was to learn how to achieve it with SERVICE wikibase:label but that wasn't clear from the question. Yet, I'd appreciate any ideas about that.Hibachi
you can request several languages with SERVICE wikibase:label like so: SERVICE wikibase:label { bd:serviceParam wikibase:language "it,en" }, but it's only a fallback mechanism, you will only get the first defined label from the requested languages, not one label per language.Sundowner
N
10

Let's list all countries in English and Russian.

#List of countries in English and Russian
SELECT ?country ?label_en ?label_ru
WHERE
{
    ?country wdt:P31 wd:Q6256.
    ?country rdfs:label ?label_en filter (lang(?label_en) = "en").
    ?country rdfs:label ?label_ru filter (lang(?label_ru) = "ru").
}

SPARQL query

This example was taken from the tutorial Research in programming Wikidata, section "Countries".

Neile answered 6/4, 2017 at 14:38 Comment(1)
This is what I exactly wanted. Thanks for the quick demo.Tubular

© 2022 - 2024 — McMap. All rights reserved.