unit SimpleTextTranslation; // Lars Fosdal (lars.fosdal@gmail.com) - 2015.03.26 - Super Simple Text Translation // Add a language, and it is IMPOSSIBLE to miss a translation // Not much declaration that has to be done. // Constants can be added anywhere, and lookup is cheap (?). // You can change the translation by setting a single variable. // You can change the text of a specific translation by assigning new values. interface uses SysUtils; const _ = string(''); type TPSDLanguage = (ENG, NOR, SWE, GER); T_X = Array[TPSDLanguage] of String; function XLT(const T_ARG:T_X):String; overload; function XLT(const T_ARG:T_X; const Arguments: Array of const):String; overload; procedure XLT_Lang(const Lang: TPSDLanguage); overload; function XLT_Lang: TPSDLanguage; overload; const T_X_Language: T_X = ('ENG', 'NOR', 'SWE', 'GER'); T_Ready: T_X = ('Ready', 'Klar', _, _); T_Yes: T_X = ('Yes', 'Ja', _, _); T_No: T_X = ('No', 'Nei', 'Nej', 'Nein'); T_TheFirst: T_X = ('First', 'Første', 'Första', 'Erste'); T_TheLast: T_X = ('Last', 'Siste', 'Sista', 'Letzte'); // Examples // T_Date: T_X = ('mm d, yyyy', 'yyyy.mm.dd', 'dd.mm.yy', 'dd.MMM, yyyy'); // t := FormatDateTime(XLT(T_Date), Now); // // T_Issue: T_X = ('Issue %d', '%d. utgave', 'Nummer %d', 'Ausgabe %d'); // t := XLT(T_Issue, [IssueNumber]); implementation var Language: TPSDLanguage = ENG; function XLT(const T_ARG:T_X):String; var lang: TPSDLanguage; begin Result := T_ARG[Language]; if Result = _ then begin lang := High(TPSDLanguage); while (lang > Low(TPSDLanguage)) and (T_ARG[Lang] = _) do Dec(lang); Result := T_ARG[lang]; end; if Result = _ then raise Exception.Create('No text in T_ARG for language=' + T_X_Language[Language]); end; function XLT(const T_ARG:T_X; const Arguments: Array of const):String; begin Result := XLT(T_ARG); Result := Format(Result, Arguments); end; procedure XLT_Lang(const Lang: TPSDLanguage); begin Language := Lang; end; function XLT_Lang: TPSDLanguage; begin Result := Language; end; initialization XLT_Lang(ENG); end.