unit Keyman.System.BuildLCIDToBCP47Registry;

interface

type
  TBuildLCIDToBCP47Registry = class
    class procedure Build(const MapFile, DestinationFile: string);
  end;

implementation

uses
  System.Classes,
  System.SysUtils;

{ TBuildWindowsToBCP47Registry }

class procedure TBuildLCIDToBCP47Registry.Build(const MapFile,
  DestinationFile: string);
var
  FResult: TStringList;
  i: Integer;
  s: string;
  n: Integer;
  code: string;
begin
  with TStringList.Create do
  try
    LoadFromFile(MapFile, TEncoding.UTF8);

    FResult := TStringList.Create;
    try
      FResult.Add('unit Keyman.System.Standards.LCIDToBCP47Registry;');
      FResult.Add('');
      FResult.Add('interface');
      FResult.Add('');
      FResult.Add('// File-Date: '+FormatDateTime('yyyy-mm-dd hh:nn:ss', Now));
      FResult.Add('// Extracted from LCIDToLocaleName');
      FResult.Add('// Generated by build_standards_data');
      FResult.Add('');
      FResult.Add('uses');
      FResult.Add('  System.Generics.Collections;');
      FResult.Add('');
      FResult.Add('type');
      FResult.Add('  TLCIDToBCP47Map = class');
      FResult.Add('  public');
      FResult.Add('    class procedure Fill(dict: TDictionary<Integer,string>);');
      FResult.Add('  end;');
      FResult.Add('');
      FResult.Add('implementation');
      FResult.Add('');
      FResult.Add('{ TLCIDToBCP47Map }');
      FResult.Add('');
      FResult.Add('class procedure TLCIDToBCP47Map.Fill(dict: TDictionary<Integer, string>);');
      FResult.Add('begin');

      for i := 0 to Count - 1 do
      begin
        s := Trim(Strings[i]);
        if s = '' then
          Continue;
        n := Pos(',', s);
        code := Copy(s, 1, n-1);
        System.Delete(s, 1, n);
        FResult.Add('  dict.Add($'+code+','''+s+''');');
      end;

      FResult.Add('end;');
      FResult.Add('');
      FResult.Add('end.');

      FResult.SaveToFile(DestinationFile, TEncoding.UTF8);
    finally
      FResult.Free;
    end;
  finally
    Free;
  end;
end;

end.
