/// <summary>
        /// Construct localization object from json files
        /// </summary>
        /// <param name="jsonPath">Json file path</param>
        private void ConstructLocalizationObject(string jsonPath, CultureInfo currentCulture)
        {
            //be sure that localization is always initialized
            if (localization == null)
            {
                localization = new Dictionary <string, LocalizatedFormat>();
            }

            IEnumerable <string> myFiles = GetMatchingJsonFiles(jsonPath);

            foreach (string file in myFiles)
            {
                Dictionary <string, JsonLocalizationFormat> tempLocalization = JsonConvert.DeserializeObject <Dictionary <string, JsonLocalizationFormat> >(File.ReadAllText(file, _localizationOptions.Value.FileEncoding));
                if (tempLocalization == null)
                {
                    continue;
                }
                foreach (KeyValuePair <string, JsonLocalizationFormat> temp in tempLocalization)
                {
                    LocalizatedFormat localizedValue = GetLocalizedValue(currentCulture, temp);
                    if (!(localizedValue.Value is null))
                    {
                        if (!localization.ContainsKey(temp.Key))
                        {
                            localization.Add(temp.Key, localizedValue);
                        }
                        else if (localization[temp.Key].IsParent)
                        {
                            localization[temp.Key] = localizedValue;
                        }
                    }
                }
            }
        }
        public ConcurrentDictionary <string, LocalizatedFormat> ConstructLocalization(
            IEnumerable <string> myFiles, CultureInfo currentCulture, JsonLocalizationOptions options)
        {
            _options = options;

            foreach (string file in myFiles)
            {
                ConcurrentDictionary <string, JsonLocalizationFormat> tempLocalization =
                    LocalisationModeHelpers.ReadAndDeserializeFile <string, JsonLocalizationFormat>(file,
                                                                                                    options.FileEncoding);

                if (tempLocalization == null)
                {
                    continue;
                }

                foreach (KeyValuePair <string, JsonLocalizationFormat> temp in tempLocalization)
                {
                    LocalizatedFormat localizedValue = GetLocalizedValue(currentCulture, temp);
                    AddOrUpdateLocalizedValue <JsonLocalizationFormat>(localizedValue, temp);
                }
            }

            return(localization);
        }
 protected void AddOrUpdateLocalizedValue <T>(LocalizatedFormat localizedValue, KeyValuePair <string, T> temp)
 {
     if (!(localizedValue.Value is null))
     {
         if (!localization.ContainsKey(temp.Key))
         {
             localization.TryAdd(temp.Key, localizedValue);
         }
         else if (localization[temp.Key].IsParent)
         {
             localization[temp.Key] = localizedValue;
         }
     }
 }
Esempio n. 4
0
        private void AddValueToLocalization(JsonLocalizationOptions options, string file, bool isParent)
        {
            ConcurrentDictionary <string, string> tempLocalization =
                LocalisationModeHelpers.ReadAndDeserializeFile <string, string>(file, options.FileEncoding);

            if (tempLocalization == null)
            {
                return;
            }

            foreach (var temp in tempLocalization)
            {
                LocalizatedFormat localizedValue = GetLocalizedValue(temp, isParent);

                AddOrUpdateLocalizedValue(localizedValue, temp);
            }
        }
Esempio n. 5
0
        string GetString(string name, CultureInfo cultureInfo = null, bool shouldTryDefaultCulture = true)
        {
            if (name == null)
            {
                throw new ArgumentNullException(nameof(name));
            }

            if (cultureInfo == null)
            {
                cultureInfo = CultureInfo.CurrentUICulture;
            }

            LocalizatedFormat localizedValue = null;

            if (localization.TryGetValue(name, out localizedValue))
            {
                return(localizedValue.Value);
            }

            // if (!cultureInfo.Equals(_localizationOptions.Value.DefaultCulture) && !cultureInfo.Equals(cultureInfo.Parent))
            // {
            //     Console.Error.WriteLine($"{name} is using parent culture instead of current ui culture");
            //     //Try the parent culture
            //     return GetString(name, cultureInfo.Parent, shouldTryDefaultCulture);
            // }

            // if (shouldTryDefaultCulture && !cultureInfo.Equals(_localizationOptions.Value.DefaultCulture))
            // {
            //     Console.Error.WriteLine($"{name} is using default option culture instead of current ui culture");
            //     //Try the default culture
            //     return GetString(name, _localizationOptions.Value.DefaultCulture, false);
            // }

            //advert user that current name string does not
            //contains any translation
            Console.Error.WriteLine($"{name} does not contains any translation");
            return(null);
        }