Merge remote-tracking branch 'origin/master' into tizen
[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
34         public static void Initialize()
35         {
36             if (File.Exists(_pathCultureInfoXml))
37             {
38                 try
39                 {
40                     ParseCultureInfoXml();
41                 }
42                 catch
43                 {
44                     Log.Warn(LogTag, "Failed to parse CultureInfo.xml");
45                 }
46             }
47
48             _initialized = true;
49         }
50
51         private static void ParseCultureInfoXml()
52         {
53             XmlDocument doc = new XmlDocument();
54             doc.Load(_pathCultureInfoXml);
55             XmlElement root = doc.DocumentElement;
56             foreach (XmlElement node in root.ChildNodes)
57             {
58                 if (node.Name != "name" && node.FirstChild == null)
59                 {
60                     continue;
61                 }
62
63                 string lang = node.GetAttribute("xml:lang");
64                 string cultureName = node.FirstChild.Value;
65                 if (!string.IsNullOrEmpty(lang) && !string.IsNullOrEmpty(cultureName))
66                 {
67                     try
68                     {
69                         _cultureNames.Add(lang, cultureName);
70                     }
71                     catch (ArgumentException e)
72                     {
73                         Log.Error(LogTag, "ArgumentException: " + e.Message);
74                     }
75                 }
76             }
77         }
78
79         public static string GetCultureName(string locale)
80         {
81             lock (_lock)
82             {
83                 if (!_initialized)
84                 {
85                     Initialize();
86                 }
87
88                 if (_cultureNames.TryGetValue(locale.ToLowerInvariant(), out string cultureName))
89                 {
90                     return cultureName;
91                 }
92             }
93
94             return string.Empty;
95         }
96     }
97 }