PageRenderTime 13ms CodeModel.GetById 8ms app.highlight 2ms 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
 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
23using System;
24using System.Globalization;
25using System.Threading;
26
27namespace Bifrost.Globalization
28{
29    /// <summary>
30    /// Represents a scope for localization, when exiting the scope, cultures will be reset back to the cultures given at construction.
31    /// The scopes purpose is to enable one to change cultures within a given scope but have it gracefully reset back to the previous
32    /// or a given culture when disposed
33    /// </summary>
34	public class LocalizationScope : IDisposable
35	{
36        /// <summary>
37        /// Gets the culture for the <see cref="LocalizationScope"/>
38        /// </summary>
39		public CultureInfo Culture { get; private set; }
40
41        /// <summary>
42        /// Gets the UI culture for the <see cref="LocalizationScope"/>
43        /// </summary>
44		public CultureInfo UICulture { get; private set; }
45
46        /// <summary>
47        /// Initializes a new instance of <see cref="LocalizationScope"/>
48        /// </summary>
49        /// <param name="culture"><see cref="CultureInfo"/> to initialize the scope with</param>
50        /// <param name="uiCulture"><see cref="CultureInfo"/> to initialize the scope as the UI culture with</param>
51		public LocalizationScope(CultureInfo culture, CultureInfo uiCulture)
52		{
53			Culture = culture;
54			UICulture = uiCulture;
55		}
56
57        /// <summary>
58        /// Get current <see cref="LocalizationScope"/> from the current thread
59        /// </summary>
60        /// <returns></returns>
61		public static LocalizationScope	FromCurrentThread()
62		{
63			var scope = new LocalizationScope(Thread.CurrentThread.CurrentCulture, Thread.CurrentThread.CurrentUICulture);
64			return scope;
65		}
66
67        /// <summary>
68        /// Dispose the scope, resetting the culture back to the cultures given at construction
69        /// </summary>
70		public void Dispose()
71		{
72			Thread.CurrentThread.CurrentCulture = Culture;
73			Thread.CurrentThread.CurrentUICulture = UICulture;
74		}
75	}
76}