Skip to content

Commit

Permalink
feat(applicationlanguages): Implemented ApplicationLanguage.PrimaryLa…
Browse files Browse the repository at this point in the history
…nguageOverride
  • Loading branch information
carldebilly committed Feb 25, 2021
1 parent 99e3663 commit 07bb5cc
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,40 +7,6 @@ namespace Windows.Globalization
#endif
public partial class ApplicationLanguages
{
#if __ANDROID__ || __IOS__ || NET461 || __WASM__ || __SKIA__ || __NETSTD_REFERENCE__ || __MACOS__
[global::Uno.NotImplemented("__ANDROID__", "__IOS__", "NET461", "__WASM__", "__SKIA__", "__NETSTD_REFERENCE__", "__MACOS__")]
public static string PrimaryLanguageOverride
{
get
{
throw new global::System.NotImplementedException("The member string ApplicationLanguages.PrimaryLanguageOverride is not implemented in Uno.");
}
set
{
global::Windows.Foundation.Metadata.ApiInformation.TryRaiseNotImplemented("Windows.Globalization.ApplicationLanguages", "string ApplicationLanguages.PrimaryLanguageOverride");
}
}
#endif
#if false || false || NET461 || __WASM__ || __SKIA__ || __NETSTD_REFERENCE__ || __MACOS__
[global::Uno.NotImplemented("NET461", "__WASM__", "__SKIA__", "__NETSTD_REFERENCE__", "__MACOS__")]
public static global::System.Collections.Generic.IReadOnlyList<string> Languages
{
get
{
throw new global::System.NotImplementedException("The member IReadOnlyList<string> ApplicationLanguages.Languages is not implemented in Uno.");
}
}
#endif
#if false || false || NET461 || __WASM__ || __SKIA__ || __NETSTD_REFERENCE__ || __MACOS__
[global::Uno.NotImplemented("NET461", "__WASM__", "__SKIA__", "__NETSTD_REFERENCE__", "__MACOS__")]
public static global::System.Collections.Generic.IReadOnlyList<string> ManifestLanguages
{
get
{
throw new global::System.NotImplementedException("The member IReadOnlyList<string> ApplicationLanguages.ManifestLanguages is not implemented in Uno.");
}
}
#endif
#if __ANDROID__ || __IOS__ || NET461 || __WASM__ || __SKIA__ || __NETSTD_REFERENCE__ || __MACOS__
[global::Uno.NotImplemented("__ANDROID__", "__IOS__", "NET461", "__WASM__", "__SKIA__", "__NETSTD_REFERENCE__", "__MACOS__")]
public static global::System.Collections.Generic.IReadOnlyList<string> GetLanguagesForUser( global::Windows.System.User user)
Expand Down
19 changes: 0 additions & 19 deletions src/Uno.UWP/Globalization/ApplicationLanguages.Android.cs

This file was deleted.

90 changes: 90 additions & 0 deletions src/Uno.UWP/Globalization/ApplicationLanguages.cs
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;
}
}
}
11 changes: 3 additions & 8 deletions src/Uno.UWP/Globalization/ApplicationLanguages.iOS.cs
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

0 comments on commit 07bb5cc

Please sign in to comment.