Example #1
0
        /// <summary>
        /// To Manually raise CurrentLanguageChanging and CurrentLanguageChanged events.
        /// Force events even if the current language didn't changed.
        /// </summary>
        public void RaiseLanguageChangeEvents()
        {
            var changingArgs = new CurrentLanguageChangingEventArgs(currentLanguage, currentLanguage);
            var changedArgs  = new CurrentLanguageChangedEventArgs(currentLanguage, currentLanguage);

            CurrentLanguageChanging?.Invoke(this, changingArgs);

            if (!changingArgs.Cancel)
            {
                CurrentLanguageChanged?.Invoke(this, changedArgs);
                NotifyPropertyChanged(nameof(CurrentLanguage));
            }
        }
Example #2
0
        /// <summary>
        /// Attempts to change the <see cref="CurrentLanguage"/> to a new language.
        /// </summary>
        /// <param name="newLanguage">The name of the language to change to.</param>
        /// <returns>True if the language was successfully changed; false if the language was already set to the <paramref name="newLanguage"/>
        /// or the <paramref name="newLanguage"/> does not exist or is invalid.</returns>
        public static bool TryChangeCurrentLanguage(string newLanguage)
        {
            if (string.IsNullOrEmpty(newLanguage))
            {
                Debug.Fail("Invalid newLanguage value.");
                return(false);
            }

            GameMessageCollection newLanguageCollection;

            // Try to create the GameMessageCollection for the new language
            try
            {
                newLanguageCollection = Create(newLanguage);
            }
            catch (Exception ex)
            {
                const string errmsg = "Failed to change language to `{0}`. Exception: {1}";
                if (log.IsErrorEnabled)
                {
                    log.ErrorFormat(errmsg, newLanguage, ex);
                }
                Debug.Fail(string.Format(errmsg, newLanguage, ex));
                return(false);
            }

            // Change to the new language
            GameMessageCollection oldLanguage;

            lock (_currentLanguageSync)
            {
                // Check if we are just changing to the same language
                if (_currentLanguage == newLanguageCollection)
                {
                    return(false);
                }

                oldLanguage      = _currentLanguage;
                _currentLanguage = newLanguageCollection;
            }

            // Raise the event
            if (CurrentLanguageChanged != null)
            {
                CurrentLanguageChanged.Raise(null, ValueChangedEventArgs.Create(oldLanguage, _currentLanguage));
            }

            return(true);
        }
Example #3
0
 private void Setting_CurrentLanguageChanged(object sender, LanguageChangedEventArgs e)
 => CurrentLanguageChanged?.Invoke(sender, e);