using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using Newtonsoft.Json; using SteamWare; namespace CMS_SC.WebUserControls { public partial class mod_textTrans : SteamWare.UserControl { protected Dictionary flagList { get { Dictionary answ = new Dictionary(); // se trovo da cache restituisco... if (memLayer.ML.isInCacheObject("FlagList.map")) { if (memLayer.ML.cacheOnRedis) { answ = JsonConvert.DeserializeObject>(memLayer.ML.objCacheObj("FlagList.map").ToString()); } else { answ = (Dictionary)memLayer.ML.objCacheObj("FlagList.map"); } } // altrimenti rileggo.... else { answ = getDictFromFile("FlagList.map"); // salvo serializzando se necessario if (memLayer.ML.cacheOnRedis) { string serVal = JsonConvert.SerializeObject(answ); memLayer.ML.setCacheVal("FlagList.map", serVal, true); } else { memLayer.ML.setCacheVal("FlagList.map", answ, true); } } return answ; } } /// /// mostra tutte le lingue o SOLO il lang corrente /// public bool showAllLang { get; set; } protected void Page_Load(object sender, EventArgs e) { } /// /// Lettura file di conf e ritorna dictionary /// /// File con le conf key-val tipo file.map /// protected Dictionary getDictFromFile(string filePath) { // leggo file... string[] contenuto = File.ReadAllLines(resDir + filePath); Dictionary answ = new Dictionary(); foreach (var line in contenuto) { // se la linea non è commento e non è vuota... if (line.Length > 0 && line[0] != '#') { string[] tokens = line.Split('|'); answ.Add(tokens[0], tokens[1]); } } // ritorno valori... return answ; } /// /// folder Risorse x configurazione (Resources) /// public static string resDir { get { return string.Format(@"{0}\Resources\", HttpContext.Current.Server.MapPath("~")); } } /// /// Versione compelta del testo /// public string fullText { get { return hfFullText.Value; } set { hfFullText.Value = value; } } public string tagOpen { get; set; } public string tagClose { get; set; } /// /// Effettua una traduzione del testo da originale a test con FLAG + a capo ove necessario /// /// /// public string tradTesto(string fullData) { fullData = fullData.Replace("\n", ""); int idx = 1; int endPos = 0; int strLenght = fullData.Length; string Lang = ""; string Translation = ""; string currOpen, currClose; string outStr = ""; // proseguo estraendo a blocchi... while (strLenght > 0) { // verifico formato inizia per iniziale del TAG if (fullData[0] == tagOpen[0]) { Lang = fullData.Substring(1, 2); } // compongo i 2 tag di apertura/chiusura... currOpen = tagOpen.Replace("IT", Lang); currClose = tagClose.Replace("IT", Lang); // calcolo fine tag... endPos = fullData.IndexOf(currClose); try { // recupero la stringa effettiva Translation = fullData.Substring(0, endPos).Replace(currOpen, "").Replace(currClose, ""); // tolgo tag+stringa da full e calcolo lunghezza, poi procedo fullData = fullData.Substring(endPos).Replace(currClose, ""); strLenght = fullData.Length; if (showAllLang || Lang == user_std.UtSn.lingua) { // accodo stringa... SE è abilitato ALL oppure se è lingua corrente... outStr += string.Format(@" {1}
", flagList[Lang], Translation); } idx++; } catch (Exception exc) { logger.lg.scriviLog(exc.ToString(), tipoLog.EXCEPTION); strLenght = 0; } } return outStr; } } }