ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
* Ctx & Lua: determination of the language being used
@ 2010-11-25 11:49 Procházka Lukáš Ing. - Pontex s. r. o.
  2010-11-25 12:13 ` Wolfgang Schuster
  0 siblings, 1 reply; 9+ messages in thread
From: Procházka Lukáš Ing. - Pontex s. r. o. @ 2010-11-25 11:49 UTC (permalink / raw)
  To: ConTeXt

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

___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://tex.aanhet.net
archive  : http://foundry.supelec.fr/projects/contextrev/
wiki     : http://contextgarden.net
___________________________________________________________________________________


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Ctx & Lua: determination of the language being used
  2010-11-25 11:49 Ctx & Lua: determination of the language being used Procházka Lukáš Ing. - Pontex s. r. o.
@ 2010-11-25 12:13 ` Wolfgang Schuster
  2010-11-25 12:31   ` Taco Hoekwater
  0 siblings, 1 reply; 9+ messages in thread
From: Wolfgang Schuster @ 2010-11-25 12:13 UTC (permalink / raw)
  To: mailing list for ConTeXt users


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

___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://tex.aanhet.net
archive  : http://foundry.supelec.fr/projects/contextrev/
wiki     : http://contextgarden.net
___________________________________________________________________________________


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Ctx & Lua: determination of the language being used
  2010-11-25 12:13 ` Wolfgang Schuster
@ 2010-11-25 12:31   ` Taco Hoekwater
  2010-11-25 13:16     ` Procházka Lukáš Ing. - Pontex s. r. o.
  0 siblings, 1 reply; 9+ messages in thread
From: Taco Hoekwater @ 2010-11-25 12:31 UTC (permalink / raw)
  To: mailing list for ConTeXt users

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



___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://tex.aanhet.net
archive  : http://foundry.supelec.fr/projects/contextrev/
wiki     : http://contextgarden.net
___________________________________________________________________________________


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Ctx & Lua: determination of the language being used
  2010-11-25 12:31   ` Taco Hoekwater
@ 2010-11-25 13:16     ` Procházka Lukáš Ing. - Pontex s. r. o.
  2010-11-25 13:22       ` Taco Hoekwater
  0 siblings, 1 reply; 9+ messages in thread
From: Procházka Lukáš Ing. - Pontex s. r. o. @ 2010-11-25 13:16 UTC (permalink / raw)
  To: mailing list for ConTeXt users

... Thanks to both. So -

On Thu, 25 Nov 2010 13:31:36 +0100, Taco Hoekwater <taco@elvenkind.com> wrote:

> 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

- 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
> a="\currentmainlanguage"
> =a
currentmainlanguage
> =("\currentmainlanguage"=="currentmainlanguage")
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

___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://tex.aanhet.net
archive  : http://foundry.supelec.fr/projects/contextrev/
wiki     : http://contextgarden.net
___________________________________________________________________________________


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Ctx & Lua: determination of the language being used
  2010-11-25 13:16     ` Procházka Lukáš Ing. - Pontex s. r. o.
@ 2010-11-25 13:22       ` Taco Hoekwater
  2010-11-25 13:43         ` Procházka Lukáš Ing. - Pontex s. r. o.
  0 siblings, 1 reply; 9+ messages in thread
From: Taco Hoekwater @ 2010-11-25 13:22 UTC (permalink / raw)
  To: mailing list for ConTeXt users

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
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://tex.aanhet.net
archive  : http://foundry.supelec.fr/projects/contextrev/
wiki     : http://contextgarden.net
___________________________________________________________________________________


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Ctx & Lua: determination of the language being used
  2010-11-25 13:22       ` Taco Hoekwater
@ 2010-11-25 13:43         ` Procházka Lukáš Ing. - Pontex s. r. o.
  2010-11-25 15:13           ` Taco Hoekwater
  0 siblings, 1 reply; 9+ messages in thread
From: Procházka Lukáš Ing. - Pontex s. r. o. @ 2010-11-25 13:43 UTC (permalink / raw)
  To: ConTeXt

On Thu, 25 Nov 2010 14:22:18 +0100, Taco Hoekwater <taco@elvenkind.com> wrote:

> 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.)

___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://tex.aanhet.net
archive  : http://foundry.supelec.fr/projects/contextrev/
wiki     : http://contextgarden.net
___________________________________________________________________________________


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Ctx & Lua: determination of the language being used
  2010-11-25 13:43         ` Procházka Lukáš Ing. - Pontex s. r. o.
@ 2010-11-25 15:13           ` Taco Hoekwater
  2010-11-25 15:57             ` Hans Hagen
  0 siblings, 1 reply; 9+ messages in thread
From: Taco Hoekwater @ 2010-11-25 15:13 UTC (permalink / raw)
  To: mailing list for ConTeXt users

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
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://tex.aanhet.net
archive  : http://foundry.supelec.fr/projects/contextrev/
wiki     : http://contextgarden.net
___________________________________________________________________________________


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Ctx & Lua: determination of the language being used
  2010-11-25 15:13           ` Taco Hoekwater
@ 2010-11-25 15:57             ` Hans Hagen
  2010-11-26  9:22               ` Taco Hoekwater
  0 siblings, 1 reply; 9+ messages in thread
From: Hans Hagen @ 2010-11-25 15:57 UTC (permalink / raw)
  To: mailing list for ConTeXt users; +Cc: Taco Hoekwater

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
-----------------------------------------------------------------
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://tex.aanhet.net
archive  : http://foundry.supelec.fr/projects/contextrev/
wiki     : http://contextgarden.net
___________________________________________________________________________________


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Ctx & Lua: determination of the language being used
  2010-11-25 15:57             ` Hans Hagen
@ 2010-11-26  9:22               ` Taco Hoekwater
  0 siblings, 0 replies; 9+ messages in thread
From: Taco Hoekwater @ 2010-11-26  9:22 UTC (permalink / raw)
  To: Hans Hagen; +Cc: mailing list for ConTeXt users

On 11/25/2010 04:57 PM, Hans Hagen wrote:
>
> however, tex does not privide access to the current language

It does: \ctxlua{print(tex.language)}


___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://tex.aanhet.net
archive  : http://foundry.supelec.fr/projects/contextrev/
wiki     : http://contextgarden.net
___________________________________________________________________________________


^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2010-11-26  9:22 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-11-25 11:49 Ctx & Lua: determination of the language being used Procházka Lukáš Ing. - Pontex s. r. o.
2010-11-25 12:13 ` Wolfgang Schuster
2010-11-25 12:31   ` Taco Hoekwater
2010-11-25 13:16     ` Procházka Lukáš Ing. - Pontex s. r. o.
2010-11-25 13:22       ` Taco Hoekwater
2010-11-25 13:43         ` Procházka Lukáš Ing. - Pontex s. r. o.
2010-11-25 15:13           ` Taco Hoekwater
2010-11-25 15:57             ` Hans Hagen
2010-11-26  9:22               ` Taco Hoekwater

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).