EduNetworkBuilder/EduNetworkBuilder/LanguageString.cs

193 lines
5.6 KiB
C#
Raw Normal View History

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
{
2018-03-15 23:12:20 +01:00
[Serializable]
public class LanguageString
{
public string text="";
public string language="en";
public string tag_type;
private LanguageString() { }
public LanguageString(string lang, string msg, string tag)
{
text = msg;
language = lang;
tag_type = tag;
}
public LanguageString Clone()
{
LanguageString newitem = new LanguageString();
newitem.text = text;
newitem.language = language;
newitem.tag_type = tag_type;
return newitem;
}
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;
}
2018-03-16 19:59:14 +01:00
public bool Equals(LanguageString compareWith)
{
if (text != compareWith.text) return false;
if (tag_type != compareWith.tag_type) return false;
if (language != compareWith.language) return false;
return true;
}
public void Save(XmlWriter writer)
{
writer.WriteElementString(language+"_" + tag_type, text);
}
}
2018-03-15 23:12:20 +01:00
[Serializable]
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;
}
private LanguageStrings() { }
public LanguageStrings(LanguageStrings ToCopy)
{
LanguageStrings.Clone(ToCopy, this);
}
public static void Clone(LanguageStrings source, LanguageStrings dest)
{
dest.tag = source.tag;
dest.TheStrings.Clear();
foreach(LanguageString ls in source.TheStrings)
{
dest.TheStrings.Add(ls.Clone());
}
}
public LanguageStrings Clone()
{
LanguageStrings newitem = new LanguageStrings();
LanguageStrings.Clone(this, newitem);
return newitem;
}
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)
{
NBSettings oursettings = NB.GetSettings();
LanguageString oneString = new LanguageString(oursettings.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 "";
}
2017-10-03 22:57:12 +02:00
public void ChangeText(string language, string NewText)
{
bool done = false;
//Find the matching language
foreach (LanguageString LS in TheStrings)
{
if (LS.language == language)
{
LS.text = NewText;
done = true;
}
}
if(!done)
{
LanguageString oneString = new LanguageString(language, NewText, tag);
TheStrings.Add(oneString);
}
}
public string GetText()
{
NBSettings oursettings = NB.GetSettings();
return GetText(oursettings.ChosenLanguage);
}
}
}