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>();
39 internal NUIGadgetResourceManager(NUIGadgetInfo info)
41 _resourcePath = info.ResourcePath; ;
42 _resourceDll = info.ResourceFile;
43 _resourceClassName = info.ResourceClassName;
47 /// Initializes the resource manager of the gadget.
49 /// <param name="resourcePath">The path of the resource</param>
50 /// <param name="resourceDll">The file name of the resource.</param>
51 /// <param name="resourceClassName">The class name of the resource.</param>
52 /// <since_tizen> 10 </since_tizen>
53 public NUIGadgetResourceManager(string resourcePath, string resourceDll, string resourceClassName)
55 _resourcePath = resourcePath;
56 _resourceDll = resourceDll;
57 _resourceClassName = resourceClassName;
61 /// Get the value of the specified string resource.
63 /// <param name="name">The name of the resource to retrieve.</param>
64 /// <returns>The value of the resource, or null if name cannot be found in a resource set.</returns>
65 /// <since_tizen> 10 </since_tizen>
66 public string GetString(string name)
68 return GetString(name, CultureInfo.CurrentUICulture);
72 /// Gets the return value of the string resource localied for the specified culture.
74 /// <param name="name">The name of the resource to retrieve.</param>
75 /// <param name="cultureInfo">An object that represents the culture for which the resource is localied.</param>
76 /// <returns>The value of the resource localied for the specified culture, or null if name cannot be found in a resource set.</returns>
77 /// <exception cref="ArgumentNullException">Thrown when failed because of a invalid argument.</exception>
78 /// <since_tizen> 10 </since_tizen>
79 public string GetString(string name, CultureInfo cultureInfo)
81 if (string.IsNullOrEmpty(name))
83 throw new ArgumentNullException(nameof(name));
86 if (cultureInfo == null)
88 Log.Warn("Use CurrentUICulture");
89 cultureInfo = CultureInfo.CurrentUICulture;
92 string result = string.Empty;
95 var resourceManager = GetResourceManager(cultureInfo.Name);
96 if (resourceManager != null)
98 result = resourceManager.GetString(name, cultureInfo);
101 if (string.IsNullOrEmpty(result))
103 resourceManager = GetResourceManager("default");
104 if (resourceManager != null)
106 #pragma warning disable CA1304
107 result = resourceManager.GetString(name);
108 #pragma warning restore CA1304
112 catch (InvalidOperationException e)
114 Log.Error("InvalidOperationException occurs. " + e.Message);
116 catch (MissingManifestResourceException e)
118 Log.Error("MissingManifestResourceException occurs. " + e.Message);
120 catch (MissingSatelliteAssemblyException e)
122 Log.Error("MissingSateliteAssemblyException occurs. " + e.Message);
128 private global::System.Resources.ResourceManager GetResourceManager(string path, string baseName)
130 global::System.Resources.ResourceManager resourceManager = null;
132 if (string.IsNullOrEmpty(path))
137 if (!File.Exists(path))
139 Log.Warn(path + " does not exist");
143 #pragma warning disable CA1031
146 Assembly assembly = Assembly.Load(File.ReadAllBytes(path));
147 if (assembly != null)
149 resourceManager = new global::System.Resources.ResourceManager(baseName, assembly);
150 if (resourceManager == null)
152 Log.Error("Failed to create ResourceManager");
157 catch (ArgumentNullException e)
159 Log.Error("ArgumentNullException occurs. " + e.Message);
161 catch (BadImageFormatException e)
163 Log.Error("BadImageFormatException occurs. " + e.Message);
167 Log.Error("Exception occurs. " + e.Message);
169 #pragma warning restore CA1031
171 return resourceManager;
175 private global::System.Resources.ResourceManager GetResourceManager(string locale)
177 global::System.Resources.ResourceManager resourceManager;
179 if (_resourceMap.TryGetValue(locale, out resourceManager))
181 return resourceManager;
184 string baseName = _resourceClassName;
186 if (locale == "default")
188 path = _resourcePath + _resourceDll;
192 path = _resourcePath + locale + "/" + _resourceDll;
193 baseName += "." + locale;
196 resourceManager = GetResourceManager(path, baseName);
197 if (resourceManager != null)
199 _resourceMap.Add(locale, resourceManager);
202 return resourceManager;