2 * Copyright (c) 2023 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 using System.Globalization;
19 using System.Collections.Generic;
20 using System.Reflection;
22 using System.ComponentModel;
23 using System.Resources;
28 /// This class has the methods of the NUIGadgetResourceManager.
30 /// <since_tizen> 10 </since_tizen>
31 [EditorBrowsable(EditorBrowsableState.Never)]
32 public class NUIGadgetResourceManager
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>();
40 /// Initializes the resource manager of the gadget.
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)
49 throw new ArgumentNullException(nameof(info));
52 _resourcePath = info.ResourcePath;
53 _resourceDll = info.ResourceFile;
54 _resourceClassName = info.ResourceClassName;
58 /// Initializes the resource manager of the gadget.
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)
66 _resourcePath = resourcePath;
67 _resourceDll = resourceDll;
68 _resourceClassName = resourceClassName;
72 /// Get the value of the specified string resource.
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)
79 return GetString(name, CultureInfo.CurrentUICulture);
83 /// Gets the return value of the string resource localized for the specified culture.
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)
92 if (string.IsNullOrEmpty(name))
94 throw new ArgumentNullException(nameof(name));
97 if (cultureInfo == null)
99 Log.Warn("Use CurrentUICulture");
100 cultureInfo = CultureInfo.CurrentUICulture;
103 string result = string.Empty;
106 var resourceManager = GetResourceManager(cultureInfo.Name);
107 if (resourceManager != null)
109 result = resourceManager.GetString(name, cultureInfo);
112 if (string.IsNullOrEmpty(result))
114 resourceManager = GetResourceManager("default");
115 if (resourceManager != null)
117 #pragma warning disable CA1304
118 result = resourceManager.GetString(name);
119 #pragma warning restore CA1304
123 catch (InvalidOperationException e)
125 Log.Error("InvalidOperationException occurs. " + e.Message);
127 catch (MissingManifestResourceException e)
129 Log.Error("MissingManifestResourceException occurs. " + e.Message);
131 catch (MissingSatelliteAssemblyException e)
133 Log.Error("MissingSateliteAssemblyException occurs. " + e.Message);
139 private global::System.Resources.ResourceManager GetResourceManager(string path, string baseName)
141 global::System.Resources.ResourceManager resourceManager = null;
143 if (string.IsNullOrEmpty(path))
148 if (!File.Exists(path))
150 Log.Warn(path + " does not exist");
154 #pragma warning disable CA1031
157 Assembly assembly = Assembly.Load(File.ReadAllBytes(path));
158 if (assembly != null)
160 resourceManager = new global::System.Resources.ResourceManager(baseName, assembly);
161 if (resourceManager == null)
163 Log.Error("Failed to create ResourceManager");
168 catch (ArgumentNullException e)
170 Log.Error("ArgumentNullException occurs. " + e.Message);
172 catch (BadImageFormatException e)
174 Log.Error("BadImageFormatException occurs. " + e.Message);
178 Log.Error("Exception occurs. " + e.Message);
180 #pragma warning restore CA1031
182 return resourceManager;
186 private global::System.Resources.ResourceManager GetResourceManager(string locale)
188 global::System.Resources.ResourceManager resourceManager;
190 if (_resourceMap.TryGetValue(locale, out resourceManager))
192 return resourceManager;
195 string baseName = _resourceClassName;
197 if (locale == "default")
199 path = _resourcePath + _resourceDll;
203 path = _resourcePath + locale + "/" + _resourceDll;
204 baseName += "." + locale;
207 resourceManager = GetResourceManager(path, baseName);
208 if (resourceManager != null)
210 _resourceMap.Add(locale, resourceManager);
213 return resourceManager;