using System;
using System.IO;
using System.Collections;
namespace TeamXBMC.TranslatorCore
{
///
/// Object to communicate with the gui.
///
public sealed class TranslationManager
{
private TranslatorArray strings = new TranslatorArray();
private static TranslationManager instance=null;
private TranslationManager()
{
}
///
/// Loads the current language
///
public void Initialize()
{
strings.Clear();
strings.Load();
}
#region Language file processing
///
/// Create a new language
///
public void CreateLanguage(string name)
{
if (File.Exists(Settings.Instance.LanguageFolder+@"\"+name+@"\strings.xml"))
throw new TranslatorException("The language \""+name+"\" already exists.");
try
{
Directory.CreateDirectory(Settings.Instance.LanguageFolder+@"\"+name);
}
catch (Exception e)
{
throw new TranslatorException("Unable to create the directory "+Settings.Instance.LanguageFolder+@"\"+name, e);
}
// Save an empty strings.xml file
StringArray stringsNew=new StringArray();
stringsNew.Save(Settings.Instance.LanguageFolder+@"\"+name+@"\strings.xml");
LanguageInfo langinfo=new LanguageInfo();
langinfo.Save(Settings.Instance.LanguageFolder+@"\"+name+@"\langinfo.xml");
}
///
/// Save the current language
///
public void SaveTranslated()
{
strings.Save();
}
#endregion
#region Properties
///
/// Gets the instance of the TranslationManager
///
public static TranslationManager Instance
{
get
{
if (instance==null)
{
instance=new TranslationManager();
}
return instance;
}
}
///
/// Get the strings of the current language with the state translated
///
public TranslatorArray Translated
{
get
{
strings.Enumerator=TranslatorArrayEnumerator.Translated;
return strings;
}
}
///
/// Get the strings of the current language with the state untranslated
///
public TranslatorArray Untranslated
{
get
{
strings.Enumerator=TranslatorArrayEnumerator.Untranslated;
return strings;
}
}
///
/// Get the strings of the current language with the state changed
///
public TranslatorArray Changed
{
get
{
strings.Enumerator=TranslatorArrayEnumerator.Changed;
return strings;
}
}
///
/// Get all strings of the current language
///
public TranslatorArray All
{
get
{
strings.Enumerator=TranslatorArrayEnumerator.All;
return strings;
}
}
///
/// Get the name of the current language
///
public string LanguageTranslated
{
get { return Settings.Instance.Language; }
}
///
/// Get the name of the master language
///
public string LanguageOriginal
{
get { return Settings.Instance.LanguageOriginal; }
}
///
/// Get all available languages
///
public string[] Languages
{
get
{
string root=Settings.Instance.LanguageFolder;
string[] dirs=Directory.GetDirectories(root);
ArrayList languages=new ArrayList();
foreach (string dir in dirs)
{
if (File.Exists(dir+@"\strings.xml"))
{
// Extract language name from path
string language=dir.Substring(root.Length+1, dir.Length-root.Length-1);
if (!language.Equals(Settings.Instance.LanguageOriginal) )
languages.Add(language);
}
}
return (string[])languages.ToArray(typeof(string));
}
}
///
/// Returns true if the currently edited language file is has been changed by the user
///
public bool IsModified
{
get { return strings.IsModified; }
}
#endregion
}
}