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 resourceManager = GetResourceManager(cultureInfo.TwoLetterISOLanguageName);
112 if (resourceManager != null)
114 result = resourceManager.GetString(name, cultureInfo);
117 if (string.IsNullOrEmpty(result))
119 resourceManager = GetResourceManager("default");
120 if (resourceManager != null)
122 #pragma warning disable CA1304
123 result = resourceManager.GetString(name);
124 #pragma warning restore CA1304
128 catch (InvalidOperationException e)
130 Log.Error("InvalidOperationException occurs. " + e.Message);
132 catch (MissingManifestResourceException e)
134 Log.Error("MissingManifestResourceException occurs. " + e.Message);
136 catch (MissingSatelliteAssemblyException e)
138 Log.Error("MissingSateliteAssemblyException occurs. " + e.Message);
144 private global::System.Resources.ResourceManager GetResourceManager(string path, string baseName)
146 global::System.Resources.ResourceManager resourceManager = null;
148 if (string.IsNullOrEmpty(path))
153 if (!File.Exists(path))
155 Log.Warn(path + " does not exist");
159 #pragma warning disable CA1031
162 Assembly assembly = Assembly.Load(File.ReadAllBytes(path));
163 if (assembly != null)
165 resourceManager = new global::System.Resources.ResourceManager(baseName, assembly);
166 if (resourceManager == null)
168 Log.Error("Failed to create ResourceManager");
173 catch (ArgumentNullException e)
175 Log.Error("ArgumentNullException occurs. " + e.Message);
177 catch (BadImageFormatException e)
179 Log.Error("BadImageFormatException occurs. " + e.Message);
183 Log.Error("Exception occurs. " + e.Message);
185 #pragma warning restore CA1031
187 return resourceManager;
191 private global::System.Resources.ResourceManager GetResourceManager(string locale)
193 global::System.Resources.ResourceManager resourceManager;
195 if (_resourceMap.TryGetValue(locale, out resourceManager))
197 return resourceManager;
200 string baseName = _resourceClassName;
202 if (locale == "default")
204 path = _resourcePath + _resourceDll;
208 path = _resourcePath + locale + "/" + _resourceDll;
209 baseName += "." + locale;
212 resourceManager = GetResourceManager(path, baseName);
213 if (resourceManager != null)
215 _resourceMap.Add(locale, resourceManager);
218 return resourceManager;