2bb42eb18354fd40cba4fa2cac5cf56059fcb206
[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                     result = resourceManager.GetString(name, cultureInfo);
110                 }
111
112                 if (string.IsNullOrEmpty(result))
113                 {
114                     resourceManager = GetResourceManager("default");
115                     if (resourceManager != null)
116                     {
117 #pragma warning disable CA1304
118                         result = resourceManager.GetString(name);
119 #pragma warning restore CA1304
120                     }
121                 }
122             }
123             catch (InvalidOperationException e)
124             {
125                 Log.Error("InvalidOperationException occurs. " + e.Message);
126             }
127             catch (MissingManifestResourceException e)
128             {
129                 Log.Error("MissingManifestResourceException occurs. " + e.Message);
130             }
131             catch (MissingSatelliteAssemblyException e)
132             {
133                 Log.Error("MissingSateliteAssemblyException occurs. " + e.Message);
134             }
135
136             return result;
137         }
138
139         private global::System.Resources.ResourceManager GetResourceManager(string path, string baseName)
140         {
141             global::System.Resources.ResourceManager resourceManager = null;
142
143             if (string.IsNullOrEmpty(path))
144             {
145                 return null;
146             }
147
148             if (!File.Exists(path))
149             {
150                 Log.Warn(path + " does not exist");
151                 return null;
152             }
153
154 #pragma warning disable CA1031
155             try
156             {
157                 Assembly assembly = Assembly.Load(File.ReadAllBytes(path));
158                 if (assembly != null)
159                 {
160                     resourceManager = new global::System.Resources.ResourceManager(baseName, assembly);
161                     if (resourceManager == null)
162                     {
163                         Log.Error("Failed to create ResourceManager");
164                         return null;
165                     }
166                 }
167             }
168             catch (ArgumentNullException e)
169             {
170                 Log.Error("ArgumentNullException occurs. " + e.Message);
171             }
172             catch (BadImageFormatException e)
173             {
174                 Log.Error("BadImageFormatException occurs. " + e.Message);
175             }
176             catch (Exception e)
177             {
178                 Log.Error("Exception occurs. " + e.Message);
179             }
180 #pragma warning restore CA1031
181
182             return resourceManager;
183         }
184
185
186         private global::System.Resources.ResourceManager GetResourceManager(string locale)
187         {
188             global::System.Resources.ResourceManager resourceManager;
189
190             if (_resourceMap.TryGetValue(locale, out resourceManager))
191             {
192                 return resourceManager;
193             }
194
195             string baseName = _resourceClassName;
196             string path;
197             if (locale == "default")
198             {
199                 path = _resourcePath + _resourceDll;
200             }
201             else
202             {
203                 path = _resourcePath + locale + "/" + _resourceDll;
204                 baseName += "." + locale;
205             }
206
207             resourceManager = GetResourceManager(path, baseName);
208             if (resourceManager != null)
209             {
210                 _resourceMap.Add(locale, resourceManager);
211             }
212
213             return resourceManager;
214         }
215     }
216 }