コード例 #1
0
        internal static IEnumerable <LocalizedResource> GetLocalizationTable(string language)
        {
            List <LocalizedResource> resources = new List <LocalizedResource>();
            string sql = "SELECT * FROM localization.get_localization_table(@Language) WHERE COALESCE(key, '') != '';";

            if (ConnectionStringHelper.DBProvider == "sqlclient")
            {
                sql = "SELECT * FROM localization.get_localization_table(@Language) WHERE COALESCE([key], '') != ''";
                using (SqlConnection connection = new SqlConnection(ConnectionStringHelper.GetConnectionString()))
                {
                    using (SqlCommand command = new SqlCommand(sql, connection))
                    {
                        command.Parameters.AddWithValue("@Language", language);

                        using (SqlDataAdapter adapter = new SqlDataAdapter(command))
                        {
                            using (DataTable dataTable = new DataTable())
                            {
                                dataTable.Locale = CultureManager.GetCurrent();
                                adapter.Fill(dataTable);

                                if (dataTable.Rows.Count > 0)
                                {
                                    foreach (DataRow row in dataTable.Rows)
                                    {
                                        LocalizedResource resource = new LocalizedResource();

                                        resource.Id            = long.Parse(row["id"].ToString());
                                        resource.ResourceClass = row["resource_class"].ToString();
                                        resource.Key           = row["key"].ToString();
                                        resource.Original      = row["original"].ToString();
                                        resource.Translated    = row["translated"].ToString();

                                        resources.Add(resource);
                                    }
                                }
                            }
                        }
                    }
                }
                return(resources);
            }

            using (NpgsqlConnection connection = new NpgsqlConnection(ConnectionStringHelper.GetConnectionString()))
            {
                using (NpgsqlCommand command = new NpgsqlCommand(sql, connection))
                {
                    command.Parameters.AddWithValue("@Language", language);

                    using (NpgsqlDataAdapter adapter = new NpgsqlDataAdapter(command))
                    {
                        using (DataTable dataTable = new DataTable())
                        {
                            dataTable.Locale = CultureManager.GetCurrent();
                            adapter.Fill(dataTable);

                            if (dataTable.Rows.Count > 0)
                            {
                                foreach (DataRow row in dataTable.Rows)
                                {
                                    LocalizedResource resource = new LocalizedResource();

                                    resource.Id            = long.Parse(row["id"].ToString());
                                    resource.ResourceClass = row["resource_class"].ToString();
                                    resource.Key           = row["key"].ToString();
                                    resource.Original      = row["original"].ToString();
                                    resource.Translated    = row["translated"].ToString();

                                    resources.Add(resource);
                                }
                            }
                        }
                    }
                }
            }

            return(resources);
        }
コード例 #2
0
        public static string TryGetResourceFromCache(string resourceClass, string resourceKey, string cultureCode = null)
        {
            CultureInfo culture = CultureManager.GetCurrent();

            if (!string.IsNullOrWhiteSpace(cultureCode))
            {
                culture = new CultureInfo(cultureCode);
            }


            IDictionary <string, string> cache;
            var cacheItem = MemoryCache.Default.Get("Resources");

            if (cacheItem is CacheItem)
            {
                CacheItem item = (CacheItem)cacheItem;

                cache = (IDictionary <string, string>)item.Value;
            }
            else
            {
                cache = (IDictionary <string, string>)cacheItem;
            }


            if (cache == null || cache.Count.Equals(0))
            {
                InitializeResources();
                return(TryGetResourceFromCache(resourceClass, resourceKey, cultureCode));
            }

            string cacheKey = resourceClass + "." + culture.Name + "." + resourceKey;
            string result;

            cache.TryGetValue(cacheKey, out result);

            if (result != null)
            {
                return(result);
            }

            //Fall back to parent culture
            while (true)
            {
                if (!string.IsNullOrWhiteSpace(culture.Parent.Name))
                {
                    cacheKey = resourceClass + "." + culture.Parent.Name + "." + resourceKey;
                    cache.TryGetValue(cacheKey, out result);

                    if (result != null)
                    {
                        return(result);
                    }

                    culture = culture.Parent;
                    continue;
                }
                break;
            }

            //Fall back to invariant culture
            cacheKey = resourceClass + "." + resourceKey;
            cache.TryGetValue(cacheKey, out result);

            return(result);
        }