feature: multi-language supported

This commit is contained in:
2020-05-05 13:36:57 +08:00
parent adfac39596
commit 1bbfdb6477
12 changed files with 298 additions and 25 deletions

View File

@@ -0,0 +1,8 @@
// This file is used by Code Analysis to maintain SuppressMessage
// attributes that are applied to this project.
// Project-level suppressions either have no target or are given
// a specific target and scoped to a namespace, type, member, etc.
using System.Diagnostics.CodeAnalysis;
[assembly: SuppressMessage("Style", "IDE0066:Convert switch statement to expression", Justification = "<Pending>")]

View File

@@ -74,6 +74,7 @@
<Compile Include="Renderers\RoundLabelRenderer.cs" />
<Compile Include="Renderers\CardViewRenderer.cs" />
<Compile Include="Renderers\RoundImageRenderer.cs" />
<Compile Include="GlobalSuppressions.cs" />
</ItemGroup>
<ItemGroup>
<InterfaceDefinition Include="Resources\LaunchScreen.storyboard" />

View File

@@ -66,7 +66,6 @@ namespace Pixiview.iOS.Renderers
}
[SuppressMessage("Code Notifications", "XI0002:Notifies you from using newer Apple APIs when targeting an older OS version", Justification = "<Pending>")]
[SuppressMessage("Style", "IDE0066:Convert switch statement to expression", Justification = "<Pending>")]
private UIStatusBarStyle ConvertStyle(StatusBarStyles style)
{
switch (style)

View File

@@ -1,7 +1,5 @@
using CoreGraphics;
using Pixiview.iOS.Renderers;
using Pixiview.iOS.Renderers;
using Pixiview.UI;
using UIKit;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;

View File

@@ -1,5 +1,9 @@
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Threading;
using Foundation;
using Pixiview.iOS.Services;
using Pixiview.Resources;
using Pixiview.Utils;
using UIKit;
using Xamarin.Essentials;
@@ -10,6 +14,20 @@ namespace Pixiview.iOS.Services
{
public class EnvironmentService : IEnvironmentService
{
public EnvironmentParameter GetEnvironment()
{
return new EnvironmentParameter
{
IconLightFontFamily = "FontAwesome5Pro-Light",
IconRegularFontFamily = "FontAwesome5Pro-Regular",
IconSolidFontFamily = "FontAwesome5Pro-Solid",
IconLeft = "\uf104" // for android, it's "\uf060"
};
}
#region - Theme -
[SuppressMessage("Code Notifications", "XI0002:Notifies you from using newer Apple APIs when targeting an older OS version", Justification = "<Pending>")]
public Theme GetApplicationTheme()
{
@@ -36,21 +54,110 @@ namespace Pixiview.iOS.Services
}
}
public EnvironmentParameter GetEnvironment()
{
return new EnvironmentParameter
{
IconLightFontFamily = "FontAwesome5Pro-Light",
IconRegularFontFamily = "FontAwesome5Pro-Regular",
IconSolidFontFamily = "FontAwesome5Pro-Solid",
IconLeft = "\uf104" // for android, it's "\uf060"
};
}
public void SetStatusBarColor(Color color)
{
// nothing need to do
}
#endregion
#region - Culture Info -
public CultureInfo GetCurrentCultureInfo()
{
string lang;
if (NSLocale.PreferredLanguages.Length > 0)
{
var pref = NSLocale.PreferredLanguages[0];
lang = iOSToDotnetLanguage(pref);
}
else
{
lang = "zh-CN";
}
CultureInfo ci;
var platform = new PlatformCulture(lang);
try
{
ci = new CultureInfo(platform.Language);
}
catch (CultureNotFoundException e)
{
try
{
var fallback = ToDotnetFallbackLanguage(platform);
App.DebugPrint($"{lang} failed, trying {fallback} ({e.Message})");
ci = new CultureInfo(fallback);
}
catch (CultureNotFoundException e1)
{
App.DebugError("culture.get", $"{lang} couldn't be set, using 'zh-CN' ({e1.Message})");
ci = new CultureInfo("zh-CN");
}
}
return ci;
}
[SuppressMessage("Style", "IDE1006:Naming Styles", Justification = "<Pending>")]
string iOSToDotnetLanguage(string iOSLanguage)
{
string netLanguage;
//certain languages need to be converted to CultureInfo equivalent
switch (iOSLanguage)
{
case "ms-MY": // "Malaysian (Malaysia)" not supported .NET culture
case "ms-SG": // "Malaysian (Singapore)" not supported .NET culture
netLanguage = "ms"; // closest supported
break;
case "gsw-CH": // "Schwiizertüütsch (Swiss German)" not supported .NET culture
netLanguage = "de-CH"; // closest supported
break;
// add more application-specific cases here (if required)
// ONLY use cultures that have been tested and known to work
default:
netLanguage = iOSLanguage;
break;
}
App.DebugPrint($"iOS Language: {iOSLanguage}, .NET Language/Locale: {netLanguage}");
return netLanguage;
}
string ToDotnetFallbackLanguage(PlatformCulture platCulture)
{
string netLanguage;
switch (platCulture.LanguageCode)
{
//
case "pt":
netLanguage = "pt-PT"; // fallback to Portuguese (Portugal)
break;
case "gsw":
netLanguage = "de-CH"; // equivalent to German (Switzerland) for this app
break;
// add more application-specific cases here (if required)
// ONLY use cultures that have been tested and known to work
default:
netLanguage = platCulture.LanguageCode; // use the first part of the identifier (two chars, usually);
break;
}
App.DebugPrint($".NET Fallback Language/Locale: {platCulture.LanguageCode} to {netLanguage} (application-specific)");
return netLanguage;
}
public void SetCultureInfo(CultureInfo ci)
{
Thread.CurrentThread.CurrentCulture = ci;
Thread.CurrentThread.CurrentUICulture = ci;
App.DebugPrint($"CurrentCulture set: {ci.Name}");
}
#endregion
}
}