1 2 /++ 3 + Authors: Stephan Dilly, lastname dot firstname at gmail dot com 4 + Copyright: MIT 5 +/ 6 module ask.locale; 7 8 /// 9 interface ITextManager 10 { 11 /++ 12 + returns the localized text string depending on the loaded locale database 13 + 14 + params: 15 + _key = loca lookup key 16 + 17 + see_also: 18 + `this`, `AlexaText`, `LocaParser` 19 +/ 20 string getText(int _key) const pure nothrow; 21 } 22 23 /// 24 struct AlexaText 25 { 26 /// 27 int key; 28 /// 29 string text; 30 } 31 32 /// template to parse csv of loca entries 33 string LocaParser(E,string input)() 34 { 35 import std.string:splitLines,strip; 36 import std.algorithm:startsWith; 37 import std.format:format; 38 import std.array:split; 39 40 enum string[] lines = input.splitLines; 41 42 string res = "["; 43 44 allMembers: 45 foreach(enumMember; __traits(allMembers, E)) 46 { 47 foreach(line; lines) 48 { 49 line = line.strip; 50 51 if(line.startsWith(enumMember)) 52 { 53 auto lineArgs = line.split(","); 54 auto locaKey = lineArgs[0]; 55 auto locaText = line[locaKey.length+1..$].strip; 56 auto entry = format("AlexaText(%s.%s, \"%s\"),", E.stringof,enumMember,locaText); 57 res ~= entry ~ "\n"; 58 continue allMembers; 59 } 60 } 61 } 62 63 return res ~ "]"; 64 } 65 66 /// 67 unittest 68 { 69 enum TextIds 70 { 71 key1,key2 72 } 73 74 enum testCsv = "key2, foo\n key1 , bar "; 75 76 enum AlexaText[] AlexaText_test = mixin(LocaParser!(TextIds,testCsv)); 77 78 static assert(AlexaText_test[0].text == "bar"); 79 static assert(AlexaText_test[1].text == "foo"); 80 81 static assert(AlexaText_test[0].key == TextIds.key1); 82 static assert(AlexaText_test[1].key == TextIds.key2); 83 }