initial
This commit is contained in:
61
Billing.Shared/Languages/PlatformCulture.cs
Normal file
61
Billing.Shared/Languages/PlatformCulture.cs
Normal file
@@ -0,0 +1,61 @@
|
||||
namespace Billing.Languages
|
||||
{
|
||||
public partial class PlatformCulture
|
||||
{
|
||||
public string PlatformString { get; set; }
|
||||
public string LanguageCode { get; set; }
|
||||
public string LocaleCode { get; set; }
|
||||
|
||||
public string Language => string.IsNullOrEmpty(LocaleCode) ? LanguageCode : LanguageCode + "-" + LocaleCode;
|
||||
|
||||
public partial string GetNamespace();
|
||||
public partial void Init();
|
||||
|
||||
public PlatformCulture()
|
||||
{
|
||||
Init();
|
||||
}
|
||||
|
||||
private void Init(string cultureString)
|
||||
{
|
||||
if (string.IsNullOrEmpty(cultureString))
|
||||
{
|
||||
cultureString = "zh-CN";
|
||||
}
|
||||
|
||||
PlatformString = cultureString.Replace('_', '-');
|
||||
if (PlatformString.Contains('-'))
|
||||
{
|
||||
var parts = PlatformString.Split('-');
|
||||
LanguageCode = parts[0];
|
||||
LocaleCode = parts[^1];
|
||||
}
|
||||
else
|
||||
{
|
||||
LanguageCode = PlatformString;
|
||||
LocaleCode = string.Empty;
|
||||
}
|
||||
}
|
||||
|
||||
private string ToDotnetFallbackLanguage()
|
||||
{
|
||||
string netLanguage = LanguageCode switch
|
||||
{
|
||||
// fallback to Portuguese (Portugal)
|
||||
"pt" => "pt-PT",
|
||||
// equivalent to German (Switzerland) for this app
|
||||
"gsw" => "de-CH",
|
||||
|
||||
// add more application-specific cases here (if required)
|
||||
// ONLY use cultures that have been tested and known to work
|
||||
|
||||
// use the first part of the identifier (two chars, usually);
|
||||
_ => LanguageCode,
|
||||
};
|
||||
Helper.Debug($".NET Fallback Language/Locale: {LanguageCode} to {netLanguage} (application-specific)");
|
||||
return netLanguage;
|
||||
}
|
||||
|
||||
public override string ToString() => PlatformString;
|
||||
}
|
||||
}
|
95
Billing.Shared/Languages/Resource.cs
Normal file
95
Billing.Shared/Languages/Resource.cs
Normal file
@@ -0,0 +1,95 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Xml;
|
||||
using Xamarin.Forms;
|
||||
using Xamarin.Forms.Xaml;
|
||||
|
||||
namespace Billing.Languages
|
||||
{
|
||||
internal class Resource
|
||||
{
|
||||
public static string TitleDateFormat => Text(nameof(TitleDateFormat));
|
||||
|
||||
static readonly Dictionary<string, LanguageResource> dict = new();
|
||||
|
||||
public static string Text(string name, params object[] args)
|
||||
{
|
||||
string current = App.CurrentCulture.PlatformString;
|
||||
if (!dict.TryGetValue(current, out LanguageResource lang))
|
||||
{
|
||||
lang = new LanguageResource(App.CurrentCulture);
|
||||
dict.Add(current, 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?.TryGetValue(key, out string val) == true)
|
||||
{
|
||||
return val;
|
||||
}
|
||||
return key;
|
||||
}
|
||||
}
|
||||
|
||||
public LanguageResource(PlatformCulture lang)
|
||||
{
|
||||
try
|
||||
{
|
||||
var ns = lang.GetNamespace();
|
||||
var langId = $"{ns}.Languages.{lang.Language}.xml";
|
||||
var langCodeId = $"{ns}.Languages.{lang.LanguageCode}.xml";
|
||||
var assembly = typeof(LanguageResource).Assembly;
|
||||
var name = assembly.GetManifestResourceNames().FirstOrDefault(n =>
|
||||
string.Equals(n, langId, StringComparison.OrdinalIgnoreCase) ||
|
||||
string.Equals(n, langCodeId, StringComparison.OrdinalIgnoreCase));
|
||||
if (name == null)
|
||||
{
|
||||
name = $"{ns}.Languages.zh-CN.xml";
|
||||
}
|
||||
var xml = new XmlDocument();
|
||||
using (var stream = assembly.GetManifestResourceStream(name))
|
||||
{
|
||||
xml.Load(stream);
|
||||
}
|
||||
strings = new Dictionary<string, string>();
|
||||
foreach (XmlElement ele in xml.DocumentElement)
|
||||
{
|
||||
strings[ele.Name] = ele.InnerText;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Helper.Error("language.ctor", $"failed to load json resource: {lang}, {ex.Message}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[ContentProperty(nameof(Text))]
|
||||
public class TextExtension : IMarkupExtension
|
||||
{
|
||||
public string Text { get; set; }
|
||||
|
||||
public object ProvideValue(IServiceProvider serviceProvider)
|
||||
{
|
||||
if (Text == null)
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
return Resource.Text(Text);
|
||||
}
|
||||
}
|
||||
}
|
16
Billing.Shared/Languages/en.xml
Normal file
16
Billing.Shared/Languages/en.xml
Normal file
@@ -0,0 +1,16 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<root>
|
||||
<Accounts>Accounts</Accounts>
|
||||
<Bills>Bills</Bills>
|
||||
<Settings>Settings</Settings>
|
||||
<Sunday>Su</Sunday>
|
||||
<Monday>Mo</Monday>
|
||||
<Tuesday>Tu</Tuesday>
|
||||
<Wednesday>We</Wednesday>
|
||||
<Thursday>Th</Thursday>
|
||||
<Friday>Fr</Friday>
|
||||
<Saturday>Sa</Saturday>
|
||||
<NoRecords>Bills not yet generated</NoRecords>
|
||||
<Memo>Click here to record</Memo>
|
||||
<TitleDateFormat>MM/dd/yyyy</TitleDateFormat>
|
||||
</root>
|
16
Billing.Shared/Languages/zh-CN.xml
Normal file
16
Billing.Shared/Languages/zh-CN.xml
Normal file
@@ -0,0 +1,16 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<root>
|
||||
<Accounts>账户</Accounts>
|
||||
<Bills>账单</Bills>
|
||||
<Settings>设置</Settings>
|
||||
<Sunday>周日</Sunday>
|
||||
<Monday>周一</Monday>
|
||||
<Tuesday>周二</Tuesday>
|
||||
<Wednesday>周三</Wednesday>
|
||||
<Thursday>周四</Thursday>
|
||||
<Friday>周五</Friday>
|
||||
<Saturday>周六</Saturday>
|
||||
<NoRecords>还未产生账单</NoRecords>
|
||||
<Memo>点此记录</Memo>
|
||||
<TitleDateFormat>yyyy年MM月dd日</TitleDateFormat>
|
||||
</root>
|
Reference in New Issue
Block a user