LocalizableString.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. using System;
  2. using System.Globalization;
  3. namespace Abp.Localization
  4. {
  5. /// <summary>
  6. /// Represents a string that can be localized.
  7. /// </summary>
  8. [Serializable]
  9. public class LocalizableString : ILocalizableString
  10. {
  11. /// <summary>
  12. /// Unique name of the localization source.
  13. /// </summary>
  14. public virtual string SourceName { get; private set; }
  15. /// <summary>
  16. /// Unique Name of the string to be localized.
  17. /// </summary>
  18. public virtual string Name { get; private set; }
  19. /// <summary>
  20. /// Needed for serialization.
  21. /// </summary>
  22. private LocalizableString()
  23. {
  24. }
  25. /// <param name="name">Unique Name of the string to be localized</param>
  26. /// <param name="sourceName">Unique name of the localization source</param>
  27. public LocalizableString(string name, string sourceName)
  28. {
  29. if (name == null)
  30. {
  31. throw new ArgumentNullException("name");
  32. }
  33. if (sourceName == null)
  34. {
  35. throw new ArgumentNullException("sourceName");
  36. }
  37. Name = name;
  38. SourceName = sourceName;
  39. }
  40. public string Localize(ILocalizationContext context)
  41. {
  42. return context.LocalizationManager.GetString(SourceName, Name);
  43. }
  44. public string Localize(ILocalizationContext context, CultureInfo culture)
  45. {
  46. return context.LocalizationManager.GetString(SourceName, Name, culture);
  47. }
  48. public override string ToString()
  49. {
  50. return string.Format("[LocalizableString: {0}, {1}]", Name, SourceName);
  51. }
  52. }
  53. }