1 module i18n.tr;
2 import i18n.mo;
3 import i18n.culture;
4 import std.path;
5 import std.file : read;
6 import std.uni : toLower;
7 import std.format : format;
8 
9 package(i18n) {
10     struct TREntry {
11         string source;
12         string[] targets;
13         const(char)*[] cTargets;
14     }
15 }
16 
17 private {
18     enum TLFormats {
19         none,
20         gettext
21     }
22 
23     TLFormats currentFormat = TLFormats.none;
24     TREntry[string] lookuptable;
25 }
26 
27 bool i18nLoadLanguage(string file) {
28     lookuptable.clear();
29 
30     switch(file.extension.toLower) {
31         case ".mo":
32             try {
33                 i18nMOLoad(cast(ubyte[])read(file));
34 
35                 lookuptable = i18nMOGenStrings();
36                 currentFormat = TLFormats.gettext;
37             } catch (Exception ex) {
38                 return false;
39             }
40             return true;
41         default: 
42             throw new Exception("i18n: Invalid file format %s".format(file.extension));
43     }
44 }
45 
46 /**
47     Returns D string translation of iText
48 */
49 string _(string iText) {
50     
51     // If in lookup table, return from lookup table
52     if (iText in lookuptable) 
53         return lookuptable[iText].targets[0];
54 
55     // Otherwise try just in case from file.
56     switch(currentFormat) {
57         case TLFormats.gettext:
58             return i18nMOStr(iText);
59         default:
60             return iText;
61     }
62 }
63 
64 /**
65     Returns C string translation of iText
66 */
67 const(char)* __(string iText) {
68     import std.string : toStringz;
69     
70     // If in lookup table, return from lookup table
71     if (iText in lookuptable) 
72         return lookuptable[iText].cTargets[0];
73 
74     // Otherwise try just in case from file.
75     switch(currentFormat) {
76         case TLFormats.gettext:
77             return i18nMOStrC(iText);
78         default:
79             return iText.toStringz;
80     }
81 }