hedgewars/uLocale.pas
changeset 10009 88929358d2e1
parent 9998 736015b847e3
child 10015 4feced261c68
equal deleted inserted replaced
10008:b8b91c7e0da8 10009:88929358d2e1
    24 
    24 
    25 const MAX_EVENT_STRINGS = 100;
    25 const MAX_EVENT_STRINGS = 100;
    26 
    26 
    27 procedure LoadLocale(FileName: shortstring);
    27 procedure LoadLocale(FileName: shortstring);
    28 function  Format(fmt: shortstring; var arg: shortstring): shortstring;
    28 function  Format(fmt: shortstring; var arg: shortstring): shortstring;
    29 function  FormatA(fmt: ansistring; var arg: ansistring): ansistring;
    29 function  FormatA(fmt: PChar; arg: ansistring): ansistring;
    30 function  GetEventString(e: TEventId): ansistring;
    30 function  GetEventString(e: TEventId): PChar;
       
    31 procedure initModule;
       
    32 procedure freeModule;
    31 
    33 
    32 {$IFDEF HWLIBRARY}
    34 {$IFDEF HWLIBRARY}
    33 procedure LoadLocaleWrapper(str: pchar); cdecl; export;
    35 procedure LoadLocaleWrapper(str: pchar); cdecl; export;
    34 {$ENDIF}
    36 {$ENDIF}
    35 
    37 
    36 implementation
    38 implementation
    37 uses uRandom, uUtils, uVariables, uDebug, uPhysFSLayer;
    39 uses uRandom, uUtils, uVariables, uDebug, uPhysFSLayer, sysutils;
    38 
    40 
    39 var trevt: array[TEventId] of array [0..Pred(MAX_EVENT_STRINGS)] of ansistring;
    41 var trevt: array[TEventId] of array [0..Pred(MAX_EVENT_STRINGS)] of PChar;
    40     trevt_n: array[TEventId] of integer;
    42     trevt_n: array[TEventId] of integer;
    41 
    43 
    42 procedure LoadLocale(FileName: shortstring);
    44 procedure LoadLocale(FileName: shortstring);
    43 var s: ansistring = '';
    45 var s, sc: PChar;
    44     f: pfsFile;
    46     f: pfsFile;
    45     a, b, c: LongInt;
    47     a, b, c: LongInt;
    46     first: array[TEventId] of boolean;
    48     first: array[TEventId] of boolean;
    47     e: TEventId;
    49     e: TEventId;
    48 begin
    50 begin
    55 if f <> nil then
    57 if f <> nil then
    56     begin
    58     begin
    57     while not pfsEof(f) do
    59     while not pfsEof(f) do
    58         begin
    60         begin
    59         pfsReadLnA(f, s);
    61         pfsReadLnA(f, s);
    60         if Length(s) = 0 then
    62         if (Length(s) > 0) and (s[0] >= '0') and (s[0] <= '9') then
    61             continue;
    63             begin
    62         if (s[1] < '0') or (s[1] > '9') then
    64             TryDo(Length(s) > 6, 'Load locale: empty string', true);
    63             continue;
    65             val(s[0]+s[1], a, c);
    64         TryDo(Length(s) > 6, 'Load locale: empty string', true);
    66             TryDo(c = 0, 'Load locale: numbers should be two-digit: ' + s, true);
    65         val(s[1]+s[2], a, c);
    67             TryDo(s[2] = ':', 'Load locale: ":" expected', true);
    66         TryDo(c = 0, 'Load locale: numbers should be two-digit: ' + s, true);
    68             val(s[3]+s[4], b, c);
    67         TryDo(s[3] = ':', 'Load locale: ":" expected', true);
    69             TryDo(c = 0, 'Load locale: numbers should be two-digit' + s, true);
    68         val(s[4]+s[5], b, c);
    70             TryDo(s[5] = '=', 'Load locale: "=" expected', true);
    69         TryDo(c = 0, 'Load locale: numbers should be two-digit' + s, true);
    71             sc:= StrAlloc(length(s) - 5);
    70         TryDo(s[6] = '=', 'Load locale: "=" expected', true);
    72             StrCopy(sc, @s[6]);
    71         Delete(s, 1, 6);
    73             case a of
    72         case a of
    74                 0: if (b >=0) and (b <= ord(High(TAmmoStrId))) then
    73             0: if (b >=0) and (b <= ord(High(TAmmoStrId))) then
    75                     trammo[TAmmoStrId(b)]:= sc;
    74                 trammo[TAmmoStrId(b)]:= s;
    76                 1: if (b >=0) and (b <= ord(High(TMsgStrId))) then
    75             1: if (b >=0) and (b <= ord(High(TMsgStrId))) then
    77                     trmsg[TMsgStrId(b)]:= sc;
    76                 trmsg[TMsgStrId(b)]:= s;
    78                 2: if (b >=0) and (b <= ord(High(TEventId))) then
    77             2: if (b >=0) and (b <= ord(High(TEventId))) then
       
    78                 begin
       
    79                 TryDo(trevt_n[TEventId(b)] < MAX_EVENT_STRINGS, 'Too many event strings in ' + IntToStr(a) + ':' + IntToStr(b), false);
       
    80                 if first[TEventId(b)] then
       
    81                     begin
    79                     begin
    82                     trevt_n[TEventId(b)]:= 0;
    80                     TryDo(trevt_n[TEventId(b)] < MAX_EVENT_STRINGS, 'Too many event strings in ' + IntToStr(a) + ':' + IntToStr(b), false);
    83                     first[TEventId(b)]:= false;
    81                     if first[TEventId(b)] then
       
    82                         begin
       
    83                         trevt_n[TEventId(b)]:= 0;
       
    84                         first[TEventId(b)]:= false;
       
    85                         end;
       
    86                     trevt[TEventId(b)][trevt_n[TEventId(b)]]:= sc;
       
    87                     inc(trevt_n[TEventId(b)]);
    84                     end;
    88                     end;
    85                 trevt[TEventId(b)][trevt_n[TEventId(b)]]:= s;
    89                 3: if (b >=0) and (b <= ord(High(TAmmoStrId))) then
    86                 inc(trevt_n[TEventId(b)]);
    90                     trammoc[TAmmoStrId(b)]:= sc;
    87                 end;
    91                 4: if (b >=0) and (b <= ord(High(TAmmoStrId))) then
    88             3: if (b >=0) and (b <= ord(High(TAmmoStrId))) then
    92                     trammod[TAmmoStrId(b)]:= sc;
    89                 trammoc[TAmmoStrId(b)]:= s;
    93                 5: if (b >=0) and (b <= ord(High(TGoalStrId))) then
    90             4: if (b >=0) and (b <= ord(High(TAmmoStrId))) then
    94                     trgoal[TGoalStrId(b)]:= sc;
    91                 trammod[TAmmoStrId(b)]:= s;
    95             end;
    92             5: if (b >=0) and (b <= ord(High(TGoalStrId))) then
    96             end;
    93                 trgoal[TGoalStrId(b)]:= s;
    97         StrDispose(s);
    94            end;
    98         end;
    95        end;
       
    96    pfsClose(f);
    99    pfsClose(f);
    97    end;
   100    end;
    98 end;
   101 end;
    99 
   102 
   100 function GetEventString(e: TEventId): ansistring;
   103 function GetEventString(e: TEventId): PChar;
   101 begin
   104 begin
   102     if trevt_n[e] = 0 then // no messages for this event type?
   105     if trevt_n[e] = 0 then // no messages for this event type?
   103         GetEventString:= '*missing translation*'
   106         GetEventString:= '*missing translation*'
   104     else
   107     else
   105         GetEventString:= trevt[e][GetRandom(trevt_n[e])]; // Pick a random message and return it
   108         GetEventString:= trevt[e][GetRandom(trevt_n[e])]; // Pick a random message and return it
   113     Format:= fmt
   116     Format:= fmt
   114 else
   117 else
   115     Format:= copy(fmt, 1, i - 1) + arg + Format(copy(fmt, i + 2, Length(fmt) - i - 1), arg)
   118     Format:= copy(fmt, 1, i - 1) + arg + Format(copy(fmt, i + 2, Length(fmt) - i - 1), arg)
   116 end;
   119 end;
   117 
   120 
   118 function FormatA(fmt: ansistring; var arg: ansistring): ansistring;
   121 function FormatA(fmt: PChar; arg: ansistring): ansistring;
   119 var i: LongInt;
   122 var i: LongInt;
       
   123     s: ansistring;
   120 begin
   124 begin
   121 i:= Pos('%1', fmt);
   125 s:= fmt;
       
   126 
       
   127 i:= Pos('%1', s);
   122 if i = 0 then
   128 if i = 0 then
   123     FormatA:= fmt
   129     FormatA:= s
   124 else
   130 else
   125     FormatA:= copy(fmt, 1, i - 1) + arg + FormatA(copy(fmt, i + 2, Length(fmt) - i - 1), arg)
   131     FormatA:= copy(s, 1, i - 1) + arg + FormatA(PChar(copy(s, i + 2, Length(s) - i - 1)), arg)
   126 end;
   132 end;
   127 
   133 
   128 {$IFDEF HWLIBRARY}
   134 {$IFDEF HWLIBRARY}
   129 procedure LoadLocaleWrapper(str: pchar); cdecl; export;
   135 procedure LoadLocaleWrapper(str: pchar); cdecl; export;
   130 begin
   136 begin
   131     LoadLocale(Strpas(str));
   137     LoadLocale(Strpas(str));
   132 end;
   138 end;
   133 {$ENDIF}
   139 {$ENDIF}
   134 
   140 
       
   141 procedure initModule;
       
   142 var e: TEventId;
       
   143     i: LongInt;
       
   144 begin
       
   145     for e:= Low(TEventId) to High(TEventId) do
       
   146         for i:= 0 to Pred(MAX_EVENT_STRINGS) do
       
   147             trevt[e][i]:= nil;
       
   148 end;
       
   149 
       
   150 procedure freeModule;
       
   151 var e: TEventId;
       
   152     i: LongInt;
       
   153 begin
       
   154     for e:= Low(TEventId) to High(TEventId) do
       
   155         for i:= 0 to Pred(trevt_n[e]) do
       
   156             StrDispose(trevt[e][i]);
       
   157 end;
       
   158 
   135 end.
   159 end.