hedgewars/uLocale.pas
changeset 13043 9119726e22bc
parent 12461 e29a4238dc19
child 13157 c5453cbdeb87
equal deleted inserted replaced
13042:29357016f374 13043:9119726e22bc
    21 unit uLocale;
    21 unit uLocale;
    22 interface
    22 interface
    23 uses uTypes;
    23 uses uTypes;
    24 
    24 
    25 const MAX_EVENT_STRINGS = 255;
    25 const MAX_EVENT_STRINGS = 255;
       
    26 const MAX_FORMAT_STRING_SYMBOLS = 9;
    26 
    27 
    27 procedure LoadLocale(FileName: shortstring);
    28 procedure LoadLocale(FileName: shortstring);
    28 function  Format(fmt: shortstring; var arg: shortstring): shortstring;
    29 function  Format(fmt: shortstring; args: array of shortstring): shortstring;
    29 function  FormatA(fmt: ansistring; var arg: ansistring): ansistring;
    30 function  FormatA(fmt: ansistring; args: array of ansistring): ansistring;
       
    31 function  Format(fmt: shortstring; arg: shortstring): shortstring;
       
    32 function  FormatA(fmt: ansistring; arg: ansistring): ansistring;
    30 function  GetEventString(e: TEventId): ansistring;
    33 function  GetEventString(e: TEventId): ansistring;
    31 
    34 
    32 {$IFDEF HWLIBRARY}
    35 {$IFDEF HWLIBRARY}
    33 procedure LoadLocaleWrapper(path: pchar; userpath: pchar; filename: pchar); cdecl; export;
    36 procedure LoadLocaleWrapper(path: pchar; userpath: pchar; filename: pchar); cdecl; export;
    34 {$ENDIF}
    37 {$ENDIF}
   111         GetEventString:= '*missing translation*'
   114         GetEventString:= '*missing translation*'
   112     else
   115     else
   113         GetEventString:= trevt[e][GetRandom(trevt_n[e])]; // Pick a random message and return it
   116         GetEventString:= trevt[e][GetRandom(trevt_n[e])]; // Pick a random message and return it
   114 end;
   117 end;
   115 
   118 
   116 function Format(fmt: shortstring; var arg: shortstring): shortstring;
   119 // Format the string fmt.
   117 var i: LongInt;
   120 // Take a shortstring with placeholders %1, %2, %3, etc. and replace
   118 begin
   121 // them with the corresponding elements of an array with up to
   119 i:= Pos('%1', fmt);
   122 // MAX_FORMAT_STRING_SYMBOLS. Important! Each placeholder can only be
   120 if i = 0 then
   123 // used exactly once and numbers MUST NOT be skipped (e.g. using %1 and %3
   121     Format:= fmt
   124 // but not %2.
   122 else
   125 function Format(fmt: shortstring; args: array of shortstring): shortstring;
   123     Format:= copy(fmt, 1, i - 1) + arg + Format(copy(fmt, i + 2, Length(fmt) - i - 1), arg)
   126 var i, p: LongInt;
   124 end;
   127 tempstr: shortstring;
   125 
   128 begin
   126 function FormatA(fmt: ansistring; var arg: ansistring): ansistring;
   129 tempstr:= fmt;
   127 var i: LongInt;
   130 for i:=0 to MAX_FORMAT_STRING_SYMBOLS - 1 do
   128 begin
   131     begin
   129 i:= Pos('%1', fmt);
   132         p:= Pos('%'+IntToStr(i), tempstr);
   130 if i = 0 then
   133         if (p = 0) or (i >= Length(args)) then
   131     FormatA:= fmt
   134             break
   132 else
   135         else
   133     FormatA:= copy(fmt, 1, i - 1) + arg + FormatA(copy(fmt, i + 2, Length(fmt) - i - 1), arg)
   136             begin
       
   137             delete(tempstr, p, 2);
       
   138             insert(args[i], tempstr, p);
       
   139             end;
       
   140     end;
       
   141 Format:= tempstr;
       
   142 end;
       
   143 
       
   144 // Same as Format, but for ansistring
       
   145 function FormatA(fmt: ansistring; args: array of ansistring): ansistring;
       
   146 var i, p: LongInt;
       
   147 tempstr: ansistring;
       
   148 begin
       
   149 tempstr:= fmt;
       
   150 for i:=0 to MAX_FORMAT_STRING_SYMBOLS - 1 do
       
   151     begin
       
   152         p:= Pos('%'+IntToStr(i+1), tempstr);
       
   153         if (p = 0) or (i >= Length(args)) then
       
   154             break
       
   155         else
       
   156             begin
       
   157             delete(tempstr, p, 2);
       
   158             insert(args[i], tempstr, p);
       
   159             end;
       
   160     end;
       
   161 FormatA:= tempstr;
       
   162 end;
       
   163 
       
   164 // Same as Format above, but with only one placeholder %1, replaced by arg.
       
   165 function Format(fmt: shortstring; arg: shortstring): shortstring;
       
   166 begin
       
   167     Format:= Format(fmt, [arg]);
       
   168 end;
       
   169 
       
   170 // Same as above, but for ansistring
       
   171 function FormatA(fmt: ansistring; arg: ansistring): ansistring;
       
   172 begin
       
   173     FormatA:= FormatA(fmt, [arg]);
   134 end;
   174 end;
   135 
   175 
   136 {$IFDEF HWLIBRARY}
   176 {$IFDEF HWLIBRARY}
   137 procedure LoadLocaleWrapper(path: pchar; userpath: pchar; filename: pchar); cdecl; export;
   177 procedure LoadLocaleWrapper(path: pchar; userpath: pchar; filename: pchar); cdecl; export;
   138 begin
   178 begin