Modify implementation
authorChangGyu Choi <uppletaste@gmail.com>
Thu, 25 May 2023 05:23:51 +0000 (14:23 +0900)
committerChangGyu Choi <changyu.choi@samsung.com>
Thu, 25 May 2023 05:33:46 +0000 (14:33 +0900)
Since LibIniParser.Load() is not expensive to call and the call
frequency of GetCultureName() method is low, there is no need to
preprocess initialization by applying Singleton pattern.

Signed-off-by: ChangGyu Choi <uppletaste@gmail.com>
src/Tizen.Applications.Common/Tizen.Applications/CultureInfoHelper.cs

index fa42c77..ba586cc 100755 (executable)
@@ -20,58 +20,34 @@ using System.Runtime.InteropServices;
 
 namespace Tizen.Applications
 {
-
     internal static class CultureInfoHelper
     {
-        private class CultureInfoManager
-        {
-            private static CultureInfoManager _inst;
-            private const string _pathCultureInfoIni = "/usr/share/dotnet.tizen/framework/i18n/CultureInfo.ini";
-            private static IntPtr _dictionary = IntPtr.Zero;
-
-            public static CultureInfoManager GetInst()
-            {
-                return _inst;
-            }
+        private const string _pathCultureInfoIni = "/usr/share/dotnet.tizen/framework/i18n/CultureInfo.ini";
+        private static bool _fileExists = File.Exists(_pathCultureInfoIni);
 
-            private CultureInfoManager()
+        public static string GetCultureName(string locale)
+        {
+            if (!_fileExists)
             {
-                if (File.Exists(_pathCultureInfoIni))
-                {
-                    _dictionary = Interop.LibIniParser.Load(_pathCultureInfoIni);
-                }
+                return string.Empty;
             }
 
-            ~CultureInfoManager()
+            IntPtr dictionary = Interop.LibIniParser.Load(_pathCultureInfoIni);
+            if (dictionary == IntPtr.Zero)
             {
-                if (_dictionary != IntPtr.Zero)
-                {
-                    Interop.LibIniParser.FreeDict(_dictionary);
-                }
+                return string.Empty;
             }
 
-            public string GetCultureName(string locale)
+            string cultureName = string.Empty;
+            string key = "CultureInfo:" + locale.ToLowerInvariant();
+            IntPtr value = Interop.LibIniParser.GetString(dictionary, key, IntPtr.Zero);
+            if (value != IntPtr.Zero)
             {
-                if (_dictionary == IntPtr.Zero)
-                {
-                    return string.Empty;
-                }
-
-                string cultureName = string.Empty;
-                string key = "CultureInfo:" + locale.ToLowerInvariant();
-                IntPtr value = Interop.LibIniParser.GetString(_dictionary, key, IntPtr.Zero);
-                if (value != IntPtr.Zero)
-                {
-                    cultureName = Marshal.PtrToStringAnsi(value);
-                }
-
-                return cultureName;
+                cultureName = Marshal.PtrToStringAnsi(value);
             }
-        }
 
-        public static string GetCultureName(string locale)
-        {
-            return CultureInfoManager.GetInst().GetCultureName(locale);
+            Interop.LibIniParser.FreeDict(dictionary);
+            return cultureName;
         }
     }
 }