97 lines
		
	
	
		
			3.2 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
			
		
		
	
	
			97 lines
		
	
	
		
			3.2 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
| 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
 | |
|     {
 | |
|         public static string Title => GetResource(nameof(Title));
 | |
|         public static string Ok => GetResource(nameof(Ok));
 | |
|         public static string SaveSuccess => GetResource(nameof(SaveSuccess));
 | |
| 
 | |
|         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);
 | |
|         }
 | |
|     }
 | |
| }
 |