Release 10.0.0.16997
[platform/core/csapi/tizenfx.git] / src / Tizen.Applications.Common / Tizen.Applications / CultureInfoHelper.cs
1 /*
2  * Copyright (c) 2021 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.Collections.Generic;
19 using System.Globalization;
20 using System.IO;
21 using System.Xml;
22 using Tizen;
23
24 namespace Tizen.Applications
25 {
26     internal static class CultureInfoHelper
27     {
28         private const string LogTag = "Tizen.Applications";
29         private static bool _initialized = false;
30         private static readonly Dictionary<string, string> _cultureNames = new Dictionary<string, string>();
31         private static readonly object _lock = new object();
32         private const string _pathCultureInfoXml = "/usr/share/dotnet.tizen/framework/i18n/CultureInfo.xml";
33         private static readonly CultureInfo _cultureInfo = new CultureInfo("en-US");
34
35         public static void Initialize()
36         {
37             if (File.Exists(_pathCultureInfoXml))
38             {
39                 try
40                 {
41                     ParseCultureInfoXml();
42                 }
43                 catch
44                 {
45                     Log.Warn(LogTag, "Failed to parse CultureInfo.xml");
46                 }
47             }
48
49             _initialized = true;
50         }
51
52         private static void ParseCultureInfoXml()
53         {
54             XmlDocument doc = new XmlDocument();
55             doc.Load(_pathCultureInfoXml);
56             XmlElement root = doc.DocumentElement;
57             foreach (XmlElement node in root.ChildNodes)
58             {
59                 if (node.Name != "name" && node.FirstChild == null)
60                 {
61                     continue;
62                 }
63
64                 string lang = node.GetAttribute("xml:lang");
65                 string cultureName = node.FirstChild.Value;
66                 if (!string.IsNullOrEmpty(lang) && !string.IsNullOrEmpty(cultureName))
67                 {
68                     try
69                     {
70                         _cultureNames.Add(lang, cultureName);
71                     }
72                     catch (ArgumentException e)
73                     {
74                         Log.Error(LogTag, "ArgumentException: " + e.Message);
75                     }
76                 }
77             }
78         }
79
80         public static string GetCultureName(string locale)
81         {
82             lock (_lock)
83             {
84                 if (!_initialized)
85                 {
86                     Initialize();
87                 }
88
89                 if (_cultureNames.TryGetValue(locale.ToLower(_cultureInfo), out string cultureName))
90                 {
91                     return cultureName;
92                 }
93             }
94
95             return string.Empty;
96         }
97     }
98 }