Recently in the implementation of an SDK project, because to achieve multi-language switching, single-class library multi-language implementation is not many, so the collation is as follows.
1. Add Appresource.resx (English) and Appresource-zh-cn.resx
Suppose our default language is English, add these two files. Add the Usercenter_title field to each of the two resource files and assign it a value. Note The access modifier is set to public.
If you are implementing multiple themes, you can add resources such as pictures.
2. Add Localizedstrings Class
Inherit INotifyPropertyChanged and implement, the final code may be as follows
12345678910111213141516171819202122232425262728293031323334 |
public class AdeasygoLocalizedStrings : INotifyPropertyChanged
{
private static AppResource _localizedResources =
new AppResource();
public AppResource AdeasygoLocalizedResource
{
get
{
return _localizedResources;
}
set
{
_localizedResources = value;
NotifyPropertyChanged(
"AdeasygoLocalizedResource"
);
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged(
string propertyName)
{
if (
this
.PropertyChanged !=
null
)
{
this
.PropertyChanged(
this
,
new PropertyChangedEventArgs(propertyName));
}
}
}
|
3.Modify AppResource.Designer.cs
This step is important to add the set method of the ResourceManager property to support dynamic language switching:
1234567891011121314 |
/// <summary>
/// 返回此类使用的缓存的 ResourceManager 实例。
/// </summary>
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
public static global::System.Resources.ResourceManager ResourceManager {
get {
if (
object
.ReferenceEquals(resourceMan,
null
)) {
global::System.Resources.ResourceManager temp =
new global::System.Resources.ResourceManager(
"Adeasygo.Community.Sdk.Resources.AppResource"
,
typeof
(AppResource).Assembly);
resourceMan = temp;
}
return resourceMan;
}
set { resourceMan = value; }
}
|
4. Control Language (theme) toggle
1234567891011121314 |
public static void Init(
string lang =
"en"
)
{
ResourceManager resManager;
switch (lang)
{
case "zh"
:
resManager =
new ResourceManager(
"Sdk.Resources.AppResource-zh-CN"
, Assembly.Load(
"Sdk"
));
break
;
default
:
resManager =
new ResourceManager(
"Sdk.Resources.AppResource"
, Assembly.Load(
"Sdk"
));
break
;
}
AppResource.ResourceManager = resManager;
}
|
In this way, the dynamic language switching, image resources and so on in the class library can also be defined in the ResX, depending on the language change or multi-topic switching.
This article fixed link: http://www.liubaicai.net/index.php/archives/425
Welcome to visit: http://www.liubaicai.net/Find more
WP8 DLL Class library (SDK) for multilingual multi-theme