forked from unoplatform/uno
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(applicationlanguages): Implemented ApplicationLanguage.PrimaryLa…
…nguageOverride
- Loading branch information
1 parent
99e3663
commit 07bb5cc
Showing
4 changed files
with
93 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
using System; | ||
using System.Globalization; | ||
using System.Linq; | ||
using System.Reflection; | ||
using Uno.UI; | ||
|
||
namespace Windows.Globalization | ||
{ | ||
public static partial class ApplicationLanguages | ||
{ | ||
private static string _primaryLanguageOverride; | ||
|
||
static ApplicationLanguages() | ||
{ | ||
ApplyLanguages(); | ||
} | ||
|
||
public static string PrimaryLanguageOverride | ||
{ | ||
get => _primaryLanguageOverride; | ||
set | ||
{ | ||
_primaryLanguageOverride = value; | ||
ApplyLanguages(); | ||
} | ||
} | ||
|
||
public static global::System.Collections.Generic.IReadOnlyList<string> Languages | ||
{ | ||
get; | ||
private set; | ||
} | ||
|
||
#if !(__IOS__) | ||
public static global::System.Collections.Generic.IReadOnlyList<string> ManifestLanguages | ||
{ | ||
get; | ||
} = GetManifestLanguages(); | ||
|
||
|
||
private static string[] GetManifestLanguages() | ||
{ | ||
var languages = new[] | ||
{ | ||
#if __ANDROID__ | ||
#pragma warning disable CS0618 // Type or member is obsolete | ||
ContextHelper.Current.Resources.Configuration.Locale.ToLanguageTag(), | ||
#pragma warning restore CS0618 // Type or member is obsolete | ||
#endif | ||
CultureInfo.InstalledUICulture.Name, | ||
CultureInfo.CurrentUICulture.Name, | ||
CultureInfo.CurrentCulture.Name | ||
}; | ||
|
||
return languages | ||
.Where(l => !string.IsNullOrWhiteSpace(l)) | ||
.Distinct() | ||
.ToArray(); | ||
} | ||
#endif | ||
|
||
private static void ApplyLanguages() | ||
{ | ||
var overridenLanguage = PrimaryLanguageOverride; | ||
|
||
if (string.IsNullOrWhiteSpace(overridenLanguage)) | ||
{ | ||
Languages = ManifestLanguages; | ||
} | ||
else | ||
{ | ||
var manifestLanguages = ManifestLanguages.ToArray(); | ||
var languages = new string[ManifestLanguages.Count + 1]; | ||
languages[0] = overridenLanguage; | ||
if (manifestLanguages.Length > 0) | ||
{ | ||
Array.Copy(manifestLanguages, 0, languages, 1, manifestLanguages.Length); | ||
} | ||
|
||
Languages = languages.Distinct().ToArray(); | ||
} | ||
|
||
var primaryLanguage = Languages.First(); | ||
var primaryCulture = new CultureInfo(primaryLanguage); | ||
|
||
CultureInfo.CurrentCulture = primaryCulture; | ||
CultureInfo.CurrentUICulture = primaryCulture; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,11 @@ | ||
#if __IOS__ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Foundation; | ||
|
||
namespace Windows.Globalization | ||
{ | ||
public static partial class ApplicationLanguages | ||
{ | ||
public static IReadOnlyList<string> Languages => NSBundle.MainBundle.PreferredLocalizations; | ||
|
||
public static IReadOnlyList<string> ManifestLanguages => NSBundle.MainBundle.Localizations; | ||
public static IReadOnlyList<string> ManifestLanguages { get; } = NSBundle.MainBundle.PreferredLocalizations.Concat(NSBundle.MainBundle.Localizations).Distinct().ToArray(); | ||
} | ||
} | ||
#endif |