From: <che...@us...> - 2010-01-24 21:58:35
|
Revision: 3384 http://mp-plugins.svn.sourceforge.net/mp-plugins/?rev=3384&view=rev Author: chef_koch Date: 2010-01-24 21:58:25 +0000 (Sun, 24 Jan 2010) Log Message: ----------- added Translation support Modified Paths: -------------- trunk/plugins/MySleepTimer/MySleepTimer/MySleepTimer.csproj Added Paths: ----------- trunk/plugins/MySleepTimer/MySleepTimer/Language/ trunk/plugins/MySleepTimer/MySleepTimer/Language/de.xml trunk/plugins/MySleepTimer/MySleepTimer/Translation.cs Added: trunk/plugins/MySleepTimer/MySleepTimer/Language/de.xml =================================================================== --- trunk/plugins/MySleepTimer/MySleepTimer/Language/de.xml (rev 0) +++ trunk/plugins/MySleepTimer/MySleepTimer/Language/de.xml 2010-01-24 21:58:25 UTC (rev 3384) @@ -0,0 +1,15 @@ +<?xml version="1.0" encoding="iso-8859-1"?> +<!-- MyAlarm translation file --> +<!-- German (de) --> +<!-- Note: English is the fallback for any strings not found in other languages --> +<strings> + <!-- P --> + <string Field="PluginDescription">Schl\xE4fst auch du beim Fernsehen st\xE4ndig ein? Dann brauchst du dieses Plugin, welches MediaPortal automatisch nach einer bestimmten Zeit ausschaltet.</string> + <!-- S --> + <string Field="SleepingInXMin">Autom. Ausschalten in {0} min.</string> + <string Field="SleepingInXMinPlaytime">Autom. Ausschalten in {0} min. (restl. Wiedergabezeit)</string> + <string Field="SleepTimer">Automatisches Ausschalten</string> + <!-- T --> + <string Field="TimeIsUp">Es ist Schlafenszeit! Gute Nacht.</string> + <string Field="TimerStopped">Sleeptimer ausgeschaltet!</string> +</strings> \ No newline at end of file Modified: trunk/plugins/MySleepTimer/MySleepTimer/MySleepTimer.csproj =================================================================== --- trunk/plugins/MySleepTimer/MySleepTimer/MySleepTimer.csproj 2010-01-24 21:32:46 UTC (rev 3383) +++ trunk/plugins/MySleepTimer/MySleepTimer/MySleepTimer.csproj 2010-01-24 21:58:25 UTC (rev 3384) @@ -69,6 +69,7 @@ </Reference> </ItemGroup> <ItemGroup> + <Compile Include="Translation.cs" /> <Compile Include="Settings.cs" /> <Compile Include="MySleepTimer.cs"> </Compile> @@ -107,6 +108,9 @@ <Install>false</Install> </BootstrapperPackage> </ItemGroup> + <ItemGroup> + <Content Include="Language\de.xml" /> + </ItemGroup> <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" /> <!-- To modify your build process, add your task inside one of the targets below and uncomment it. Other similar extension points exist, see Microsoft.Common.targets. Added: trunk/plugins/MySleepTimer/MySleepTimer/Translation.cs =================================================================== --- trunk/plugins/MySleepTimer/MySleepTimer/Translation.cs (rev 0) +++ trunk/plugins/MySleepTimer/MySleepTimer/Translation.cs 2010-01-24 21:58:25 UTC (rev 3384) @@ -0,0 +1,177 @@ +using System; +using System.Collections.Generic; +using System.Globalization; +using System.IO; +using System.Reflection; +using System.Text.RegularExpressions; +using System.Xml; +using MediaPortal.Configuration; +using MediaPortal.GUI.Library; + +namespace MySleepTimer +{ + public static class Translation + { + #region Private variables + + private static Dictionary<string, string> _translations; + private static readonly string _path = string.Empty; + + #endregion + + #region Constructor + + static Translation() + { + string lang; + + try + { + lang = GUILocalizeStrings.GetCultureName(GUILocalizeStrings.CurrentLanguage()); + } + catch (Exception) + { + // when running MovingPicturesConfigTester outside of the MediaPortal directory this happens unfortunately + // so we grab the active culture name from the system + lang = CultureInfo.CurrentUICulture.Name; + } + + Log.Info("Using language " + lang); + + _path = Config.GetSubFolder(Config.Dir.Language, "MySleepTimer"); + + if (!System.IO.Directory.Exists(_path)) + System.IO.Directory.CreateDirectory(_path); + + LoadTranslations(lang); + } + + #endregion + + #region Public Properties + + /// <summary> + /// Gets the translated strings collection in the active language + /// </summary> + public static Dictionary<string, string> Strings + { + get + { + if (_translations == null) + { + _translations = new Dictionary<string, string>(); + Type transType = typeof(Translation); + FieldInfo[] fields = transType.GetFields(BindingFlags.Public | BindingFlags.Static); + foreach (FieldInfo field in fields) + { + _translations.Add(field.Name, field.GetValue(transType).ToString()); + } + } + return _translations; + } + } + + #endregion + + #region Public Methods + + public static int LoadTranslations(string lang) + { + XmlDocument doc = new XmlDocument(); + Dictionary<string, string> TranslatedStrings = new Dictionary<string, string>(); + string langPath = ""; + try + { + langPath = Path.Combine(_path, lang + ".xml"); + doc.Load(langPath); + } + catch (Exception e) + { + if (lang == "en") + return 0; // otherwise we are in an endless loop! + + if (e.GetType() == typeof(FileNotFoundException)) + Log.Warn("Cannot find translation file {0}. Failing back to English", langPath); + else + { + Log.Error("Error in translation xml file: {0}. Failing back to English", lang); + Log.Error(e); + } + + return LoadTranslations("en"); + } + foreach (XmlNode stringEntry in doc.DocumentElement.ChildNodes) + { + if (stringEntry.NodeType == XmlNodeType.Element) + try + { + TranslatedStrings.Add(stringEntry.Attributes.GetNamedItem("Field").Value, stringEntry.InnerText); + } + catch (Exception ex) + { + Log.Error("Error in Translation Engine"); + Log.Error(ex); + } + } + + Type TransType = typeof(Translation); + FieldInfo[] fieldInfos = TransType.GetFields(BindingFlags.Public | BindingFlags.Static); + foreach (FieldInfo fi in fieldInfos) + { + if (TranslatedStrings != null && TranslatedStrings.ContainsKey(fi.Name)) + TransType.InvokeMember(fi.Name, BindingFlags.SetField, null, TransType, new object[] { TranslatedStrings[fi.Name] }); + else + Log.Info("Translation not found for field: {0}. Using hard-coded English default.", fi.Name); + } + return TranslatedStrings.Count; + } + + public static string GetByName(string name) + { + if (!Strings.ContainsKey(name)) + return name; + + return Strings[name]; + } + + public static string GetByName(string name, params object[] args) + { + return String.Format(GetByName(name), args); + } + + /// <summary> + /// Takes an input string and replaces all ${named} variables with the proper translation if available + /// </summary> + /// <param name="input">a string containing ${named} variables that represent the translation keys</param> + /// <returns>translated input string</returns> + public static string ParseString(string input) + { + Regex replacements = new Regex(@"\$\{([^\}]+)\}"); + MatchCollection matches = replacements.Matches(input); + foreach (Match match in matches) + { + input = input.Replace(match.Value, GetByName(match.Groups[1].Value)); + } + return input; + } + + #endregion + + #region Translations / Strings + + // P + public static string PluginDescription = + "Want MediaPortal to be your sleeping agent of choice? Well, here you go! (but be warned - might be highly addictive!)"; + + // S + public static string SleepingInXMin = "Sleep in: {0} min"; + public static string SleepingInXMinPlaytime = "Sleep in: {0} min (remaining playtime)"; + public static string SleepTimer = "Sleeptimer"; + + // T + public static string TimeIsUp = "Time is up - Have a good night!"; + public static string TimerStopped = "Sleep Timer stopped!"; + + #endregion + } +} \ No newline at end of file This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |