LocalizationManager.cs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Linq;
  5. using System.Xml.Linq;
  6. using GCAS.Dto;
  7. namespace GCAS.Localization
  8. {
  9. internal static class LocalizationHelper
  10. {
  11. public static List<NameValue> GetSource(CultureInfo cultureInfo)
  12. {
  13. var xmlSoruce = Properties.Resources.Languages;
  14. XDocument xdoc = XDocument.Parse(xmlSoruce);
  15. var culture = xdoc.Root.Elements("Language").SingleOrDefault(c => c.Attribute("culture").Value == cultureInfo.Name);
  16. if (culture != null)
  17. {
  18. var texts = culture.Element("texts")
  19. .Elements("text")
  20. .Select(c => new NameValue
  21. {
  22. Name = c.Attribute("name").Value,
  23. Value = c.Value
  24. }).ToList();
  25. return texts;
  26. }
  27. else
  28. {
  29. throw new Exception("未找到资源名为: " + cultureInfo.Name + " 的资源包!");
  30. }
  31. }
  32. public static string GetString(this List<NameValue> source, string name)
  33. {
  34. if (source != null && source.Any())
  35. {
  36. var value = source.SingleOrDefault(c => c.Name.ToLower() == name.ToLower());
  37. if (value == null)
  38. {
  39. return name;
  40. }
  41. else
  42. {
  43. return value.Value;
  44. }
  45. }
  46. throw new Exception("资源文件为空!");
  47. }
  48. }
  49. }