##
##  wml::std::lang - Multi-Lingual Support
##  Copyright (c) 1997-2001 Ralf S. Engelschall, All Rights Reserved. 
##  Copyright (c) 1999-2001 Denis Barbier
##

#  The <preserve>/<restore> tags with multiple arguments require WML 2.0.3
#use wml::mod::version
<require 2.0.3 />

#use wml::std::tags

#   
#  The list of defined languages
#
<set-var __languages= />

#   
#  The stack of language slices
#
<set-var __lang:stack= />

#
#   Display current language and list of defined languages
#
<define-tag lang:current><array-topvalue __lang:stack /></define-tag>
<define-tag lang:list><get-var __languages /></define-tag>

#
#   The `lang:new xx' tag which adds another language to
#   the list of known ones and defines the `lang:xx' tag.
#
<define-tag lang:new whitespace=delete>
<preserve id short />
<set-var short=* />
<set-var %attributes />
<set-var __ok=1 />
<set-var __i=0 />
<while <get-var __languages[<get-var __i />] />>
  <ifeq "<get-var __languages[<get-var __i />] />" "<get-var id />"
      <set-var __ok=0 /> />
  <increment __i />
</while>
<when <eq "<get-var __ok />" 1 />>
  <array-push __languages <get-var id /> />
  <set-var __i="LANG_<upcase <get-var id /> />" />
  <set-var __j="=LT=when =LT=string-eq =LT=or =LT=lang:current /> <get-var id /> /> <get-var id /> caseless=true />>" />
  <subst-in-string 
        "=LT=define-tag lang:<get-var id />: attributes=verbatim><get-var __j />[<get-var __i />:=LT=array-push __lang:stack <get-var id /> />%%attributes=LT=set-var __dummy==LT=array-pop __lang:stack /> />:<get-var __i />]=LT=/when>=LT=/define-tag>
         =LT=define-tag lang:<get-var id /> endtag=required><get-var __j />[<get-var __i />:=LT=array-push __lang:stack <get-var id /> />%%body=LT=set-var __dummy==LT=array-pop __lang:stack /> />:<get-var __i />]=LT=/when>=LT=/define-tag>" 
     "=LT=" "<"
  />
  <when <not <get-var short /> />>
  <subst-in-string 
        "=LT=define-tag <get-var id />: attributes=verbatim><get-var __j />[<get-var __i />:=LT=array-push __lang:stack <get-var id /> />%%attributes=LT=set-var __dummy==LT=array-pop __lang:stack /> />:<get-var __i />]=LT=/when>=LT=/define-tag>
         =LT=define-tag <get-var id /> endtag=required><get-var __j />[<get-var __i />:=LT=array-push __lang:stack <get-var id /> />%%body=LT=set-var __dummy==LT=array-pop __lang:stack /> />:<get-var __i />]=LT=/when>=LT=/define-tag>" 
     "=LT=" "<"
  />
  </when>
</when>
<restore  id short />
</define-tag>

#
#   The `lang:area' container tag
#
<define-tag lang:area endtag=required whitespace=delete>
<set-var __body=%Ubody />
<set-var __i=0 />
<set-var __j=<get-var __languages[<get-var __i />] /> />
<while <get-var __j />>
  <subst-in-var __body
    "\\(<get-var __j />\\)(.+?)(?=\\((?:[a-z]+|\\/)\\)|$)"
    "<lang:<get-var __j />>\\1</lang:<get-var __j />>" />
  <increment __i />
  <set-var __j=<get-var __languages[<get-var __i />] /> />
</while>
<get-var __body />
</define-tag>

#
#   The ``lang:set-wildcard'' tag lets you define which wildcard to use
#
<define-tag lang:set-wildcard>
<set-var __lang:wildcard=<or "%0" <@[\*]@> /> />
</define-tag>

#   
#   Sets the default wildcard 
#
<lang:set-wildcard />

#
#   The ``lang:star:'' tag
#      The protection is for navbar
#
<define-tag lang:star: whitespace=delete>
<protect pass=3>
<set-var __i=0 />
<while <get-var __languages[<get-var __i />] />>
  <when <string-eq
           <or <lang:current /> <get-var __languages[<get-var __i />] /> />
           <get-var __languages[<get-var __i />] /> caseless=true />>
    <subst-in-string 
      "[LANG_<upcase <get-var __languages[<get-var __i />] /> />:%attributes:]" 
      "<get-var __lang:wildcard />" "<get-var __languages[<get-var __i />] />" />
  </when>
  <increment __i />
</while>
</protect>
</define-tag>

#
#   The specialized <lang:star:href:> tag which
#   is a <lang:star:> tag but enhanced for use
#   with URLs...
#
<define-tag lang:star:href: whitespace=delete>
#   Colons may confuse pass 3, e.g. in rollovers
<protect pass=3>
<if <match "%attributes" ".+|.*" action=report /> <group
    <set-var __str=<match "%attributes" "^[^|]+" action=extract /> />
    <set-var __alt=<match "%attributes" "[^|]*$" action=extract /> />
/>    <group
    <set-var __str="%attributes" />
    <set-var __alt=<match "%attributes" "<get-var __lang:wildcard />" action=delete /> />
/>/>
<set-var __i=0 />
<while <get-var __languages[<get-var __i />] />>
  <when <string-eq
           <or <lang:current /> <get-var __languages[<get-var __i />] /> />
           <get-var __languages[<get-var __i />] /> caseless=true />>
    <set-var __url=<subst-in-string "<get-var __str />" 
                    "<get-var __lang:wildcard />" "<get-var __languages[<get-var __i />] />" /> />
    <if <file-exists <get-var __url /> />
       <group
         <suck/><subst-in-string
                "[LANG_<upcase <get-var __languages[<get-var __i />] /> />:<get-var __str />:]"
                "<get-var __lang:wildcard />" "<get-var __languages[<get-var __i />] />" /><suck/> />
       <group
         <suck/>[LANG_<upcase <get-var __languages[<get-var __i />] /> />:<get-var __alt />:]<suck/>
    />/>
  </when>
  <increment __i />
</while>
</protect>
</define-tag>

#
#   The ``lang:star:slice:'' tag
#   Another variant of <lang:star:> to dynamically specify
#   the output filenames instead of using the wml shebang line
#
#   This definition is really awful because it can deal with all
#   sort of situations
#   Former syntax:
#   * wml without command-line option (= wml -o %BASE.*.html)
#   * #!wml -o (ALL-LANG_*)+LANG_EN:foo.en.html
#   New syntax:
#   * #!wml -o (ALL-LANG_*)+LANG_**:foo.*.html
#   * #!wml -o (ALL-LANG_*)+LANG_**+FOO:foo.*.html \
#           -o (ALL-LANG_*)+LANG_**+BAR:bar.*.html
#
<define-tag lang:star:slice: whitespace=delete>
<set-var __slices:filename=<or %0
      "<get-var SLICE_OUTPUT_FILENAME />"
      "<get-var WML_SRC_BASENAME />.*.html" /> />
<set-var __str="" />
<foreach __sl:file __slices:filename>
    <when <match <get-var __sl:file /> "<@[\*[^:]*$]@>" />>
        <foreach __i __languages>
            <if <match <get-var __sl:file /> ":" />
                <group
<set-var __slice=<match <get-var __sl:file /> ":.*$" action=delete /> />
<subst-in-var __slice "<@[\*\*]@>" <upcase <get-var __i /> /> />
<set-var __filename=<match <get-var __sl:file /> ".*:" action=delete /> /> />
                <group
<set-var __slice=<group "(ALL-LANG_*)+LANG_" <upcase <get-var __i /> /> /> />
<set-var __filename=<get-var __sl:file /> /> />
            />
            <set-var __str=<group <get-var __str />
                    " -o " <get-var __slice />
                    ":"
                    <subst-in-string <get-var __filename /> "<@[\*]@>" <get-var __i /> /> /> />
        </foreach>
    </when>
</foreach>
<when <get-var __str />>
    <set-var __str=<group "%!slice" <get-var __str /> "\n" /> />
    <subst-in-var __str "%BASE" <get-var WML_SRC_BASENAME /> />
    <subst-in-var __str "%DIR" <get-var WML_SRC_DIRNAME /> />
    <get-var __str />
</when>
</define-tag>

##EOF##
