Mobileread
Change Wikipedia Language
#1  nachox77 02-18-2016, 01:25 AM
Hello.

I was wondering if is possible to change the lenguage of the wikipedia search. Instead of searching in wikipedia in English I want to search in Spanish.

Also I'm looking for Dictionaries in an appropriate format to KOReader.

Thanks in advance
Reply 

#2  Ken Maltby 02-18-2016, 10:14 AM
Quote nachox77
Hello.

I was wondering if is possible to change the lenguage of the wikipedia search. Instead of searching in wikipedia in English I want to search in Spanish.

Also I'm looking for Dictionaries in an appropriate format to KOReader.

Thanks in advance
I would suspect that the URL for Wikipedia would include a language reference. For the desktop sites it looks like this https://es.wikipedia.org/wiki/Wikipedia:Portada

If you look at "/koreader/frontend/ui/Wikipedia.lua" there is a provision for changing the variable that defines the language. I don't know how that works, but you could change the "default_lang = "en" to "es".

KOReader uses Stardict files. There are a number of them on the internet, including translation dictionaries.

Luck;
Ken
Reply 

#3  nachox77 02-18-2016, 06:16 PM
Here is the wikipedia.lua file with default values:

Quote
local JSON = require("json")
local DEBUG = require("dbg")

--[[
-- Query wikipedia using Wikimedia Web API.
-- http://en.wikipedia.org/w/api.php?action=query&prop=extracts&format=json&exi ntro=&explaintext=&redirects=&titles=hello
--]]

local Wikipedia = {
wiki_server = "https://%s.wikipedia.org",
wiki_path = "/w/api.php",
wiki_params = {
action = "query",
prop = "extracts",
format = "json",
exintro = "",
explaintext = "",
redirects = "",
},
default_lang = "en",
}

function Wikipedia:getWikiServer(lang)
return string.format(self.wiki_server, lang or self.default_lang)
end

--[[
-- return decoded JSON table from Wikipedia
--]]
function Wikipedia:loadPage(text, lang, intro, plain)
local socket = require('socket')
local url = require('socket.url')
local http = require('socket.http')
local https = require('ssl.https')
local ltn12 = require('ltn12')

local request, sink = {}, {}
local query = ""
self.wiki_params.exintro = intro and "" or nil
self.wiki_params.explaintext = plain and "" or nil
for k,v in pairs(self.wiki_params) do
query = query .. k .. '=' .. v .. '&'
end
local parsed = url.parse(self:getWikiServer(lang))
parsed.path = self.wiki_path
parsed.query = query .. "titles=" .. url.escape(text)

-- HTTP request
request['url'] = url.build(parsed)
request['method'] = 'GET'
request['sink'] = ltn12.sink.table(sink)
DEBUG("request", request)
http.TIMEOUT, https.TIMEOUT = 10, 10
local httpRequest = parsed.scheme == 'http' and http.request or https.request
local code, headers, status = socket.skip(1, httpRequest(request))

-- raise error message when network is unavailable
if headers == nil then
error("Network is unreachable")
end

if status ~= "HTTP/1.1 200 OK" then
DEBUG("HTTP status not okay:", status)
return
end

local content = table.concat(sink)
if content ~= "" and string.sub(content, 1,1) == "{" then
local ok, result = pcall(JSON.decode, content)
if ok and result then
DEBUG("wiki result", result)
return result
else
DEBUG("error:", result)
end
else
DEBUG("not JSON:", content)
end
end

-- extract intro passage in wiki page
function Wikipedia:wikintro(text, lang)
local result = self:loadPage(text, lang, true, true)
if result then
local query = result.query
if query then
return query.pages
end
end
end

return Wikipedia
I changed both variables:
default_lang="es" //instead of "en"
wiki_server="https://es.wikipedia.org" //instead of "https://%s.wikipedia.org"

(I don't know what means "%", it doesn´t look like a joker char)

And it doesn´t work, it keep giving me english text as answers. May be i'm losing some variable
Reply 

#4  Alan_S 02-19-2016, 08:04 AM
Try to change only default_lang="es", leave wiki_server alone.

%s means that some variable is in question. Probably the default language one. On the other hand, why hardcoding language (es) instead using variable doesn't work, I can't tell.
Reply 

#5  nachox77 02-21-2016, 01:31 AM
Thanks for answering. It doesn't work.
Reply 

#6  Alan_S 02-21-2016, 08:42 AM
It would be good idea if you report it in koreaders github page, i.e.: https://github.com/koreader/koreader/issues In this way authors would know about this issue and can address it.
Reply 

#7  Frenzie 02-22-2016, 07:59 AM
The %s is used a little further down with string.format:

string.format(self.wiki_server, lang or self.default_lang)

The lang variable is set in frontend/ui/translator.lua, although given that we're dealing with single words I don't know about the viability of that approach.

Anyway, did you try the Wikipedia API without bothering with the KOReader code?
Reply 

#8  nachox77 02-24-2016, 03:20 PM
What i want is searching directly in spanish wikipedia no a traduction
Reply 

#9  Frenzie 02-26-2016, 03:31 AM
As far as I've been able to ascertain, what you did (i.e. change the variable %s to es) should already work.
Reply 

#10  djalma 03-04-2016, 11:07 AM
Quote nachox77
wiki_server="https://es.wikipedia.org" //instead of "https://%s.wikipedia.org"

(I don't know what means "%", it doesn´t look like a joker char)

And it doesn´t work, it keep giving me english text as answers. May be i'm losing some variable

Quote Frenzie
As far as I've been able to ascertain, what you did (i.e. change the variable %s to es) should already work.
Yes, It works. Anyway link "selected koreader language", (or the language in epub's metadata for example, in the same way that koreader nightly version is doing with the "automatic" hyphenation), to the wikipedia's language should be a good idea.
Regards.
Reply 

Today's Posts | Search this Thread | Login | Register