[Common] Set the current CultureInfo
[platform/core/csapi/tizenfx.git] / src / Tizen.Applications.Common / Tizen.Applications / CoreApplication.cs
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 using System;
18 using System.Globalization;
19 using Tizen.Applications.CoreBackend;
20
21 namespace Tizen.Applications
22 {
23     /// <summary>
24     /// This class represents an application controlled lifecycles by the backend system.
25     /// </summary>
26     /// <since_tizen> 3 </since_tizen>
27     public class CoreApplication : Application
28     {
29         private readonly ICoreBackend _backend;
30         private bool _disposedValue = false;
31
32         /// <summary>
33         /// Initializes the CoreApplication class.
34         /// </summary>
35         /// <param name="backend">The backend instance implementing ICoreBacked interface.</param>
36         /// <since_tizen> 3 </since_tizen>
37         public CoreApplication(ICoreBackend backend)
38         {
39             _backend = backend;
40         }
41
42         /// <summary>
43         /// Occurs when the application is launched.
44         /// </summary>
45         /// <since_tizen> 3 </since_tizen>
46         public event EventHandler Created;
47
48         /// <summary>
49         /// Occurs when the application is about to shutdown.
50         /// </summary>
51         /// <since_tizen> 3 </since_tizen>
52         public event EventHandler Terminated;
53
54         /// <summary>
55         /// Occurs whenever the application receives the appcontrol message.
56         /// </summary>
57         /// <since_tizen> 3 </since_tizen>
58         public event EventHandler<AppControlReceivedEventArgs> AppControlReceived;
59
60         /// <summary>
61         /// Occurs when the system memory is low.
62         /// </summary>
63         /// <since_tizen> 3 </since_tizen>
64         public event EventHandler<LowMemoryEventArgs> LowMemory;
65
66         /// <summary>
67         /// Occurs when the system battery is low.
68         /// </summary>
69         /// <since_tizen> 3 </since_tizen>
70         public event EventHandler<LowBatteryEventArgs> LowBattery;
71
72         /// <summary>
73         /// Occurs when the system language is chagned.
74         /// </summary>
75         /// <since_tizen> 3 </since_tizen>
76         public event EventHandler<LocaleChangedEventArgs> LocaleChanged;
77
78         /// <summary>
79         /// Occurs when the region format is changed.
80         /// </summary>
81         /// <since_tizen> 3 </since_tizen>
82         public event EventHandler<RegionFormatChangedEventArgs> RegionFormatChanged;
83
84         /// <summary>
85         /// Occurs when the device orientation is changed.
86         /// </summary>
87         /// <since_tizen> 3 </since_tizen>
88         public event EventHandler<DeviceOrientationEventArgs> DeviceOrientationChanged;
89
90         /// <summary>
91         /// The backend instance.
92         /// </summary>
93         /// <since_tizen> 3 </since_tizen>
94         protected ICoreBackend Backend { get { return _backend; } }
95
96         /// <summary>
97         /// Runs the application's main loop.
98         /// </summary>
99         /// <param name="args">Arguments from commandline.</param>
100         /// <since_tizen> 3 </since_tizen>
101         public override void Run(string[] args)
102         {
103             base.Run(args);
104
105             _backend.AddEventHandler(EventType.Created, OnCreate);
106             _backend.AddEventHandler(EventType.Terminated, OnTerminate);
107             _backend.AddEventHandler<AppControlReceivedEventArgs>(EventType.AppControlReceived, OnAppControlReceived);
108             _backend.AddEventHandler<LowMemoryEventArgs>(EventType.LowMemory, OnLowMemory);
109             _backend.AddEventHandler<LowBatteryEventArgs>(EventType.LowBattery, OnLowBattery);
110             _backend.AddEventHandler<LocaleChangedEventArgs>(EventType.LocaleChanged, OnLocaleChanged);
111             _backend.AddEventHandler<RegionFormatChangedEventArgs>(EventType.RegionFormatChanged, OnRegionFormatChanged);
112             _backend.AddEventHandler<DeviceOrientationEventArgs>(EventType.DeviceOrientationChanged, OnDeviceOrientationChanged);
113
114             string[] argsClone = null;
115
116             if (args == null)
117             {
118                 argsClone = new string[1];
119             }
120             else
121             {
122                 argsClone = new string[args.Length + 1];
123                 args.CopyTo(argsClone, 1);
124             }
125             argsClone[0] = string.Empty;
126             _backend.Run(argsClone);
127         }
128
129         /// <summary>
130         /// Exits the main loop of the application.
131         /// </summary>
132         /// <since_tizen> 3 </since_tizen>
133         public override void Exit()
134         {
135             _backend.Exit();
136         }
137
138         /// <summary>
139         /// Overrides this method if want to handle behavior when the application is launched.
140         /// If base.OnCreated() is not called, the event 'Created' will not be emitted.
141         /// </summary>
142         /// <since_tizen> 3 </since_tizen>
143         protected virtual void OnCreate()
144         {
145             Created?.Invoke(this, EventArgs.Empty);
146         }
147
148         /// <summary>
149         /// Overrides this method if want to handle behavior when the application is terminated.
150         /// If base.OnTerminate() is not called, the event 'Terminated' will not be emitted.
151         /// </summary>
152         /// <since_tizen> 3 </since_tizen>
153         protected virtual void OnTerminate()
154         {
155             Terminated?.Invoke(this, EventArgs.Empty);
156         }
157
158         /// <summary>
159         /// Overrides this method if want to handle behavior when the application receives the appcontrol message.
160         /// If base.OnAppControlReceived() is not called, the event 'AppControlReceived' will not be emitted.
161         /// </summary>
162         /// <param name="e"></param>
163         /// <since_tizen> 3 </since_tizen>
164         protected virtual void OnAppControlReceived(AppControlReceivedEventArgs e)
165         {
166             AppControlReceived?.Invoke(this, e);
167         }
168
169         /// <summary>
170         /// Overrides this method if want to handle behavior when the system memory is low.
171         /// If base.OnLowMemory() is not called, the event 'LowMemory' will not be emitted.
172         /// </summary>
173         /// <since_tizen> 3 </since_tizen>
174         protected virtual void OnLowMemory(LowMemoryEventArgs e)
175         {
176             LowMemory?.Invoke(this, e);
177             System.GC.Collect();
178         }
179
180         /// <summary>
181         /// Overrides this method if want to handle behavior when the system battery is low.
182         /// If base.OnLowBattery() is not called, the event 'LowBattery' will not be emitted.
183         /// </summary>
184         /// <since_tizen> 3 </since_tizen>
185         protected virtual void OnLowBattery(LowBatteryEventArgs e)
186         {
187             LowBattery?.Invoke(this, e);
188         }
189
190         /// <summary>
191         /// Overrides this method if want to handle behavior when the system language is changed.
192         /// If base.OnLocaleChanged() is not called, the event 'LocaleChanged' will not be emitted.
193         /// </summary>
194         /// <since_tizen> 3 </since_tizen>
195         protected virtual void OnLocaleChanged(LocaleChangedEventArgs e)
196         {
197             ChangeCurrentCultureInfo(e.Locale);
198             LocaleChanged?.Invoke(this, e);
199         }
200
201         /// <summary>
202         /// Overrides this method if want to handle behavior when the region format is changed.
203         /// If base.OnRegionFormatChanged() is not called, the event 'RegionFormatChanged' will not be emitted.
204         /// </summary>
205         /// <since_tizen> 3 </since_tizen>
206         protected virtual void OnRegionFormatChanged(RegionFormatChangedEventArgs e)
207         {
208             RegionFormatChanged?.Invoke(this, e);
209         }
210
211         /// <summary>
212         /// Overrides this method if want to handle behavior when the device orientation is changed.
213         /// If base.OnRegionFormatChanged() is not called, the event 'RegionFormatChanged' will not be emitted.
214         /// </summary>
215         /// <since_tizen> 3 </since_tizen>
216         protected virtual void OnDeviceOrientationChanged(DeviceOrientationEventArgs e)
217         {
218             DeviceOrientationChanged?.Invoke(this, e);
219         }
220
221         /// <summary>
222         /// Releases any unmanaged resources used by this object. Can also dispose any other disposable objects.
223         /// </summary>
224         /// <param name="disposing">If true, disposes any disposable objects. If false, does not dispose disposable objects.</param>
225         /// <since_tizen> 3 </since_tizen>
226         protected override void Dispose(bool disposing)
227         {
228             if (!_disposedValue)
229             {
230                 if (disposing)
231                 {
232                     _backend.Dispose();
233                 }
234
235                 _disposedValue = true;
236             }
237             base.Dispose(disposing);
238         }
239
240         private void ChangeCurrentCultureInfo(string locale)
241         {
242             string languageCode = string.Empty;
243             string localeCode = string.Empty;
244             CultureInfo currentCultureInfo = null;
245
246             if (locale.Contains("."))
247             {
248                 locale = locale.Substring(0, locale.IndexOf("."));
249             }
250
251             if (locale.Contains("_"))
252             {
253                 locale = locale.Replace("_", "-");
254             }
255
256             var dashIndex = locale.IndexOf("-", StringComparison.Ordinal);
257             if (dashIndex > 0)
258             {
259                 var parts = locale.Split('-');
260                 languageCode = parts[0];
261                 localeCode = parts[1];
262             }
263             else
264             {
265                 languageCode = locale;
266                 localeCode = "";
267             }
268
269             foreach (CultureInfo ci in CultureInfo.GetCultures(CultureTypes.AllCultures))
270             {
271                 if (ci.TwoLetterISOLanguageName == languageCode)
272                 {
273                     if (localeCode == "")
274                     {
275                         if (ci.Name == languageCode)
276                         {
277                             currentCultureInfo = new CultureInfo(ci.Name);
278                             break;
279                         }
280                     }
281                     else
282                     {
283                         if (ci.Name.Contains(localeCode.ToUpper()))
284                         {
285                             currentCultureInfo = new CultureInfo(ci.Name);
286                             break;
287                         }
288                     }
289                 }
290             }
291
292             if (currentCultureInfo == null)
293             {
294                 try
295                 {
296                     currentCultureInfo = new CultureInfo(languageCode);
297                 }
298                 catch (CultureNotFoundException)
299                 {
300                     currentCultureInfo = new CultureInfo("en");
301                 }
302             }
303
304             CultureInfo.CurrentCulture = currentCultureInfo;
305         }
306     }
307 }