You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

60 lines
1.9 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Resources;
using System.Text;
using System.Threading;
namespace MaterialTraceabilityUI.Resources
{
public class LanguageManager : INotifyPropertyChanged
{
/// <summary>
/// 资源
/// </summary>
private readonly ResourceManager _resourceManager;
/// <summary>
/// 懒加载
/// </summary>
private static readonly Lazy<LanguageManager> _lazy = new Lazy<LanguageManager>(() => new LanguageManager());
public static LanguageManager Instance => _lazy.Value;
public event PropertyChangedEventHandler PropertyChanged;
public LanguageManager()
{
//获取此命名空间下Resources的Lang的资源Lang可以修改
_resourceManager = new ResourceManager("MaterialTraceabilityUI.Resources.Lang", typeof(LanguageManager).Assembly);
}
/// <summary>
/// 索引器的写法,传入字符串的下标
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
/// <exception cref="ArgumentNullException"></exception>
public string this[string name]
{
get
{
if (name == null)
{
throw new ArgumentNullException(nameof(name));
}
return _resourceManager.GetString(name);
}
}
public void ChangeLanguage(CultureInfo cultureInfo)
{ // 获取当前线程
var currentThread = Thread.CurrentThread;
currentThread.CurrentCulture = cultureInfo;
currentThread.CurrentUICulture = cultureInfo;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("item[]")); //字符串集合,对应资源的值
}
}
}