[NUI.Gadget] Fix NUIGadgetResourceManager (#6069)
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI.Gadget / Tizen.NUI / NUIGadgetResourceManager.cs
1 /*
2  * Copyright (c) 2023 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 System.Collections.Generic;
20 using System.Reflection;
21 using System.IO;
22 using System.ComponentModel;
23 using System.Resources;
24
25 namespace Tizen.NUI
26 {
27     /// <summary>
28     /// This class has the methods of the NUIGadgetResourceManager.
29     /// </summary>
30     /// <since_tizen> 10 </since_tizen>
31     [EditorBrowsable(EditorBrowsableState.Never)]
32     public class NUIGadgetResourceManager
33     {
34         private readonly string _resourcePath;
35         private readonly string _resourceDll;
36         private readonly string _resourceClassName;
37         private readonly IDictionary<string, global::System.Resources.ResourceManager> _resourceMap = new Dictionary<string, global::System.Resources.ResourceManager>();
38
39         /// <summary>
40         /// Initializes the resource manager of the gadget.
41         /// </summary>
42         /// <param name="info">The information of the gadget.</param>
43         /// <exception cref="ArgumentNullException">Thrown when failed because of a invalid argument.</exception>
44         /// <since_tizen> 11 </since_tizen>
45         public NUIGadgetResourceManager(NUIGadgetInfo info)
46         {
47             if (info == null)
48             {
49                 throw new ArgumentNullException(nameof(info));
50             }
51
52             _resourcePath = info.ResourcePath;
53             _resourceDll = info.ResourceFile;
54             _resourceClassName = info.ResourceClassName;
55         }
56
57         /// <summary>
58         /// Initializes the resource manager of the gadget.
59         /// </summary>
60         /// <param name="resourcePath">The path of the resource</param>
61         /// <param name="resourceDll">The file name of the resource.</param>
62         /// <param name="resourceClassName">The class name of the resource.</param>
63         /// <since_tizen> 10 </since_tizen>
64         public NUIGadgetResourceManager(string resourcePath, string resourceDll, string resourceClassName)
65         {
66             _resourcePath = resourcePath;
67             _resourceDll = resourceDll;
68             _resourceClassName = resourceClassName;
69         }
70
71         /// <summary>
72         /// Get the value of the specified string resource.
73         /// </summary>
74         /// <param name="name">The name of the resource to retrieve.</param>
75         /// <returns>The value of the resource, or null if name cannot be found in a resource set.</returns>
76         /// <since_tizen> 10 </since_tizen>
77         public string GetString(string name)
78         {
79             return GetString(name, CultureInfo.CurrentUICulture);
80         }
81
82         /// <summary>
83         /// Gets the return value of the string resource localized for the specified culture.
84         /// </summary>
85         /// <param name="name">The name of the resource to retrieve.</param>
86         /// <param name="cultureInfo">An object that represents the culture for which the resource is localied.</param>
87         /// <returns>The value of the resource localied for the specified culture, or null if name cannot be found in a resource set.</returns>
88         /// <exception cref="ArgumentNullException">Thrown when failed because of a invalid argument.</exception>
89         /// <since_tizen> 10 </since_tizen>
90         public string GetString(string name, CultureInfo cultureInfo)
91         {
92             if (string.IsNullOrEmpty(name))
93             {
94                 throw new ArgumentNullException(nameof(name));
95             }
96
97             if (cultureInfo == null)
98             {
99                 Log.Warn("Use CurrentUICulture");
100                 cultureInfo = CultureInfo.CurrentUICulture;
101             }
102
103             string result = string.Empty;
104             try
105             {
106                 var resourceManager = GetResourceManager(cultureInfo.Name);
107                 if (resourceManager == null)
108                 {
109                     resourceManager = GetResourceManager(cultureInfo.TwoLetterISOLanguageName);
110                 }
111
112                 if (resourceManager != null)
113                 {
114                     result = resourceManager.GetString(name, cultureInfo);
115                 }
116
117                 if (string.IsNullOrEmpty(result))
118                 {
119                     resourceManager = GetResourceManager("default");
120                     if (resourceManager != null)
121                     {
122 #pragma warning disable CA1304
123                         result = resourceManager.GetString(name);
124 #pragma warning restore CA1304
125                     }
126                 }
127             }
128             catch (InvalidOperationException e)
129             {
130                 Log.Error("InvalidOperationException occurs. " + e.Message);
131             }
132             catch (MissingManifestResourceException e)
133             {
134                 Log.Error("MissingManifestResourceException occurs. " + e.Message);
135             }
136             catch (MissingSatelliteAssemblyException e)
137             {
138                 Log.Error("MissingSateliteAssemblyException occurs. " + e.Message);
139             }
140
141             return result;
142         }
143
144         private global::System.Resources.ResourceManager GetResourceManager(string path, string baseName)
145         {
146             global::System.Resources.ResourceManager resourceManager = null;
147
148             if (string.IsNullOrEmpty(path))
149             {
150                 return null;
151             }
152
153             if (!File.Exists(path))
154             {
155                 Log.Warn(path + " does not exist");
156                 return null;
157             }
158
159 #pragma warning disable CA1031
160             try
161             {
162                 Assembly assembly = Assembly.Load(File.ReadAllBytes(path));
163                 if (assembly != null)
164                 {
165                     resourceManager = new global::System.Resources.ResourceManager(baseName, assembly);
166                     if (resourceManager == null)
167                     {
168                         Log.Error("Failed to create ResourceManager");
169                         return null;
170                     }
171                 }
172             }
173             catch (ArgumentNullException e)
174             {
175                 Log.Error("ArgumentNullException occurs. " + e.Message);
176             }
177             catch (BadImageFormatException e)
178             {
179                 Log.Error("BadImageFormatException occurs. " + e.Message);
180             }
181             catch (Exception e)
182             {
183                 Log.Error("Exception occurs. " + e.Message);
184             }
185 #pragma warning restore CA1031
186
187             return resourceManager;
188         }
189
190
191         private global::System.Resources.ResourceManager GetResourceManager(string locale)
192         {
193             global::System.Resources.ResourceManager resourceManager;
194
195             if (_resourceMap.TryGetValue(locale, out resourceManager))
196             {
197                 return resourceManager;
198             }
199
200             string baseName = _resourceClassName;
201             string path;
202             if (locale == "default")
203             {
204                 path = _resourcePath + _resourceDll;
205             }
206             else
207             {
208                 path = _resourcePath + locale + "/" + _resourceDll;
209                 baseName += "." + locale;
210             }
211
212             resourceManager = GetResourceManager(path, baseName);
213             if (resourceManager != null)
214             {
215                 _resourceMap.Add(locale, resourceManager);
216             }
217
218             return resourceManager;
219         }
220     }
221 }