Advertisement
LarsFosdal

Super Simple Text Translation

Mar 26th, 2015
557
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 2.14 KB | None | 0 0
  1. unit SimpleTextTranslation;
  2. // Lars Fosdal (lars.fosdal@gmail.com) - 2015.03.26 - Super Simple Text Translation
  3. // Add a language, and it is IMPOSSIBLE to miss a translation
  4. // Not much declaration that has to be done.
  5. // Constants can be added anywhere, and lookup is cheap (?).
  6. // You can change the translation by setting a single variable.
  7. // You can change the text of a specific translation by assigning new values.
  8.  
  9. interface
  10. uses
  11.   SysUtils;
  12.  
  13. const
  14.   _ = string('');
  15.  
  16. type
  17.   TPSDLanguage = (ENG, NOR, SWE, GER);
  18.   T_X = Array[TPSDLanguage] of String;
  19.  
  20. function XLT(const T_ARG:T_X):String; overload;
  21. function XLT(const T_ARG:T_X; const Arguments: Array of const):String; overload;
  22.  
  23. procedure XLT_Lang(const Lang: TPSDLanguage); overload;
  24. function XLT_Lang: TPSDLanguage; overload;
  25.  
  26. const
  27.   T_X_Language: T_X = ('ENG', 'NOR', 'SWE', 'GER');
  28.   T_Ready: T_X = ('Ready', 'Klar', _, _);
  29.   T_Yes: T_X = ('Yes', 'Ja', _, _);
  30.   T_No: T_X = ('No', 'Nei', 'Nej', 'Nein');
  31.   T_TheFirst: T_X = ('First', 'Første', 'Första', 'Erste');
  32.   T_TheLast: T_X = ('Last', 'Siste', 'Sista', 'Letzte');
  33.  
  34.   // Examples
  35.   // T_Date: T_X = ('mm d, yyyy', 'yyyy.mm.dd', 'dd.mm.yy', 'dd.MMM, yyyy');
  36.   // t := FormatDateTime(XLT(T_Date), Now);
  37.   //
  38.   // T_Issue: T_X = ('Issue %d', '%d. utgave', 'Nummer %d', 'Ausgabe %d');
  39.   // t := XLT(T_Issue, [IssueNumber]);
  40.  
  41. implementation
  42.  
  43. var
  44.   Language: TPSDLanguage = ENG;
  45.  
  46. function XLT(const T_ARG:T_X):String;
  47. var
  48.   lang: TPSDLanguage;
  49. begin
  50.   Result := T_ARG[Language];
  51.   if Result = _
  52.   then begin
  53.     lang := High(TPSDLanguage);
  54.     while (lang > Low(TPSDLanguage)) and (T_ARG[Lang] = _)
  55.     do Dec(lang);
  56.     Result := T_ARG[lang];
  57.   end;
  58.   if Result = _
  59.    then raise Exception.Create('No text in T_ARG for language=' + T_X_Language[Language]);
  60. end;
  61.  
  62. function XLT(const T_ARG:T_X; const Arguments: Array of const):String;
  63. begin
  64.   Result := XLT(T_ARG);
  65.   Result := Format(Result, Arguments);
  66. end;
  67.  
  68. procedure XLT_Lang(const Lang: TPSDLanguage);
  69. begin
  70.   Language := Lang;
  71. end;
  72.  
  73. function XLT_Lang: TPSDLanguage;
  74. begin
  75.   Result := Language;
  76. end;
  77.  
  78. initialization
  79.   XLT_Lang(ENG);
  80. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement