hedgewars/uLocale.pas
changeset 13832 10a3b80130b5
parent 13457 b587cdb03bac
child 13877 8c702a4839ec
equal deleted inserted replaced
13831:b07610de9957 13832:10a3b80130b5
    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;
       
    27 
    26 
    28 procedure LoadLocale(FileName: shortstring);
    27 procedure LoadLocale(FileName: shortstring);
    29 function  Format(fmt: shortstring; args: array of shortstring): shortstring;
       
    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;
       
    33 function  GetEventString(e: TEventId): ansistring;
    28 function  GetEventString(e: TEventId): ansistring;
       
    29 
       
    30 function Format(fmt: shortstring; arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9: shortstring; argCount: Byte): shortstring;
       
    31 function Format(fmt: shortstring; arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9: shortstring): shortstring;
       
    32 function Format(fmt: shortstring; arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8: shortstring): shortstring;
       
    33 function Format(fmt: shortstring; arg1, arg2, arg3, arg4, arg5, arg6, arg7: shortstring): shortstring;
       
    34 function Format(fmt: shortstring; arg1, arg2, arg3, arg4, arg5, arg6: shortstring): shortstring;
       
    35 function Format(fmt: shortstring; arg1, arg2, arg3, arg4, arg5: shortstring): shortstring;
       
    36 function Format(fmt: shortstring; arg1, arg2, arg3, arg4: shortstring): shortstring;
       
    37 function Format(fmt: shortstring; arg1, arg2, arg3: shortstring): shortstring;
       
    38 function Format(fmt: shortstring; arg1, arg2: shortstring): shortstring;
       
    39 function Format(fmt: shortstring; arg1: shortstring): shortstring;
       
    40 function FormatA(fmt: ansistring; arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9: ansistring; argCount: Byte): ansistring;
       
    41 function FormatA(fmt: ansistring; arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9: ansistring): ansistring;
       
    42 function FormatA(fmt: ansistring; arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8: ansistring): ansistring;
       
    43 function FormatA(fmt: ansistring; arg1, arg2, arg3, arg4, arg5, arg6, arg7: ansistring): ansistring;
       
    44 function FormatA(fmt: ansistring; arg1, arg2, arg3, arg4, arg5, arg6: ansistring): ansistring;
       
    45 function FormatA(fmt: ansistring; arg1, arg2, arg3, arg4, arg5: ansistring): ansistring;
       
    46 function FormatA(fmt: ansistring; arg1, arg2, arg3, arg4: ansistring): ansistring;
       
    47 function FormatA(fmt: ansistring; arg1, arg2, arg3: ansistring): ansistring;
       
    48 function FormatA(fmt: ansistring; arg1, arg2: ansistring): ansistring;
       
    49 function FormatA(fmt: ansistring; arg1: ansistring): ansistring;
    34 
    50 
    35 {$IFDEF HWLIBRARY}
    51 {$IFDEF HWLIBRARY}
    36 procedure LoadLocaleWrapper(path: pchar; userpath: pchar; filename: pchar); cdecl; export;
    52 procedure LoadLocaleWrapper(path: pchar; userpath: pchar; filename: pchar); cdecl; export;
    37 {$ENDIF}
    53 {$ENDIF}
    38 
    54 
   117     else
   133     else
   118         GetEventString:= trevt[e][GetRandom(trevt_n[e])]; // Pick a random message and return it
   134         GetEventString:= trevt[e][GetRandom(trevt_n[e])]; // Pick a random message and return it
   119 end;
   135 end;
   120 
   136 
   121 // Format the string fmt.
   137 // Format the string fmt.
   122 // Take a shortstring with placeholders %1, %2, %3, etc. and replace
   138 // Take a shortstring with placeholders %1, %2, %3, ... %9. and replace
   123 // them with the corresponding elements of an array with up to
   139 // them with the corresponding elements of an array with up to
   124 // MAX_FORMAT_STRING_SYMBOLS. Important! Each placeholder can only be
   140 // argCount. ArgCount must not be larger than 9.
   125 // used exactly once and numbers MUST NOT be skipped (e.g. using %1 and %3
   141 // Each placeholder must be used exactly once and numbers MUST NOT be
   126 // but not %2.
   142 // skipped (e.g. using %1 and %3 but not %2.
   127 function Format(fmt: shortstring; args: array of shortstring): shortstring;
   143 function Format(fmt: shortstring; arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9: shortstring; argCount: Byte): shortstring;
   128 var i, p: LongInt;
   144 var i, p: LongInt;
   129 tempstr: shortstring;
   145 tempstr, curArg: shortstring;
   130 begin
   146 begin
   131 tempstr:= fmt;
   147 tempstr:= fmt;
   132 for i:=0 to MAX_FORMAT_STRING_SYMBOLS - 1 do
   148 for i:=0 to argCount - 1 do
   133     begin
   149     begin
       
   150         case i of
       
   151             0: curArg:= arg1;
       
   152             1: curArg:= arg2;
       
   153             2: curArg:= arg3;
       
   154             3: curArg:= arg4;
       
   155             4: curArg:= arg5;
       
   156             5: curArg:= arg6;
       
   157             6: curArg:= arg7;
       
   158             7: curArg:= arg8;
       
   159             8: curArg:= arg9;
       
   160         end;
       
   161 
   134         p:= Pos('%'+IntToStr(i+1), tempstr);
   162         p:= Pos('%'+IntToStr(i+1), tempstr);
   135         if (p = 0) or (i >= Length(args)) then
   163         if (p = 0) then
   136             break
   164             break
   137         else
   165         else
   138             begin
   166             begin
   139             delete(tempstr, p, 2);
   167             delete(tempstr, p, 2);
   140             insert(args[i], tempstr, p);
   168             insert(curArg, tempstr, p);
   141             end;
   169             end;
   142     end;
   170     end;
   143 Format:= tempstr;
   171 Format:= tempstr;
   144 end;
   172 end;
   145 
   173 
   146 // Same as Format, but for ansistring
   174 // Same as Format, but for ansistring
   147 function FormatA(fmt: ansistring; args: array of ansistring): ansistring;
   175 function FormatA(fmt: ansistring; arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9: ansistring; argCount: Byte): ansistring;
   148 var i, p: LongInt;
   176 var i, p: LongInt;
   149 tempstr: ansistring;
   177 tempstr, curArg: ansistring;
   150 begin
   178 begin
   151 tempstr:= fmt;
   179 tempstr:= fmt;
   152 for i:=0 to MAX_FORMAT_STRING_SYMBOLS - 1 do
   180 for i:=0 to argCount - 1 do
   153     begin
   181     begin
       
   182         case i of
       
   183             0: curArg:= arg1;
       
   184             1: curArg:= arg2;
       
   185             2: curArg:= arg3;
       
   186             3: curArg:= arg4;
       
   187             4: curArg:= arg5;
       
   188             5: curArg:= arg6;
       
   189             6: curArg:= arg7;
       
   190             7: curArg:= arg8;
       
   191             8: curArg:= arg9;
       
   192         end;
       
   193 
   154         p:= Pos('%'+IntToStr(i+1), tempstr);
   194         p:= Pos('%'+IntToStr(i+1), tempstr);
   155         if (p = 0) or (i >= Length(args)) then
   195         if (p = 0) then
   156             break
   196             break
   157         else
   197         else
   158             begin
   198             begin
   159             delete(tempstr, p, 2);
   199             delete(tempstr, p, 2);
   160             insert(args[i], tempstr, p);
   200             insert(curArg, tempstr, p);
   161             end;
   201             end;
   162     end;
   202     end;
   163 FormatA:= tempstr;
   203 FormatA:= tempstr;
   164 end;
   204 end;
   165 
   205 
   166 // Same as Format above, but with only one placeholder %1, replaced by arg.
   206 // The following functions are just shortcuts of Format/FormatA, with fewer argument counts
   167 function Format(fmt: shortstring; arg: shortstring): shortstring;
   207 function Format(fmt: shortstring; arg1: shortstring): shortstring;
   168 begin
   208 begin
   169     Format:= Format(fmt, [arg]);
   209     Format:= Format(fmt, arg1, '', '', '', '', '', '', '', '', 1);
   170 end;
   210 end;
   171 
   211 function Format(fmt: shortstring; arg1, arg2: shortstring): shortstring;
   172 // Same as above, but for ansistring
   212 begin
   173 function FormatA(fmt: ansistring; arg: ansistring): ansistring;
   213     Format:= Format(fmt, arg1, arg2, '', '', '', '', '', '', '', 2);
   174 begin
   214 end;
   175     FormatA:= FormatA(fmt, [arg]);
   215 function Format(fmt: shortstring; arg1, arg2, arg3: shortstring): shortstring;
       
   216 begin
       
   217     Format:= Format(fmt, arg1, arg2, arg3, '', '', '', '', '', '', 3);
       
   218 end;
       
   219 function Format(fmt: shortstring; arg1, arg2, arg3, arg4: shortstring): shortstring;
       
   220 begin
       
   221     Format:= Format(fmt, arg1, arg2, arg3, arg4, '', '', '', '', '', 4);
       
   222 end;
       
   223 function Format(fmt: shortstring; arg1, arg2, arg3, arg4, arg5: shortstring): shortstring;
       
   224 begin
       
   225     Format:= Format(fmt, arg1, arg2, arg3, arg4, arg5, '', '', '', '', 5);
       
   226 end;
       
   227 function Format(fmt: shortstring; arg1, arg2, arg3, arg4, arg5, arg6: shortstring): shortstring;
       
   228 begin
       
   229     Format:= Format(fmt, arg1, arg2, arg3, arg4, arg5, arg6, '', '', '', 6);
       
   230 end;
       
   231 function Format(fmt: shortstring; arg1, arg2, arg3, arg4, arg5, arg6, arg7: shortstring): shortstring;
       
   232 begin
       
   233     Format:= Format(fmt, arg1, arg2, arg3, arg4, arg5, arg6, arg7, '', '', 7);
       
   234 end;
       
   235 function Format(fmt: shortstring; arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8: shortstring): shortstring;
       
   236 begin
       
   237     Format:= Format(fmt, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, '', 8);
       
   238 end;
       
   239 function Format(fmt: shortstring; arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9: shortstring): shortstring;
       
   240 begin
       
   241     Format:= Format(fmt, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, 9);
       
   242 end;
       
   243 
       
   244 function FormatA(fmt: ansistring; arg1: ansistring): ansistring;
       
   245 begin
       
   246     FormatA:= FormatA(fmt, arg1, '', '', '', '', '', '', '', '', 1);
       
   247 end;
       
   248 function FormatA(fmt: ansistring; arg1, arg2: ansistring): ansistring;
       
   249 begin
       
   250     FormatA:= FormatA(fmt, arg1, arg2, '', '', '', '', '', '', '', 2);
       
   251 end;
       
   252 function FormatA(fmt: ansistring; arg1, arg2, arg3: ansistring): ansistring;
       
   253 begin
       
   254     FormatA:= FormatA(fmt, arg1, arg2, arg3, '', '', '', '', '', '', 3);
       
   255 end;
       
   256 function FormatA(fmt: ansistring; arg1, arg2, arg3, arg4: ansistring): ansistring;
       
   257 begin
       
   258     FormatA:= FormatA(fmt, arg1, arg2, arg3, arg4, '', '', '', '', '', 4);
       
   259 end;
       
   260 function FormatA(fmt: ansistring; arg1, arg2, arg3, arg4, arg5: ansistring): ansistring;
       
   261 begin
       
   262     FormatA:= FormatA(fmt, arg1, arg2, arg3, arg4, arg5, '', '', '', '', 5);
       
   263 end;
       
   264 function FormatA(fmt: ansistring; arg1, arg2, arg3, arg4, arg5, arg6: ansistring): ansistring;
       
   265 begin
       
   266     FormatA:= FormatA(fmt, arg1, arg2, arg3, arg4, arg5, arg6, '', '', '', 6);
       
   267 end;
       
   268 function FormatA(fmt: ansistring; arg1, arg2, arg3, arg4, arg5, arg6, arg7: ansistring): ansistring;
       
   269 begin
       
   270     FormatA:= FormatA(fmt, arg1, arg2, arg3, arg4, arg5, arg6, arg7, '', '', 7);
       
   271 end;
       
   272 function FormatA(fmt: ansistring; arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8: ansistring): ansistring;
       
   273 begin
       
   274     FormatA:= FormatA(fmt, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, '', 8);
       
   275 end;
       
   276 function FormatA(fmt: ansistring; arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9: ansistring): ansistring;
       
   277 begin
       
   278     FormatA:= FormatA(fmt, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, 9);
   176 end;
   279 end;
   177 
   280 
   178 {$IFDEF HWLIBRARY}
   281 {$IFDEF HWLIBRARY}
   179 procedure LoadLocaleWrapper(path: pchar; userpath: pchar; filename: pchar); cdecl; export;
   282 procedure LoadLocaleWrapper(path: pchar; userpath: pchar; filename: pchar); cdecl; export;
   180 begin
   283 begin