
int transdte_handle;
string transdte_lang;
string transdte_string;
string transdte_path;


// read choices

void transdte_readchoices(void)
{
 int h;
 string s;

 h = fileopen(transdte_path + ".Choices", "rb");
 if(h)
 {
  filereads(s, h);
  transdte_lang = s;
  fileclose(h);
 }
 else
 {
  transdte_lang = "{TRANSDTE_02}";
  translate(transdte_lang);
 }
}


// write choices

void transdte_writechoices(void)
{
 int h;

 h = fileopen(transdte_path + ".Choices", "wb");
 if(h)
 {
  filewrites(transdte_lang, h);
  fileclose(h);
 }
}


// read language data from file

void transdte_readdata()
{
 int h;
 string s;

 transdte_string = "";
 h = fileopen(transdte_path + ".language." + transdte_lang, "rb");
 if (!h)
 {
  s = "{TRANSDTE_03}";
  translate(s);
  errorbox(transdte_lang + s);
 }
 else
  while(!fileeof(h))
  {
   filereads(s, h);
   transdte_string += s;
  }
 fileclose(h);
}


// search for string t in string s, and set t to converted string

int transdte_find(string s, string &t)
{
 int p, b, e;

 if((p = s / t) >= 0)
 {
  b = p + slen(t);
  for(e = b; mids(s, e, 1) != "\n" && e != slen(s); ++e);
  t = mids(s, b, e - b);
 }
 return p;
}


// deal with 'TransDate' menu entry

int transdte_entry(int entry, int subcode)
{
 int i = 0, n;
 string c, s, f, d = "";

 if(subcode >= 0)
  return(0);

 gettimeformat(f, s);
 
 while(i < slen(f))
 {
  c = mids(f, i++, 1);
  if (c != "%")
   d += c;
  else
  {
   n = mids(f, i, 1) == "Z" ? 3 : 2;
   c = mids(f, i, n);
   i += n;

   if(c == "WE" || c == "W3")
   {
    gettimestring("%ZWN", s);
    s = c + s + "/";
    transdte_find(transdte_string, s);
   }
   else
    if(c == "MO" || c == "M3")
    {
     gettimestring("%ZMN", s);
     s = c + s + "/";
     transdte_find(transdte_string, s);
    }
   else
    if(c == "ST")
    {
     gettimestring("%ZDY", s);
     s = c + s + "/";
     transdte_find(transdte_string, s);
    }
   else
    gettimestring("%" + c, s);

   d += s;
  }
 }
 type(d);
 return(0);
}


// shade or un-shade 'TransDate' menu entry

int transdte_flags(int entry, string &text)
{
 return((activetype(TEXTFRAME) > 1) ? 0 : SHADED);
}


// deal with 'Language' menu entries

int transdte_langentry(int entry, int subcode)
{
 transdte_lang = string_menu(transdte_handle, entry);
 transdte_readdata();
 transdte_writechoices();
 return(0);
}


// tick 'Language' menu entry

int transdte_langflags(int entry, string &text)
{
 if(text == transdte_lang)
  return(TICKED);
 return(0);
}


// build menu of languages

int transdte_menu(int open)
{
 string s;

 if(transdte_handle)
 {
  delete_menu(transdte_handle);
  transdte_handle=create_menu("{TRANSDTE_00}");
 }

 startscan();
 while(nextobject(transdte_path + ".Language", "*", s))
  addentry_menu(transdte_handle,"transdte_langentry","transdte_langflags","","", s);
 return(transdte_handle);
}


// create 'TransDate' menu and add it to 'Applets' menu

void main(void)
{
 script_menu_initialise();

 transdte_path = "OvationPro$AppletsDir";
 getenvs(transdte_path);
 transdte_path += ".!TransDte";

 transdte_handle = create_menu("{TRANSDTE_00}");
 addentry_menu(script_handle,"transdte_entry","transdte_flags","transdte_menu","C_D","{TRANSDTE_01}");
 transdte_readchoices();
 transdte_readdata();
}
