Skip to content

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
to_delphi_i18n
/**
* Usage: to_delphi_i18n.exe lang_json1 [lang_json2...]
* Scans 'lang_json1' JSON-file then [lang_json2...] and adds it's data to generated
* 'my_i18n.pas'.
*
* JSON-file example:
---
{
"message1": "Message1 Text",
"message2": "Message2 Text"
}
---
* usage example(Delphi):
---
uses my_i18n;
...
SetLanguage(0);
some_string := i18n.message1;
---
*
*/
import std.stdio, std.file, std.conv;
string to_delphi_string(string data)
{
string s = "'";
foreach (ubyte c; cast(ubyte[])(data))
{ if (c < 32 || c > 127 || c == '\'')
s ~= "'#" ~ to!string(c) ~ "'";
else
s ~= c;
}
return s ~ "'";
}
int main(string[] args)
{
if (args.length < 2)
{ writeln("usage to_delphi_i18n.exe file1 [file2, ...]");
return 1;
}
auto files = args[1..$];
foreach (file_; files)
if (!exists(file_))
{ writefln("File '%s' doesn't exits", file_);
return 1;
}
string[string][] data;
//Part 1.
foreach (file_; files)
{ string[string] hashMap;
//JSON parse file
import std.json;
auto str = cast(string)std.file.read(file_);
auto js = parseJSON(str);
if (js.type != JSON_TYPE.OBJECT)
{ writefln("File '%s': expected JSON OBJECT", file_);
return 1;
}
foreach (key, value; js.object)
{ if (value.type == JSON_TYPE.STRING)
hashMap[key] = value.str;
else
{ writefln("JSON expected string value for key '%s' in file '%s'", key, file_);
return 1;
}
}
data ~= hashMap;
}
//add key/value pairs for other languages
foreach (i, aa; data)
{ foreach (key; aa.byKey)
{ foreach (j, ab; data)
{ if (i == j) continue;
if ((key in ab) is null)
ab[key] = aa[key];
}
}
}
//save to generated file
auto f = File("my_i18n.pas", "w+");
string s = "unit my_i18n;\ninterface\ntype\n"
~ " Tmy_i18n_BEFCFE32_A9A0_426D_968A_51E11788BF2C = record\n";
foreach (key; data[0].byKey)
s ~= " " ~ key ~ ": string;\n";
s ~= " end;\nvar\n i18n: Tmy_i18n_BEFCFE32_A9A0_426D_968A_51E11788BF2C;\n"
~ "procedure SetLanguage(lang: Integer);\n\nimplementation\n\n"
~ "procedure SetLanguage(lang: Integer);\nbegin\n";
foreach (i; 0..files.length)
{ s ~= " if lang = " ~ to!string(i) ~ " then\n with i18n do\n begin\n";
foreach (key, value; data[i])
s ~= " " ~ key ~ " := " ~ to_delphi_string(value) ~ ";\n";
s ~= " end;\n";
}
s ~= "end;\n\nend.\n";
f.write(s);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.