1 module rpui.resources.strings;
2 
3 import std.path;
4 import std.typecons;
5 import std.algorithm: canFind;
6 import std.ascii;
7 import std.conv;
8 
9 import rpui.primitives;
10 import rpui.paths;
11 
12 import rpdl.tree;
13 
14 /**
15  * This class uses RPDL as strings repository, strings needed for
16  * internationalization.
17  */
18 final class StringsRes {
19     private RpdlTree strings = null;
20 
21     this() {
22     }
23 
24     void setLocale(in string locale) {
25         const paths = createPathes();
26         const string path = buildPath(paths.resources, "strings", locale);
27         this.strings = new RpdlTree(path);
28     }
29 
30     void addStrings(in string fileName) {
31         if (strings is null)
32             return;
33 
34         strings.load(fileName);
35     }
36 
37     utf32string parseString(in utf32string source) {
38         if (strings is null)
39             return source;
40 
41         import std.stdio;
42         writeln(strings);
43         utf32string result = "";
44 
45         for (size_t i = 0; i < source.length; ++i) {
46             const utf32char ch = source[i];
47 
48             if (ch == '@') {
49                 auto reference = parseReference(source, i);
50                 result ~= reference.value;
51                 i = reference.endPosition;
52             } else {
53                 result ~= ch;
54             }
55         }
56 
57         return result;
58     }
59 
60     private struct Reference {
61         dstring value;
62         size_t endPosition;
63     }
64 
65     private Reference parseReference(in utf32string source, in size_t position) {
66         utf32string reference = "";
67         size_t endPosition = position + 1;
68         const referenceAlphabet = letters ~ digits ~ ".";
69 
70         for (size_t i = position + 1; i < source.length; ++i) {
71             const utf32char ch = source[i];
72 
73             if (!referenceAlphabet.canFind(ch))
74                 break;
75 
76             reference ~= ch;
77             endPosition = i;
78         }
79 
80         const value = strings.data.optUTF32String(to!string(reference) ~ ".0", reference);
81         return Reference(value, endPosition);
82     }
83 }