SpectacleTransformer/Language.cs

51 lines
2.1 KiB
C#

using System;
namespace SpectacleTransformer {
public class Language : IEquatable<Language> {
public readonly string DisplayName;
public readonly string SourceName;
public readonly string TargetName;
public Language(string DisplayName, string SourceName = null, string TargetName = null) {
if (SourceName == null && TargetName == null) {
throw new ArgumentNullException("TargetName", "SourceName and TargetName cannot be null simultaneously.");
}
this.DisplayName = DisplayName;
this.SourceName = SourceName;
this.TargetName = TargetName;
}
public Language(string DisplayName, string SourceName)
: this(DisplayName, SourceName, null) {
}
public override string ToString() {
return this.DisplayName;
}
public bool Equals(Language other) {
return this.SourceName == other.SourceName && this.TargetName == other.TargetName;
}
public static Language[] Load() {
string[] codes = Properties.Resources.LanguageCodes.Split(new char[] {','});
Language[] langs = new Language[codes.Length + 1];
// Special case for the auto-detect language, not included in the list
langs[0] = new Language(Properties.Resources.lang_auto, "auto");
for (int i = 0; i < codes.Length; i++) {
langs[i + 1] = new Language(
Properties.Resources.ResourceManager.GetString(String.Format("lang_{0}", codes[i])),
codes[i],
codes[i]);
}
Array.Sort(langs, delegate(Language a, Language b) {
// Ensure the auto language is at the first position
if (a.SourceName == "auto") return b.SourceName == "auto" ? 0 : -1;
if (b.SourceName == "auto") return a.SourceName == "auto" ? 0 : -1;
return a.DisplayName.CompareTo(b.DisplayName);
});
return langs;
}
}
}