221V 1 неделя назад
Родитель
Сommit
0c26746f85
4 измененных файлов с 130 добавлено и 0 удалено
  1. 12 0
      vtest/source/app.d
  2. 88 0
      vtest/source/tr.d
  3. 15 0
      vtest/source/tr_en.d
  4. 15 0
      vtest/source/tr_uk.d

+ 12 - 0
vtest/source/app.d

@@ -19,6 +19,8 @@ import memcached4d;
 
 import std.conv : to;
 
+import tr;
+
 import mustache;
 alias MustacheEngine!(string) Mustache;
 
@@ -163,6 +165,16 @@ void test(HTTPServerRequest req, HTTPServerResponse res){
   
   
   
+  Language Lang = Language.uk;
+  writeln("tr 1 = ", Tr(Lang, TKey.hello));
+  writeln("tr 2 = ", Tr(Lang, TKey.welcome, ["username"], 0) );
+  writeln("tr 3 = ", Tr(Lang, TKey.apples, [], 1) );
+  writeln("tr 3 = ", Tr(Lang, TKey.apples, [], 2) ) ;
+  writeln("tr 3 = ", Tr(Lang, TKey.apples, [], 5) );
+  writeln("tr 4 = ", Tr(Lang, TKey.apples_n_oranges, ["6", "7"], 0) );
+  
+  
+  
   writeln("get test1 = ", cache.get!string("test1"));
   
   string v1 = "value1 = 🔥🦀";

+ 88 - 0
vtest/source/tr.d

@@ -0,0 +1,88 @@
+
+
+import tr_en;
+import tr_uk;
+
+import std.array : empty;
+import std.conv : to;
+import std.format;
+
+enum TKey{
+  hello,
+  welcome,
+  apples,
+  apples_n_oranges,
+}
+
+struct Translation{
+  string[] text;
+  int arg_count;
+}
+
+enum Language{
+  en,
+  uk
+}
+
+
+string plural(int count, string[] txt){
+  int n1 = count % 100; // get rem
+  if(n1 > 4 && n1 < 20){
+    return txt[2];
+  }else{
+    switch(count % 10){ // get rem
+      case 0: return txt[2];
+        case 1: return txt[0];
+        case 2, 3, 4: return txt[1];
+        default: return txt[2];
+    }
+  }
+}
+
+// plural(1, ["яблуко", "яблука", "яблук"]) => "яблуко"
+// plural(2, ["яблуко", "яблука", "яблук"]) => "яблука"
+// plural(5, ["яблуко", "яблука", "яблук"]) => "яблук"
+
+
+string format_args(string txt, string[] args){
+  switch (args.length){
+    case 0: return txt;
+    case 1: return format(txt, args[0]);
+    case 2: return format(txt, args[0], args[1]);
+    case 3: return format(txt, args[0], args[1], args[2]);
+    case 4: return format(txt, args[0], args[1], args[2], args[3]);
+    default: return txt;
+  }
+}
+
+
+string Tr(Language lang, TKey text_key, string[] args = [], int plural_num = 0){
+  auto key = cast(int)text_key;
+  Translation translation;
+  switch(lang){
+    case Language.uk:
+      translation = tr_uk.translations[key];
+      break;
+    //case Language.en:
+    //  translation = tr_en.translations[key];
+    //  break;
+    default :
+      translation = tr_en.translations[key];
+  }
+  
+  if( (plural_num > 0) && (args.length < translation.arg_count) ){
+    args = [ to!string(plural_num) ] ~ args;
+  }
+  
+  if(args.length != translation.arg_count){
+    return translation.text[0];
+  }
+  
+  if( (translation.text.length > 1) && (plural_num > 0) ){
+    auto text = plural(plural_num, translation.text);
+    return format_args(text, args);
+  }else{
+    return (args.empty) ? translation.text[0] : format_args(translation.text[0], args);
+  }
+}
+

+ 15 - 0
vtest/source/tr_en.d

@@ -0,0 +1,15 @@
+
+
+import tr : TKey, Translation;
+
+alias t = Translation;
+
+
+Translation[] translations = [
+  t(["Hello"], 0),
+  t(["Welcome, %s!"], 1),
+  //t(["%d apple", "%d apples", "%d apples"], 1),
+  t(["%s apple", "%s apples", "%s apples"], 1),
+  t(["%s apples and %s oranges"], 2)
+];
+

+ 15 - 0
vtest/source/tr_uk.d

@@ -0,0 +1,15 @@
+
+
+import tr : TKey, Translation;
+
+alias t = Translation;
+
+
+Translation[] translations = [
+  t(["Привіт"], 0),
+  t(["Ласкаво просимо, %s!"], 1),
+  //t(["%d яблуко", "%d яблука", "%d яблук"], 1),
+  t(["%s яблуко", "%s яблука", "%s яблук"], 1),
+  t(["%s яблук і %s апельсинів"], 2)
+];
+