feature: multi-language supported
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using Pixiview.Resources;
|
||||
using Pixiview.UI.Theme;
|
||||
using Pixiview.Utils;
|
||||
using Xamarin.Forms;
|
||||
@@ -11,16 +12,11 @@ namespace Pixiview
|
||||
{
|
||||
// public properties
|
||||
public static Theme CurrentTheme { get; private set; }
|
||||
public static PlatformCulture CurrentCulture { get; private set; }
|
||||
public static Dictionary<string, object> ExtraResources { get; private set; }
|
||||
|
||||
public App()
|
||||
private void InitResources(IEnvironmentService service)
|
||||
{
|
||||
InitResources();
|
||||
}
|
||||
|
||||
private void InitResources()
|
||||
{
|
||||
var service = DependencyService.Get<IEnvironmentService>();
|
||||
var p = service.GetEnvironment();
|
||||
|
||||
ExtraResources = new Dictionary<string, object>
|
||||
@@ -36,6 +32,13 @@ namespace Pixiview
|
||||
SetTheme(theme, true);
|
||||
}
|
||||
|
||||
private void InitLanguage(IEnvironmentService service)
|
||||
{
|
||||
var ci = service.GetCurrentCultureInfo();
|
||||
service.SetCultureInfo(ci);
|
||||
CurrentCulture = new PlatformCulture(ci.Name.ToLower());
|
||||
}
|
||||
|
||||
private void SetTheme(Theme theme, bool force = false)
|
||||
{
|
||||
if (force || theme != CurrentTheme)
|
||||
@@ -62,6 +65,10 @@ namespace Pixiview
|
||||
|
||||
protected override void OnStart()
|
||||
{
|
||||
var service = DependencyService.Get<IEnvironmentService>();
|
||||
InitResources(service);
|
||||
InitLanguage(service);
|
||||
|
||||
MainPage = UIFactory.CreateNavigationPage(new MainPage());
|
||||
}
|
||||
|
||||
|
@@ -5,13 +5,14 @@
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
xmlns:u="clr-namespace:Pixiview.UI"
|
||||
xmlns:util="clr-namespace:Pixiview.Utils"
|
||||
xmlns:r="clr-namespace:Pixiview.Resources"
|
||||
mc:Ignorable="d"
|
||||
x:Class="Pixiview.MainPage"
|
||||
BackgroundColor="{DynamicResource WindowColor}"
|
||||
OrientationChanged="Page_OrientationChanged"
|
||||
util:StatusBar.StatusBarStyle="{DynamicResource StatusBarStyle}">
|
||||
<NavigationPage.TitleView>
|
||||
<u:NavigationTitle Title="Follow"
|
||||
<u:NavigationTitle Title="{r:Text Follow}"
|
||||
IsLeftButtonVisible="True"
|
||||
LeftButtonClicked="NavigationTitle_LeftButtonClicked"
|
||||
RightButtonClicked="NavigationTitle_RightButtonClicked"/>
|
||||
|
@@ -19,5 +19,13 @@
|
||||
<Folder Include="UI\" />
|
||||
<Folder Include="Utils\" />
|
||||
<Folder Include="UI\Theme\" />
|
||||
<Folder Include="Resources\" />
|
||||
<Folder Include="Resources\Languages\" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Remove="Resources\Languages\zh-CN.xml" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<EmbeddedResource Include="Resources\Languages\zh-CN.xml" />
|
||||
</ItemGroup>
|
||||
</Project>
|
4
Pixiview/Resources/Languages/zh-CN.xml
Normal file
4
Pixiview/Resources/Languages/zh-CN.xml
Normal file
@@ -0,0 +1,4 @@
|
||||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<root>
|
||||
<Follow>已关注</Follow>
|
||||
</root>
|
43
Pixiview/Resources/PlatformCulture.cs
Normal file
43
Pixiview/Resources/PlatformCulture.cs
Normal file
@@ -0,0 +1,43 @@
|
||||
namespace Pixiview.Resources
|
||||
{
|
||||
public class PlatformCulture
|
||||
{
|
||||
public string PlatformString { get; private set; }
|
||||
public string LanguageCode { get; private set; }
|
||||
public string LocaleCode { get; private set; }
|
||||
|
||||
public string Language
|
||||
{
|
||||
get { return string.IsNullOrEmpty(LocaleCode) ? LanguageCode : LanguageCode + "-" + LocaleCode; }
|
||||
}
|
||||
|
||||
public PlatformCulture() : this(null) { }
|
||||
public PlatformCulture(string cultureString)
|
||||
{
|
||||
if (string.IsNullOrEmpty(cultureString))
|
||||
{
|
||||
//throw new ArgumentNullException(nameof(cultureString), "Expected culture identieifer");
|
||||
cultureString = "en";
|
||||
}
|
||||
|
||||
PlatformString = cultureString.Replace('_', '-');
|
||||
var index = PlatformString.IndexOf('-');
|
||||
if (index > 0)
|
||||
{
|
||||
var parts = PlatformString.Split('-');
|
||||
LanguageCode = parts[0];
|
||||
LocaleCode = parts[parts.Length - 1];
|
||||
}
|
||||
else
|
||||
{
|
||||
LanguageCode = PlatformString;
|
||||
LocaleCode = "";
|
||||
}
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return PlatformString;
|
||||
}
|
||||
}
|
||||
}
|
92
Pixiview/Resources/ResourceHelper.cs
Normal file
92
Pixiview/Resources/ResourceHelper.cs
Normal file
@@ -0,0 +1,92 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Xml.Linq;
|
||||
using Xamarin.Forms;
|
||||
using Xamarin.Forms.Xaml;
|
||||
|
||||
namespace Pixiview.Resources
|
||||
{
|
||||
public class ResourceHelper
|
||||
{
|
||||
static readonly Dictionary<string, LanguageResource> dict = new Dictionary<string, LanguageResource>();
|
||||
|
||||
public static string GetResource(string name, params object[] args)
|
||||
{
|
||||
if (!dict.TryGetValue(App.CurrentCulture.PlatformString, out LanguageResource lang))
|
||||
{
|
||||
lang = new LanguageResource(App.CurrentCulture);
|
||||
dict.Add(App.CurrentCulture.PlatformString, lang);
|
||||
}
|
||||
|
||||
if (args == null || args.Length == 0)
|
||||
{
|
||||
return lang[name];
|
||||
}
|
||||
return string.Format(lang[name], args);
|
||||
}
|
||||
|
||||
private class LanguageResource
|
||||
{
|
||||
private readonly Dictionary<string, string> strings;
|
||||
|
||||
public string this[string key]
|
||||
{
|
||||
get
|
||||
{
|
||||
if (strings != null && strings.TryGetValue(key, out string val))
|
||||
{
|
||||
return val;
|
||||
}
|
||||
return key;
|
||||
}
|
||||
}
|
||||
|
||||
public LanguageResource(PlatformCulture lang)
|
||||
{
|
||||
try
|
||||
{
|
||||
var assembly = IntrospectionExtensions.GetTypeInfo(typeof(LanguageResource)).Assembly;
|
||||
var names = assembly.GetManifestResourceNames();
|
||||
var name = names.FirstOrDefault(n
|
||||
=> string.Equals(n, $"Pixiview.Resources.Languages.{lang.Language}.xml", StringComparison.OrdinalIgnoreCase)
|
||||
|| string.Equals(n, $"Pixiview.Resources.Languages.{lang.LanguageCode}.xml", StringComparison.OrdinalIgnoreCase));
|
||||
if (name == null)
|
||||
{
|
||||
name = "Pixiview.Resources.Languages.zh-CN.xml";
|
||||
}
|
||||
XDocument xml;
|
||||
using (var stream = assembly.GetManifestResourceStream(name))
|
||||
{
|
||||
xml = XDocument.Load(stream);
|
||||
}
|
||||
strings = new Dictionary<string, string>();
|
||||
foreach (var ele in xml.Root.Elements())
|
||||
{
|
||||
strings[ele.Name.LocalName] = ele.Value;
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
// load failed
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[ContentProperty(nameof(Text))]
|
||||
public class TextExtension : IMarkupExtension
|
||||
{
|
||||
public string Text { get; set; }
|
||||
|
||||
public object ProvideValue(IServiceProvider serviceProvider)
|
||||
{
|
||||
if (Text == null)
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
return ResourceHelper.GetResource(Text);
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,12 +1,17 @@
|
||||
using Xamarin.Forms;
|
||||
using System.Globalization;
|
||||
using Xamarin.Forms;
|
||||
|
||||
namespace Pixiview.Utils
|
||||
{
|
||||
public interface IEnvironmentService
|
||||
{
|
||||
EnvironmentParameter GetEnvironment();
|
||||
|
||||
Theme GetApplicationTheme();
|
||||
void SetStatusBarColor(Color color);
|
||||
|
||||
CultureInfo GetCurrentCultureInfo();
|
||||
void SetCultureInfo(CultureInfo ci);
|
||||
}
|
||||
|
||||
public class EnvironmentParameter
|
||||
|
Reference in New Issue
Block a user