Files
SSC/CMS_SC/WebUserControls/mod_textTrans.ascx.cs
T
2018-02-23 16:24:18 +01:00

118 lines
3.3 KiB
C#

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 SteamWare;
namespace CMS_SC.WebUserControls
{
public partial class mod_textTrans : SteamWare.UserControl
{
protected Dictionary<string, string> flagList;
protected void Page_Load(object sender, EventArgs e)
{
flagList = getDictFromFile("FlagList.map");
}
/// <summary>
/// Lettura file di conf e ritorna dictionary
/// </summary>
/// <param name="filePath">File con le conf key-val tipo file.map</param>
/// <returns></returns>
protected Dictionary<string, string> getDictFromFile(string filePath)
{
// leggo file...
string[] contenuto = File.ReadAllLines(resDir + filePath);
Dictionary<string, string> answ = new Dictionary<string, string>();
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;
}
/// <summary>
/// folder Risorse x configurazione (Resources)
/// </summary>
public static string resDir
{
get
{
return string.Format(@"{0}\App_Data\", HttpContext.Current.Server.MapPath("~"));
}
}
/// <summary>
/// Versione compelta del testo
/// </summary>
public string fullText
{
get
{
return hfFullText.Value;
}
set
{
hfFullText.Value = value;
}
}
public string tagOpen { get; set; }
public string tagClose { get; set; }
/// <summary>
/// Effettua una traduzione del testo da originale a test con FLAG + a capo ove necessario
/// </summary>
/// <param name="fullData"></param>
/// <returns></returns>
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;
// accodo stringa...
outStr += string.Format(@"<img src='Images/blank.gif' class='flag {0}'/> {1}</br>", flagList[Lang], Translation);
idx++;
}
catch
{
strLenght = 0;
}
}
return outStr;
}
}
}