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