;;; lang.mhtml: -*- Meta-HTML -*-  Language helper macros.
;;; 
;;;  Copyright (c) 1998 Brian J. Fox
;;;  Author: Brian J. Fox (bfox@ai.mit.edu) Tue Apr 28 22:12:42 1998.

<set-var lang::languages[]=
  <prog
    English:en
    Deutsch:de
    Francais:fr:Fran&ccedil;ais
    Italian:it:
    Espanol:es:Espa&ntilde;ol>>

<defun lang &key en de fr it es>
  ;;; Provides an interface for selecting one of several phrases
  ;;; in different languages, based on the value of <var authdata::language>.
  ;;;
  ;;; Use in the following situations:
  ;;; <example code>
  ;;; <input type=submit name=action value=<lang en=Now de=Jetzt>>
  ;;; </example>
  ;;;
  <defvar choice <get-var authdata::language>>
  <defvar choice en>
  <if <get-var-once <get-var-once choice>>
      <get-var-once <get-var-once choice>>
    <get-var-once en>>
</defun>

<defun lang-message &key en de fr it es &rest message>
  ;;; This function uses the value of <var authdata::language> to select a
  ;;; language, and then calls <var message> with that choice.
  <defvar choice <or <get-var authdata::language>
		     <get-var default::language>
		     en>>
  <if <get-var-once <get-var-once choice>>
      <message <get-var-once <get-var-once choice>>>
    <message <or <get-var-once en> <get-var-once message>>>>
</defun>

<defun lang-inform &key en de fr it es &rest info>
  ;;; This function uses the value of <var authdata::language> to select a
  ;;; language, and then calls <var inform> with <info>.
  <defvar choice <or <get-var authdata::language>
		     <get-var default::language>
		     en>>
  <if <get-var-once <get-var-once choice>>
      <inform <get-var-once <get-var-once choice>>>
    <inform <or <get-var-once en> <get-var-once info>>>>
</defun>

<defmacro lang::en whitespace=delete>
  <when <or <not <get-var authdata::language>>
	    <string-eq <get-var authdata::language> en>>>
    %body
  </when>
</defmacro>

<defmacro lang::de whitespace=delete>
  <when <string-eq <get-var authdata::language> de>>
    %body
  </when>
</defmacro>

<defmacro lang::fr whitespace=delete>
  <when <string-eq <get-var authdata::language> fr>>
    %body
  </when>
</defmacro>

<defmacro lang::it whitespace=delete>
  <when <string-eq <get-var authdata::language> it>>
    %body
  </when>
</defmacro>

<defmacro lang::es whitespace=delete>
  <when <string-eq <get-var authdata::language> es>>
    %body
  </when>
</defmacro>

<defun lang::select-language varname>
  <parser::canonicalize-var varname>
  <set-var
    var=<get-var-once varname>
    varname=<subst-in-string <get-var-once varname> "^[^:]::" "">>

  <select name=<get-var-once varname>>
    <foreach :lang lang::languages>
      <concat "\n  ">
      <set-var :info[]=<subst-in-string <get-var :lang> ":" "\n">>
      <option value="<get-var :info[1]>" <if <string-eq
					      <get-var-once <get-var var>>
					      <get-var :info[1]>>
					     "SELECTED">>
      <or <get-var-once :info[2]> <get-var-once :info[0]>>
      </option>
    </foreach>
    <concat "\n">
  </select>
</defun>
