EduNetworkBuilder/EduNetworkBuilder/LanguageString.cs

131 lines
3.7 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Text.RegularExpressions;
namespace EduNetworkBuilder
{
public class LanguageString
{
public string text="";
public string language="en";
public string tag_type;
public LanguageString(string lang, string msg, string tag)
{
text = msg;
language = lang;
tag_type = tag;
}
public LanguageString(XmlNode theNode, string tag)
{
XmlNodeType myNodetype = theNode.NodeType;
if (myNodetype == XmlNodeType.Element)
{
if(Regex.IsMatch(theNode.Name.ToLower(),"^[a-z][a-z]_" + tag))
{
//We have a language, and then a message
text = theNode.InnerText;
language = Regex.Replace(theNode.Name.ToLower(), "_" + tag, "");
}
else
{
//It is just a message. Use the default language.
language = "en";
text = theNode.InnerText;
}
}
tag_type = tag;
}
public void Save(XmlWriter writer)
{
writer.WriteElementString(language+"_" + tag_type, text);
}
}
public class LanguageStrings
{
List<LanguageString> TheStrings = new List<LanguageString>();
string tag = "";
/// <summary>
/// Make a language string list of type: "message", "title"
/// </summary>
/// <param name="type">Should be of type "message" or "title"</param>
public LanguageStrings(string type)
{
tag = type;
}
public void Add(XmlNode theNode)
{
LanguageString oneString = new LanguageString(theNode, tag);
//remove duplicates
for (int i = TheStrings.Count - 1; i >= 0; i--)
{
if (TheStrings[i].language == oneString.language)
{
TheStrings.RemoveAt(i);
}
}
TheStrings.Add(oneString);
}
public void Add(string Text)
{
LanguageString oneString = new LanguageString(Properties.Settings.Default.ChosenLanguage,Text,tag);
//remove duplicates
for (int i = TheStrings.Count - 1; i >= 0; i--)
{
if (TheStrings[i].language == oneString.language)
{
TheStrings.RemoveAt(i);
}
}
TheStrings.Add(oneString);
}
public void Save(XmlWriter writer)
{
foreach(LanguageString LS in TheStrings)
{
LS.Save(writer);
}
}
public string GetText(string language)
{
//Find the matching language
foreach(LanguageString LS in TheStrings)
{
if(LS.language == language)
{
return LS.text;
}
}
//Return the english version if one exists
if(language != "en")
{
return GetText("en");
}
//return the first language (if the puzzle is only in french, use that.)
if (TheStrings.Count > 0)
return TheStrings[0].text;
//If there is no language, return a blank message
return "";
}
public string GetText()
{
return GetText(Properties.Settings.Default.ChosenLanguage);
}
}
}