예제 #1
0
        private I18NLanguagesConfiguration LoadLanguagesConfiguration(IEnumerable <string> fileNames)
        {
            var returnValue = new I18NLanguagesConfiguration();

            foreach (var fileName in fileNames)
            {
                Debug.Console(1, this, "Loading language definition from file {0}", fileName);
                using (var reader = new JsonTextReader(File.OpenText(fileName)))
                {
                    var jt = JToken.Load(reader);

                    try
                    {
                        JsonConvert.PopulateObject(jt.ToString(), returnValue.LanguageDefinitions);
                    }
                    catch (Exception ex)
                    {
                        Debug.Console(0, this, "Error deserializing languages file: {0}", ex.Message);
                        Debug.Console(0, this, "Stack Trace: {0}", ex.StackTrace);
                    }
                }
            }

            return(returnValue);
        }
예제 #2
0
        public I18NUtility(string key, string name, I18NUtilityConfiguration config)
            : base(key, name)
        {
            _config = config;

            CurrentLanguageFeedback    = new StringFeedback(() => CurrentLanguageString);
            CurrentLanguageIntFeedback = _config.SortAlphabetically ? new IntFeedback(() => CurrentLanguageIndex + 1) : new IntFeedback(() => CurrentLanguageIndex);

            CurrentLanguageString = _config.DefaultLocale;

            AddPreActivationAction(() =>
            {
                var fileNames = FindLanguagesConfigurationFiles();

                if (fileNames == null)
                {
                    return;
                }

                _languagesConfig = LoadLanguagesConfiguration(fileNames);

                if (_config.SortAlphabetically)
                {
                    var sortedDict = new Dictionary <string, LanguageDefinition>();
                    foreach (var item in _languagesConfig.LanguageDefinitions.OrderBy(i => i.Value.FriendlyName))
                    {
                        sortedDict.Add(item.Key, item.Value);
                    }
                    _languagesConfig.LanguageDefinitions = sortedDict;
                }

                if (_languagesConfig != null)
                {
                    return;
                }

                Debug.Console(0, this, Debug.ErrorLogLevel.Error,
                              "Languages not deserialized correctly. Please check configuration file");
            });
        }