Ctx & Lua: determination of the language being used
Hello, how to determine the current language being used in Ctx source via Lua? I mean soumething like: --- \mainlanguage[cz] \starttext AAA \startluacode if context.mainlanguage == "cz" then -- Do something specific context("Ahoj!") elseif context.mainlanguage == "en" then -- Do something specific context("Hello!") else end \stopluacode \stoptext --- So is there something like 'context.mainlanguage' or 'context.mainlanguage()' or 'tex.mainlanguage' or ...? Best regards, Lukas -- Ing. Lukáš Procházka [mailto:LPr@pontex.cz] Pontex s. r. o. [mailto:pontex@pontex.cz] [http://www.pontex.cz] Bezová 1658 147 14 Praha 4 Tel: +420 244 062 238 Fax: +420 244 461 038
Am 25.11.2010 um 12:49 schrieb Procházka Lukáš Ing. - Pontex s. r. o.:
Hello,
how to determine the current language being used in Ctx source via Lua? I mean soumething like:
--- \mainlanguage[cz]
\starttext AAA
\startluacode if context.mainlanguage == "cz" then -- Do something specific context("Ahoj!") elseif context.mainlanguage == "en" then -- Do something specific context("Hello!") else end \stopluacode \stoptext ---
So is there something like 'context.mainlanguage' or 'context.mainlanguage()' or 'tex.mainlanguage' or ...?
\mainlanguage[...] -> \currentmainlanguage \language[...] -> \currentlanguage Wolfgang
On 11/25/2010 01:13 PM, Wolfgang Schuster wrote:
Am 25.11.2010 um 12:49 schrieb Procházka Lukáš Ing. - Pontex s. r. o.:
Hello,
how to determine the current language being used in Ctx source via Lua? I mean soumething like:
--- \mainlanguage[cz]
\starttext AAA
\startluacode if context.mainlanguage == "cz" then -- Do something specific context("Ahoj!") elseif context.mainlanguage == "en" then -- Do something specific context("Hello!") else end \stopluacode \stoptext ---
So is there something like 'context.mainlanguage' or 'context.mainlanguage()' or 'tex.mainlanguage' or ...?
\mainlanguage[...] -> \currentmainlanguage \language[...] -> \currentlanguage
For the benefit of the mailing list archive, this means you can write in lua: if "\currentmainlanguage"=="cz" then Except that that will not work because 'cz' is just an alias for 'cs', so you should test for that instead: if "\currentmainlanguage"=="cs" then context("Ahoj!") elseif "\currentmainlanguage"=="en" then context("Hello!") Best wishes, Taco
... Thanks to both. So -
On Thu, 25 Nov 2010 13:31:36 +0100, Taco Hoekwater
On 11/25/2010 01:13 PM, Wolfgang Schuster wrote:
Am 25.11.2010 um 12:49 schrieb Procházka Lukáš Ing. - Pontex s. r. o.:
Hello,
how to determine the current language being used in Ctx source via Lua? I mean soumething like:
--- \mainlanguage[cz]
\starttext AAA
\startluacode if context.mainlanguage == "cz" then -- Do something specific context("Ahoj!") elseif context.mainlanguage == "en" then -- Do something specific context("Hello!") else end \stopluacode \stoptext ---
So is there something like 'context.mainlanguage' or 'context.mainlanguage()' or 'tex.mainlanguage' or ...?
\mainlanguage[...] -> \currentmainlanguage \language[...] -> \currentlanguage
For the benefit of the mailing list archive, this means you can write in lua:
if "\currentmainlanguage"=="cz" then
Except that that will not work because 'cz' is just an alias for 'cs', so you should test for that instead:
if "\currentmainlanguage"=="cs" then context("Ahoj!") elseif "\currentmainlanguage"=="en" then context("Hello!")
Best wishes, Taco
a="\currentmainlanguage" =a currentmainlanguage =("\currentmainlanguage"=="currentmainlanguage")
- In standalone Lua, the "\c" would be read (translated) as "c", as "\c" is not a valid escape sequence. See the test: --- Microsoft Windows XP [Verze 5.1.2600] (C) Copyright 1985-2001 Microsoft Corp. C:\Lukas\ConTeXt\Samples\LuaTest>lua Lua 5.1.4 Copyright (C) 1994-2008 Lua.org, PUC-Rio true -- SO "\c" BECOMES "c" --- So does it mean that "\<char>" (in the beginning of a string) in Lua for ConTeXt is a "hack" which expands a (Con)TeX(t) internal value (in the case that <char> is not a valid escape char)? - If so, how to get an internal (Con)TeX(t) value which begins with a valid escape <char>, e.g. 't', so e.g. "\textwidth"? ("\t" should become <tab> in this case, or am I wrong?) (Where to read about? I've been searching in luatexref-t.pdf...) Lukas
On 11/25/2010 02:16 PM, Procházka Lukáš Ing. - Pontex s. r. o. wrote:
So does it mean that "\<char>" (in the beginning of a string) in Lua for ConTeXt is a "hack" which expands a (Con)TeX(t) internal value (in the case that <char> is not a valid escape char)?
No, nothing as complicated as that: \currentmainlanguage is an expandable macro, so it simply expands to its actual content: cs This also means that my proposed solution will not work if you put it in a separate lua file: you have to convert it to a lua function in that case that takes \currentmainlanguage as an argument. Best wishes, Taco
On Thu, 25 Nov 2010 14:22:18 +0100, Taco Hoekwater
On 11/25/2010 02:16 PM, Procházka Lukáš Ing. - Pontex s. r. o. wrote:
No, nothing as complicated as that: \currentmainlanguage is an expandable macro, so it simply expands to its actual content: cs
This also means that my proposed solution will not work if you put it in a separate lua file: you have to convert it to a lua function in that case that takes \currentmainlanguage as an argument.
... And using this in a separate file is also what I'd need. So do I have to do something like: --- A.mkiv \startluacode function currentmainlanguage() return "\currentmainlanguage" end -- Or: the_currentmainlanguage = "\currentmainlanguage" dofile("A.lua") \stopluacode --- And: --- A.lua if currentmainlanguage() == "cs" then -- Or: if the_currentmainlanguage = "cs" then -- Something end --- OK? (Suggestion: Wouldn't be nice to have function(s) or variable(s) like e.g. 'context.environment["currentmainlanguage"]' or something like this?) Best regards, Lukas (To Taco: Sorry, I sent the same message to your private address first.)
On 11/25/2010 02:43 PM, Procházka Lukáš Ing. - Pontex s. r. o. wrote:
... And using this in a separate file is also what I'd need. So do I have to do something like:
Something like that, yes. But it depends a little on what you want to do: because \currentmainlanguage expands, you will get the value that is active during the first scan of the lua code block, which may not be what you want, but it is hard to judge without the actual use case.
(Suggestion: Wouldn't be nice to have function(s) or variable(s) like e.g. 'context.environment["currentmainlanguage"]' or something like this?)
Yes, agreed, something like that would be useful. The same applies to some other things, like color and active modes. Some of these are probably there already, but it would be nice if they were documented. Best wishes, Taco
On 25-11-2010 4:13, Taco Hoekwater wrote:
On 11/25/2010 02:43 PM, Procházka Lukáš Ing. - Pontex s. r. o. wrote:
... And using this in a separate file is also what I'd need. So do I have to do something like:
Something like that, yes. But it depends a little on what you want to do: because \currentmainlanguage expands, you will get the value that is active during the first scan of the lua code block, which may not be what you want, but it is hard to judge without the actual use case.
(Suggestion: Wouldn't be nice to have function(s) or variable(s) like e.g. 'context.environment["currentmainlanguage"]' or something like this?)
Yes, agreed, something like that would be useful. The same applies to some other things, like color and active modes. Some of these are probably there already, but it would be nice if they were documented.
it's not so hard to interface that but we need to pick up the current value \ctxlua{tex.sprint(languages.numbers[1])} however, tex does not privide access to the current language so i'd have to introduce an extra counter at the tex end anyhow, for the moment you can use languages.numbers[tex.count.mainlanguagenumber] which probably gives you wat you want: en ----------------------------------------------------------------- Hans Hagen | PRAGMA ADE Ridderstraat 27 | 8061 GH Hasselt | The Netherlands tel: 038 477 53 69 | voip: 087 875 68 74 | www.pragma-ade.com | www.pragma-pod.nl -----------------------------------------------------------------
participants (4)
-
Hans Hagen
-
Procházka Lukáš Ing. - Pontex s. r. o.
-
Taco Hoekwater
-
Wolfgang Schuster