
; r - list with the labels
; v - list with the values
; c - counter for the strings
; lc - counter for the languages
; ll - list with the languages

macro __InitI18nEngine {
  local r, v, c, lc, lf, ll
  c equ 0
  lc equ 0

  macro _max_length lbl, [arg] \{
  \common
    \local max
    max=0
  \forward
     virtual at 0
       match lang:text, arg \\{
         db text
         if $ > max
           max = $
         end if
       \\}
     end virtual
   \common
     lbl\#.maxlen = max
  \}

  struc itext [val] \{
    \common
    .@here = 0
    \local max
      rept 1 x:c+1 \\{
        c equ x
        r\\#x equ .
      \\}
    _max_length ., val
    \forward
      match lang:text, val \\{
        rept 1 x:c \\\{
          v\\\#lang\\\#x equ text
        \\\}
        lf equ FALSE
        rept lc x:1 \\\{
          match =lang, ll\\\#x \\\\{
            lf equ TRUE
          \\\\}
        \\\}
        match =FALSE, lf \\\{
          rept 1 x:lc+1 \\\\{
            lc equ x
            ll\\\\#x equ lang
          \\\\}
        \\\}
      \\}
  \}

  macro __IncludeI18nStrings \{
    \common

    label I18nLanguages dword
    rept lc y \\{ match lang, ll\\#y \\\{
      dd \\\`lang
      dd Language\\\#lang
    \\\} \\}
    if lc>0
      dd 0
    end if

    rept lc y \\{ match lang, ll\\#y \\\{

      times (($ + 15) and $fffffff0) - $ db 0

      label Language\\\#lang byte

      rept c x \\\\{
        \\\\local f
        f equ TRUE
        match =v\\\\#lang\\\\#x, v\\\\#lang\\\\#x \\\\\{
          f equ FALSE
        \\\\\}

        times (($ + 3) and $fffffffc) - $ db 0

        match lbl1, r\\\\#x \\\\\{
          label lbl1\\\\\#.\\\\\#lang byte
        \\\\\}

        match =TRUE, f \\\\\{
          db v\\\\\#lang\\\\\#x
          db 0
        \\\\\}

        match lbl1, r\\\\#x \\\\\{
          match lbl2, lbl1\\\\\#.\\\\\#lang \\\\\\{
            times (lbl1\\\\\\#.maxlen - ($ - lbl2)+1) db 0
          \\\\\\}
        \\\\\}

      \\\\}
      Language\\\#lang\\\#.length = $ - Language\\\#lang
    \\\} \\}
  \}

  macro IncludeI18nLabels \{
    \common
    label I18nStrings byte

    rept c x \\{
      rb (($ + 3) and $fffffffc) - $
      match lbl, r\\#x \\\{
        lbl rb lbl\\\#.maxlen
            rb 1
      \\\}
    \\}

    I18nStrings.length = $ - I18nStrings
  \}
}

__InitI18nEngine