Skip to content

Instantly share code, notes, and snippets.

@artem-zinnatullin
Created December 20, 2013 22:28
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save artem-zinnatullin/8062647 to your computer and use it in GitHub Desktop.
Save artem-zinnatullin/8062647 to your computer and use it in GitHub Desktop.
Changing windows phone app localization at runtime via bindings!
public class App : Application
{
// ... bla bla
// when app is launching, we should set its language to previous saved or best for user's system language or default for application
private void Application_Launching(object sender, LaunchingEventArgs e)
{
LocalizationManager.ChangeAppLanguage(LocalizationManager.GetCurrentAppLang());
}
}
/// <summary>
/// Handles localization switching in the app
/// </summary>
public static class LocalizationManager
{
public class LocalizationChangedEventArgs : EventArgs
{
public string NewLocalization { get; private set; }
public LocalizationChangedEventArgs(string newLocalization)
{
NewLocalization = newLocalization;
}
}
public static event EventHandler<LocalizationChangedEventArgs> LocalizationChanged;
public static class SupportedLanguages
{
public const string En = "en";
public const string Ru = "ru";
public static readonly string[] All =
{
En,
Ru
};
}
private static readonly object LocalizationManagerLock = new object();
/// <summary>
/// Gets current language on device
/// </summary>
/// <returns></returns>
public static string GetCurrentAppLang()
{
lock (LocalizationManagerLock)
{
// please implement this part as you want, here you can see, that we just getting previous saved language from IsolatedStorageSettings via our helper
var savedAppLang = IsolatedStorageSettingsManager.Get(IsolatedStorageSettingsKeys.AppLang) as string;
if (savedAppLang != null) return savedAppLang;
var currentCulture = Thread.CurrentThread.CurrentUICulture.TwoLetterISOLanguageName;
// returning language from current UI culture only if it is supported by our app, otherwise reutrning EN as default
foreach (var lang in SupportedLanguages.All.Where(lang => lang == currentCulture)) return lang;
return SupportedLanguages.En;
}
}
/// <summary>
/// Changes current app language, updates all views and saves current language
/// if language is not supported, it will not switch to it
/// </summary>
/// <param name="lang">Use values from SupportedLanguages class</param>
public static void ChangeAppLanguage(string lang)
{
lock (LocalizationManagerLock)
{
// Can not switch to not supported language
if (!SupportedLanguages.All.Contains(lang)) return;
Thread.CurrentThread.CurrentUICulture = new CultureInfo(lang);
LocalizedStrings.LocalizedStringsResource.UpdateLanguage();
IsolatedStorageSettingsManager.Put(IsolatedStorageSettingsKeys.AppLang, lang); // saving new language in IsolatedStorageSettings
// notifying binded views about language change, so they will reload resources automatically
NotifyLocalizationChanged(lang);
}
}
/// <summary>
/// Resets current App language to the system language
/// </summary>
public static void ResetAppLanguageToTheSystemLanguage()
{
ChangeAppLanguage(Thread.CurrentThread.CurrentCulture.TwoLetterISOLanguageName);
}
private static void NotifyLocalizationChanged(string newLocalization)
{
var localizationChanged = LocalizationChanged;
if (localizationChanged != null)
{
LocalizationChanged(null, new LocalizationChangedEventArgs(newLocalization));
}
}
}
public class LocalizedStrings : INotifyPropertyChanged
{
/// We need to provide ability to change app language at runtime
/// So we need to notify views which had binded string resource about change
/// </summary>
public event PropertyChangedEventHandler PropertyChanged;
private static AppResources _localizedResources = new AppResources();
public AppResources LocalizedResources
{
get { return _localizedResources; }
}
/// <summary>
/// Call this method using LocalizedStrings.LocalizedStringsResource.UpdateLanguage()
/// When you had switched current ui language for the application and want to notify your views about that
/// </summary>
public void UpdateLanguage()
{
// To prevent NPE, if another thread modify event handler after check
var handler = PropertyChanged;
if (handler != null) handler(LocalizedResources, new PropertyChangedEventArgs(null));
}
public static LocalizedStrings LocalizedStringsResource
{
get
{
return Application.Current.Resources["LocalizedStrings"] as LocalizedStrings;
}
}
}
@stepos01
Copy link

Nice man
Thanks for this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment