1 /*
2     Copyright © 2020, Inochi2D Project
3     Distributed under the 2-Clause BSD License, see LICENSE file.
4     
5     Authors: Luna Nielsen
6 */
7 module creator.i18n;
8 
9 // module i18n;
10 // import std.exception;
11 
12 // // Translation ID
13 // private struct transidx {
14 //     string file;
15 //     int line;
16 // }
17 
18 // // The language file serialization construct
19 // private struct LangFile {
20 //     string name;
21 //     string[string] table;
22 // }
23 
24 // // List of languages
25 // private string[string] langList;
26 
27 // // The translation table
28 // __gshared private string[transidx] transtable;
29 
30 // private void genLangList() {
31 //     import std.path;
32 //     import std.file : dirEntries, SpanMode, isDir, exists, mkdir, DirEntry, readText;
33 //     import asdf : deserialize;
34 
35 //     // Create an empty language directory if none exists already.
36 //     if (!exists("lang/")) mkdir("lang/");
37 
38 //     // Go through every language and add them to the list
39 //     foreach(DirEntry trFile; dirEntries(buildPath("lang"), SpanMode.shallow, true)) {
40         
41 //         // Skip any stray directories
42 //         if (trFile.isDir) continue;
43 
44 //         Json jsonObject = parseJsonString(readText(trFile.name));
45 //         string langName = jsonObject["name"].get!string;
46 
47 //         langList[langName] = trFile;
48 //     }
49 // }
50 
51 // /**
52 //     Gets a list of the languages currently available
53 // */
54 // string[] getLanguages() {
55 //     string[] langs;
56 //     foreach(name, _; langList) {
57 //         langs ~= name;
58 //     }
59 //     return langs;
60 // }
61 
62 // /**
63 //     Sets the current language
64 // */
65 // void language(string language) {
66 //     import vibe.data.json : deserializeJson;
67 //     import std.file : readText, exists;
68 //     import std.path : buildPath, setExtension;
69 //     import std.string : split;
70 //     import std.conv : to;
71 
72 //     enforce(language in langList, "Language not found");
73 
74 //     // Read and parse the json
75 //     string json = langList[language].readText();
76 //     LangFile table = deserializeJson!LangFile(json);
77     
78 //     // Iterate over all the keys and build the LangFile object from it
79 //     foreach(idx, str; table.table) {
80 //         string[] components = idx.split(":");
81 
82 //         // There has to be 2 components seperated by :
83 //         enforce(components.length == 2, "Invalid translation index \""~idx~"\"");
84 
85 //         // Set the translation for the translation index parsed from the components
86 //         // NOTE: File is component 0, Line is component 1
87 //         transtable[transidx(components[0], components[1].to!int)] = str;
88 //     }
89 
90 //     import std.stdio : writefln;
91 //     debug writefln("Loaded language %s...", table.name);
92 // }
93 
94 // /**
95 //     Returns a translated string, defaults to the given text if no translation was found
96 // */
97 // string _(string file=__FILE__, int line = __LINE__)(string text) {
98 
99 //     // No translation was loaded
100 //     if (transtable.length == 0) return text;
101 
102 //     transidx idx = transidx(file, line);
103 
104 //     // Line was not found in translation
105 //     if (idx !in transtable) return text;
106 
107 //     // Line was found, get translation
108 //     return transtable[idx];
109 // }
110 
111 // shared static this() {
112 //     genLangList();
113 //     language("English");
114 // }