Skip to content

Commit

Permalink
Completed locale overriding
Browse files Browse the repository at this point in the history
  • Loading branch information
rubo committed May 19, 2018
1 parent 5d787fe commit b0fd4e9
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 3 deletions.
4 changes: 2 additions & 2 deletions Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Ruben Buniatyan")]
[assembly: AssemblyProduct("MissinKit")]
[assembly: AssemblyCopyright("© 2016 Ruben Buniatyan. All rights reserved.")]
[assembly: AssemblyCopyright("© 2016 Ruben Buniatyan")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

Expand All @@ -31,5 +31,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyInformationalVersion("1.5.0-beta2")]
[assembly: AssemblyInformationalVersion("1.5.0")]
[assembly: AssemblyVersion("1.5.0.*")]
20 changes: 19 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,26 @@ DateTime datetime = nsdate.ToDateTime();
```
Not a big deal, but convenient.

#### iOS 11 Fallback
#### Changing Current Locale on the Fly
Since iOS doesn't provide an easy way to change `NSLocale.CurrentLocale` programmatically,
there's a utility to help with that.
```csharp
var date = new DateTime(2018, 1, 1).ToNSDate();
var formatter = new NSDateFormatter() { DateFormat = "EEEE" };

L10n.OverrideCurrentLocale("es_MX");

formatter.Locale = NSLocale.CurrentLocale;
Debug.WriteLine(formatter.StringFor(date)); // outputs 'lunes'
L10n.RestoreCurrentLocale();

formatter.Locale = NSLocale.CurrentLocale;
Debug.WriteLine(formatter.StringFor(date)); // outputs 'Monday'
```
Overriding or restoring the current locale triggers `NSCurrentLocaleDidChangeNotification`.

#### iOS 11 Fallback
Since iOS 11 introduced some breaking changes to UIView layout handling,
there are a few fallback extension methods to reduce boilerplate code required to handle those changes.

Expand Down
21 changes: 21 additions & 0 deletions Utilities/L10n.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ public static class L10n
private static NSLocale _locale;
#endregion

/// <summary>
/// Overrides <see cref="NSLocale.CurrentLocale"/> with a locale for the identifier specified.
/// </summary>
/// <param name="localeId">Locale identifier</param>
public static void OverrideCurrentLocale(string localeId)
{
if (string.CompareOrdinal(localeId, _locale?.Identifier) == 0)
Expand Down Expand Up @@ -50,8 +54,13 @@ public static void OverrideCurrentLocale(string localeId)
block.CleanupBlock();

IsOverridden = true;

NSNotificationCenter.DefaultCenter.PostNotificationName(NSLocale.CurrentLocaleDidChangeNotification, null);
}

/// <summary>
/// Restores <see cref="NSLocale.CurrentLocale"/> to its genuine value.
/// </summary>
public static void RestoreCurrentLocale()
{
if (!IsOverridden)
Expand All @@ -67,10 +76,22 @@ public static void RestoreCurrentLocale()
BundleForCurrentLocale = NSBundle.MainBundle;

IsOverridden = false;

NSNotificationCenter.DefaultCenter.PostNotificationName(NSLocale.CurrentLocaleDidChangeNotification, null);
}

/// <summary>
/// Returns an <see cref="NSBundle"/> object corresponding to the bundle directory for the overridden locale.
/// If the current locale is not overridden, returns <see cref="NSBundle.MainBundle"/>.
/// </summary>
/// <returns>
/// An <see cref="NSBundle"/> object corresponding to the bundle directory for the overridden locale.
/// </returns>
public static NSBundle BundleForCurrentLocale { get; private set; } = NSBundle.MainBundle;

/// <summary>
/// Gets a value indicating whether the current locale is overridden.
/// </summary>
public static bool IsOverridden { get; private set; }

#region Swizzling Methods
Expand Down

0 comments on commit b0fd4e9

Please sign in to comment.