PageRenderTime 41ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/Source/Bifrost/Globalization/LocalizationScope.cs

#
C# | 76 lines | 28 code | 6 blank | 42 comment | 0 complexity | 8d2b902b12f376dce660f50ff777f88d MD5 | raw file
Possible License(s): CC-BY-SA-3.0
  1. #region License
  2. //
  3. // Copyright (c) 2008-2012, DoLittle Studios and Komplett ASA
  4. //
  5. // Licensed under the Microsoft Permissive License (Ms-PL), Version 1.1 (the "License")
  6. // With one exception :
  7. // Commercial libraries that is based partly or fully on Bifrost and is sold commercially,
  8. // must obtain a commercial license.
  9. //
  10. // You may not use this file except in compliance with the License.
  11. // You may obtain a copy of the license at
  12. //
  13. // http://bifrost.codeplex.com/license
  14. //
  15. // Unless required by applicable law or agreed to in writing, software
  16. // distributed under the License is distributed on an "AS IS" BASIS,
  17. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18. // See the License for the specific language governing permissions and
  19. // limitations under the License.
  20. //
  21. #endregion
  22. using System;
  23. using System.Globalization;
  24. using System.Threading;
  25. namespace Bifrost.Globalization
  26. {
  27. /// <summary>
  28. /// Represents a scope for localization, when exiting the scope, cultures will be reset back to the cultures given at construction.
  29. /// The scopes purpose is to enable one to change cultures within a given scope but have it gracefully reset back to the previous
  30. /// or a given culture when disposed
  31. /// </summary>
  32. public class LocalizationScope : IDisposable
  33. {
  34. /// <summary>
  35. /// Gets the culture for the <see cref="LocalizationScope"/>
  36. /// </summary>
  37. public CultureInfo Culture { get; private set; }
  38. /// <summary>
  39. /// Gets the UI culture for the <see cref="LocalizationScope"/>
  40. /// </summary>
  41. public CultureInfo UICulture { get; private set; }
  42. /// <summary>
  43. /// Initializes a new instance of <see cref="LocalizationScope"/>
  44. /// </summary>
  45. /// <param name="culture"><see cref="CultureInfo"/> to initialize the scope with</param>
  46. /// <param name="uiCulture"><see cref="CultureInfo"/> to initialize the scope as the UI culture with</param>
  47. public LocalizationScope(CultureInfo culture, CultureInfo uiCulture)
  48. {
  49. Culture = culture;
  50. UICulture = uiCulture;
  51. }
  52. /// <summary>
  53. /// Get current <see cref="LocalizationScope"/> from the current thread
  54. /// </summary>
  55. /// <returns></returns>
  56. public static LocalizationScope FromCurrentThread()
  57. {
  58. var scope = new LocalizationScope(Thread.CurrentThread.CurrentCulture, Thread.CurrentThread.CurrentUICulture);
  59. return scope;
  60. }
  61. /// <summary>
  62. /// Dispose the scope, resetting the culture back to the cultures given at construction
  63. /// </summary>
  64. public void Dispose()
  65. {
  66. Thread.CurrentThread.CurrentCulture = Culture;
  67. Thread.CurrentThread.CurrentUICulture = UICulture;
  68. }
  69. }
  70. }