1 module creator.core.i18n;
2 import creator.core;
3 import i18n.culture;
4 import i18n;
5 import std.file;
6 import std.path;
7 import std.string;
8 
9 private {
10     TLEntry[] localeFiles;
11 
12     void incLocaleScan(string path) {
13         foreach(DirEntry entry; dirEntries(path, "*.mo", SpanMode.shallow)) {
14             
15             // Get langcode from filename
16             string langcode = baseName(stripExtension(entry.name));
17 
18             // Skip langcodes we don't know
19             if (!i18nValidateCultureCode(langcode)) continue;
20 
21             // Add locale
22             localeFiles ~= TLEntry(
23                 i18nGetCultureLanguage(langcode),
24                 i18nGetCultureLanguage(langcode).toStringz,
25                 langcode, 
26                 entry.name
27             );
28         }
29     }
30 }
31 
32 /**
33     Entry in the translations table
34 */
35 struct TLEntry {
36 public:
37     string humanName;
38     const(char)* humanNameC;
39     string code;
40     string file;
41 }
42 
43 /**
44     Initialize translations
45 */
46 void incLocaleInit() {
47     incLocaleScan(incGetAppLocalePath());
48     incLocaleScan(getcwd());
49     incLocaleScan(thisExePath().rootName);
50 }
51 
52 /**
53     Gets the current selected locale human name
54 */
55 string incLocaleCurrentName() {
56     string code = incSettingsGet("lang", "en");
57     return i18nGetCultureLanguage(code.length == 0 ? "en" : code);
58 }
59 
60 /**
61     Sets the locale for the application
62 */
63 void incLocaleSet(string code) {
64     incSettingsSet("lang", code);
65 }
66 
67 /**
68     Get locale entry for a code
69 */
70 TLEntry* incLocaleGetEntryFor(string code) {
71     foreach(ref entry; localeFiles) {
72         if (entry.code == code) return &entry;
73     }
74     return null;
75 }
76 
77 /**
78     Returns the locale list
79 */
80 TLEntry[] incLocaleGetEntries() {
81     return localeFiles;
82 }