StringProvider.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. namespace GDNXFD.Alert.Config.Resources.Strings
  2. {
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Globalization;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Data;
  10. using System.Resources;
  11. using System.Reflection;
  12. /// <summary>
  13. /// String provider acts as a resolver to allow data binding to resx files.
  14. /// </summary>
  15. public class StringProvider
  16. {
  17. private static ObjectDataProvider provider;
  18. /// <summary>
  19. /// Default constructor.
  20. /// </summary>
  21. public StringProvider()
  22. {
  23. }
  24. /// <summary>
  25. /// Object data provider from app.xaml
  26. /// </summary>
  27. public static ObjectDataProvider Provider
  28. {
  29. get
  30. {
  31. if (provider == null)
  32. provider = (ObjectDataProvider)App.Current.FindResource("Resources");
  33. return provider;
  34. }
  35. }
  36. /// <summary>
  37. /// Returns an instance of the resx file.
  38. /// </summary>
  39. /// <returns></returns>
  40. public StringResources GetResourceInstance()
  41. {
  42. return new StringResources();
  43. }
  44. /// <summary>
  45. /// Apply change to current culture and refresh the string provider.
  46. /// </summary>
  47. /// <param name="culture"></param>
  48. public static void ChangeCulture(CultureInfo culture)
  49. {
  50. Properties.Resources.Culture = culture;
  51. Provider.Refresh();
  52. }
  53. /// <summary>
  54. /// Get a property string from it name.
  55. /// </summary>
  56. /// <param name="propertyName"></param>
  57. /// <returns></returns>
  58. public static string GetString(string propertyName)
  59. {
  60. if (string.IsNullOrWhiteSpace(propertyName))
  61. return string.Empty;
  62. try
  63. {
  64. ResourceManager man = new ResourceManager("GDNXFD.Alert.Config.Resources.Strings.StringResources", Assembly.GetExecutingAssembly());
  65. return man.GetString(propertyName);
  66. }
  67. catch
  68. {
  69. return string.Empty;
  70. }
  71. }
  72. }
  73. }