IToolS Blog

IToolS i18Next utility

by itools@albertoschiassi.it on lunedì 9 marzo 2015 16:10
using System;
using System.Collections.Generic;
using System.IO;
 
namespace IToolS.Web.i18Next
{
   using Newtonsoft.Json.Linq;
 
   public class Translator
   {
      String _path;
 
      Dictionary<StringJObject> _dictionary;
 
      private Translator()
      {
         _dictionary = new Dictionary<stringJObject>();
      }
 
      public Translator(String path, String jsonFileName = "translation.json")
         : this()
      {
         _path = path;
 
         foreach (var folder in Directory.GetDirectories(path))
         {
            String directoryName = new DirectoryInfo(folder).Name;
 
            String jsonFile = Path.Combine(path, directoryName, jsonFileName);
 
            if (File.Exists(jsonFile))
            {
               JObject jObject = JObject.Parse(File.ReadAllText(jsonFile));
 
               _dictionary[directoryName] = jObject;
            }
         }
      }
 
      public String Translate(String culture, String text)
      {
         if (String.IsNullOrEmpty(text))
            return text;
 
         if (!_dictionary.ContainsKey(culture))
            return text;
 
         String[] fields = text.Split('.');
 
         JToken jToken = _dictionary[culture];
 
         for (int i = 0; i < fields.Length; i++)
         {
            jToken = jToken[fields[i]];
 
            if (jToken == null)
               return text;
 
            if (i == fields.Length - 1)
               return jToken.ToString();
         }
 
         return text;
      }
   }
}

Blogs Parent Separator IToolS Blog
Author

Tags