hedgewars/uLocale.pas
branchui-scaling
changeset 15283 c4fd2813b127
parent 14861 acb0526bf458
child 15960 79b1129b4d03
equal deleted inserted replaced
13390:0135e64c6c66 15283:c4fd2813b127
    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 
    39 implementation
    55 implementation
    40 uses uRandom, uUtils, uVariables, uDebug, uPhysFSLayer;
    56 uses SysUtils, uRandom, uUtils, uVariables, uDebug, uPhysFSLayer;
    41 
    57 
    42 var trevt: array[TEventId] of array [0..Pred(MAX_EVENT_STRINGS)] of ansistring;
    58 var trevt: array[TEventId] of array [0..Pred(MAX_EVENT_STRINGS)] of ansistring;
    43     trevt_n: array[TEventId] of integer;
    59     trevt_n: array[TEventId] of integer;
    44 
    60 
    45 procedure LoadLocale(FileName: shortstring);
    61 procedure LoadLocale(FileName: shortstring);
    47     f: pfsFile;
    63     f: pfsFile;
    48     a, b, c: LongInt;
    64     a, b, c: LongInt;
    49     first: array[TEventId] of boolean;
    65     first: array[TEventId] of boolean;
    50     e: TEventId;
    66     e: TEventId;
    51 begin
    67 begin
       
    68 {- TODO: Add support for localized decimal separator in Pas2C -}
       
    69 {$IFNDEF PAS2C}
       
    70 lDecimalSeparator:= FormatSettings.DecimalSeparator;
       
    71 {$ENDIF}
       
    72 
    52 for e:= Low(TEventId) to High(TEventId) do
    73 for e:= Low(TEventId) to High(TEventId) do
    53     first[e]:= true;
    74     first[e]:= true;
    54 
    75 
    55 f:= pfsOpenRead(FileName);
    76 f:= pfsOpenRead(FileName);
    56 checkFails(f <> nil, 'Cannot load locale "' + FileName + '"', false);
    77 checkFails(f <> nil, 'Cannot load locale "' + FileName + '"', false);
   100                 trammoc[TAmmoStrId(b)]:= s;
   121                 trammoc[TAmmoStrId(b)]:= s;
   101             4: if (b >=0) and (b <= ord(High(TAmmoStrId))) then
   122             4: if (b >=0) and (b <= ord(High(TAmmoStrId))) then
   102                 trammod[TAmmoStrId(b)]:= s;
   123                 trammod[TAmmoStrId(b)]:= s;
   103             5: if (b >=0) and (b <= ord(High(TGoalStrId))) then
   124             5: if (b >=0) and (b <= ord(High(TGoalStrId))) then
   104                 trgoal[TGoalStrId(b)]:= s;
   125                 trgoal[TGoalStrId(b)]:= s;
       
   126             6: if (b >=0) and (b <= ord(High(TCmdHelpStrId))) then
       
   127                 trcmd[TCmdHelpStrId(b)]:= s;
   105            end;
   128            end;
   106        end;
   129        end;
   107    pfsClose(f);
   130    pfsClose(f);
   108    end;
   131    end;
   109 end;
   132 end;
   115     else
   138     else
   116         GetEventString:= trevt[e][GetRandom(trevt_n[e])]; // Pick a random message and return it
   139         GetEventString:= trevt[e][GetRandom(trevt_n[e])]; // Pick a random message and return it
   117 end;
   140 end;
   118 
   141 
   119 // Format the string fmt.
   142 // Format the string fmt.
   120 // Take a shortstring with placeholders %1, %2, %3, etc. and replace
   143 // Take a shortstring with placeholders %1, %2, %3, ... %9. and replace
   121 // them with the corresponding elements of an array with up to
   144 // them with the corresponding elements of an array with up to
   122 // MAX_FORMAT_STRING_SYMBOLS. Important! Each placeholder can only be
   145 // argCount. ArgCount must not be larger than 9.
   123 // used exactly once and numbers MUST NOT be skipped (e.g. using %1 and %3
   146 // Each placeholder must be used exactly once and numbers MUST NOT be
   124 // but not %2.
   147 // skipped (e.g. using %1 and %3 but not %2.
   125 function Format(fmt: shortstring; args: array of shortstring): shortstring;
   148 function Format(fmt: shortstring; arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9: shortstring; argCount: Byte): shortstring;
   126 var i, p: LongInt;
   149 var i, p: LongInt;
   127 tempstr: shortstring;
   150 tempstr, curArg: shortstring;
   128 begin
   151 begin
   129 tempstr:= fmt;
   152 tempstr:= fmt;
   130 for i:=0 to MAX_FORMAT_STRING_SYMBOLS - 1 do
   153 for i:=0 to argCount - 1 do
   131     begin
   154     begin
       
   155         case i of
       
   156             0: curArg:= arg1;
       
   157             1: curArg:= arg2;
       
   158             2: curArg:= arg3;
       
   159             3: curArg:= arg4;
       
   160             4: curArg:= arg5;
       
   161             5: curArg:= arg6;
       
   162             6: curArg:= arg7;
       
   163             7: curArg:= arg8;
       
   164             8: curArg:= arg9;
       
   165         end;
       
   166 
       
   167         repeat
   132         p:= Pos('%'+IntToStr(i+1), tempstr);
   168         p:= Pos('%'+IntToStr(i+1), tempstr);
   133         if (p = 0) or (i >= Length(args)) then
   169         if (p <> 0) then
   134             break
       
   135         else
       
   136             begin
   170             begin
   137             delete(tempstr, p, 2);
   171             delete(tempstr, p, 2);
   138             insert(args[i], tempstr, p);
   172             insert(curArg, tempstr, p);
   139             end;
   173             end;
       
   174         until (p = 0);
   140     end;
   175     end;
   141 Format:= tempstr;
   176 Format:= tempstr;
   142 end;
   177 end;
   143 
   178 
   144 // Same as Format, but for ansistring
   179 // Same as Format, but for ansistring
   145 function FormatA(fmt: ansistring; args: array of ansistring): ansistring;
   180 function FormatA(fmt: ansistring; arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9: ansistring; argCount: Byte): ansistring;
   146 var i, p: LongInt;
   181 var i, p: LongInt;
   147 tempstr: ansistring;
   182 tempstr, curArg: ansistring;
   148 begin
   183 begin
   149 tempstr:= fmt;
   184 tempstr:= fmt;
   150 for i:=0 to MAX_FORMAT_STRING_SYMBOLS - 1 do
   185 for i:=0 to argCount - 1 do
   151     begin
   186     begin
       
   187         case i of
       
   188             0: curArg:= arg1;
       
   189             1: curArg:= arg2;
       
   190             2: curArg:= arg3;
       
   191             3: curArg:= arg4;
       
   192             4: curArg:= arg5;
       
   193             5: curArg:= arg6;
       
   194             6: curArg:= arg7;
       
   195             7: curArg:= arg8;
       
   196             8: curArg:= arg9;
       
   197         end;
       
   198 
       
   199         repeat
   152         p:= Pos('%'+IntToStr(i+1), tempstr);
   200         p:= Pos('%'+IntToStr(i+1), tempstr);
   153         if (p = 0) or (i >= Length(args)) then
   201         if (p <> 0) then
   154             break
       
   155         else
       
   156             begin
   202             begin
   157             delete(tempstr, p, 2);
   203             delete(tempstr, p, 2);
   158             insert(args[i], tempstr, p);
   204             insert(curArg, tempstr, p);
   159             end;
   205             end;
       
   206         until (p = 0);
   160     end;
   207     end;
   161 FormatA:= tempstr;
   208 FormatA:= tempstr;
   162 end;
   209 end;
   163 
   210 
   164 // Same as Format above, but with only one placeholder %1, replaced by arg.
   211 // The following functions are just shortcuts of Format/FormatA, with fewer argument counts
   165 function Format(fmt: shortstring; arg: shortstring): shortstring;
   212 function Format(fmt: shortstring; arg1: shortstring): shortstring;
   166 begin
   213 begin
   167     Format:= Format(fmt, [arg]);
   214     Format:= Format(fmt, arg1, '', '', '', '', '', '', '', '', 1);
   168 end;
   215 end;
   169 
   216 function Format(fmt: shortstring; arg1, arg2: shortstring): shortstring;
   170 // Same as above, but for ansistring
   217 begin
   171 function FormatA(fmt: ansistring; arg: ansistring): ansistring;
   218     Format:= Format(fmt, arg1, arg2, '', '', '', '', '', '', '', 2);
   172 begin
   219 end;
   173     FormatA:= FormatA(fmt, [arg]);
   220 function Format(fmt: shortstring; arg1, arg2, arg3: shortstring): shortstring;
       
   221 begin
       
   222     Format:= Format(fmt, arg1, arg2, arg3, '', '', '', '', '', '', 3);
       
   223 end;
       
   224 function Format(fmt: shortstring; arg1, arg2, arg3, arg4: shortstring): shortstring;
       
   225 begin
       
   226     Format:= Format(fmt, arg1, arg2, arg3, arg4, '', '', '', '', '', 4);
       
   227 end;
       
   228 function Format(fmt: shortstring; arg1, arg2, arg3, arg4, arg5: shortstring): shortstring;
       
   229 begin
       
   230     Format:= Format(fmt, arg1, arg2, arg3, arg4, arg5, '', '', '', '', 5);
       
   231 end;
       
   232 function Format(fmt: shortstring; arg1, arg2, arg3, arg4, arg5, arg6: shortstring): shortstring;
       
   233 begin
       
   234     Format:= Format(fmt, arg1, arg2, arg3, arg4, arg5, arg6, '', '', '', 6);
       
   235 end;
       
   236 function Format(fmt: shortstring; arg1, arg2, arg3, arg4, arg5, arg6, arg7: shortstring): shortstring;
       
   237 begin
       
   238     Format:= Format(fmt, arg1, arg2, arg3, arg4, arg5, arg6, arg7, '', '', 7);
       
   239 end;
       
   240 function Format(fmt: shortstring; arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8: shortstring): shortstring;
       
   241 begin
       
   242     Format:= Format(fmt, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, '', 8);
       
   243 end;
       
   244 function Format(fmt: shortstring; arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9: shortstring): shortstring;
       
   245 begin
       
   246     Format:= Format(fmt, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, 9);
       
   247 end;
       
   248 
       
   249 function FormatA(fmt: ansistring; arg1: ansistring): ansistring;
       
   250 begin
       
   251     FormatA:= FormatA(fmt, arg1, ansistring(''), ansistring(''), ansistring(''), ansistring(''), ansistring(''), ansistring(''), ansistring(''), ansistring(''), 1);
       
   252 end;
       
   253 function FormatA(fmt: ansistring; arg1, arg2: ansistring): ansistring;
       
   254 begin
       
   255     FormatA:= FormatA(fmt, arg1, arg2, ansistring(''), ansistring(''), ansistring(''), ansistring(''), ansistring(''), ansistring(''), ansistring(''), 2);
       
   256 end;
       
   257 function FormatA(fmt: ansistring; arg1, arg2, arg3: ansistring): ansistring;
       
   258 begin
       
   259     FormatA:= FormatA(fmt, arg1, arg2, arg3, ansistring(''), ansistring(''), ansistring(''), ansistring(''), ansistring(''), ansistring(''), 3);
       
   260 end;
       
   261 function FormatA(fmt: ansistring; arg1, arg2, arg3, arg4: ansistring): ansistring;
       
   262 begin
       
   263     FormatA:= FormatA(fmt, arg1, arg2, arg3, arg4, ansistring(''), ansistring(''), ansistring(''), ansistring(''), ansistring(''), 4);
       
   264 end;
       
   265 function FormatA(fmt: ansistring; arg1, arg2, arg3, arg4, arg5: ansistring): ansistring;
       
   266 begin
       
   267     FormatA:= FormatA(fmt, arg1, arg2, arg3, arg4, arg5, ansistring(''), ansistring(''), ansistring(''), ansistring(''), 5);
       
   268 end;
       
   269 function FormatA(fmt: ansistring; arg1, arg2, arg3, arg4, arg5, arg6: ansistring): ansistring;
       
   270 begin
       
   271     FormatA:= FormatA(fmt, arg1, arg2, arg3, arg4, arg5, arg6, ansistring(''), ansistring(''), ansistring(''), 6);
       
   272 end;
       
   273 function FormatA(fmt: ansistring; arg1, arg2, arg3, arg4, arg5, arg6, arg7: ansistring): ansistring;
       
   274 begin
       
   275     FormatA:= FormatA(fmt, arg1, arg2, arg3, arg4, arg5, arg6, arg7, ansistring(''), ansistring(''), 7);
       
   276 end;
       
   277 function FormatA(fmt: ansistring; arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8: ansistring): ansistring;
       
   278 begin
       
   279     FormatA:= FormatA(fmt, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, ansistring(''), 8);
       
   280 end;
       
   281 function FormatA(fmt: ansistring; arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9: ansistring): ansistring;
       
   282 begin
       
   283     FormatA:= FormatA(fmt, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, 9);
   174 end;
   284 end;
   175 
   285 
   176 {$IFDEF HWLIBRARY}
   286 {$IFDEF HWLIBRARY}
   177 procedure LoadLocaleWrapper(path: pchar; userpath: pchar; filename: pchar); cdecl; export;
   287 procedure LoadLocaleWrapper(path: pchar; userpath: pchar; filename: pchar); cdecl; export;
   178 begin
   288 begin