I have a solution with a Windows Store app project and a Class Library project and I want to add Localization support.
How can I add the all the Resource files to my Class Library and use them in both my App and Class Library?
In order to avoid writing a lot of boilerplate and error prone code when you add a new resource string like:
- Add it to .resw file
- Add it to your Static class that provides access to the resource
- Add it to each language specific .resw file (ex: en, fr, pt, etc)
You can use the following approach:
- Create "Strings" folder and add there just one folder for default language (for example "en-US") in your Class Library
- Add Resources.resw file to "en-US" folder with required keys/values
- Install Multilingual App Toolkit
- Enable MAT in VS for your Class Library (VS->Tools->Enable Multilingual App Toolkit)
- Add required languages to your Class Library (VS->Project->Add Translation languages...)
- Install ResW File Code Generator VS extension
- Go to Resources.resw file properties and set Custom Tool to "ReswFileCodeGenerator" (you can also specify namespace in Custom Tool Namespace)
- To solve issue with supported languages detection(currently generated manifest contains supported languages according to folder structure "Strings/en-US") you need to add folders for all required languages in your App library ("fr-FR", "bg-BG", etc) and put Resources.resw file with only one fake key.
- Build your solution and enjoy!
With this approach all your resources are available via static class generated by ReswFileCodeGenerator and all of them work with x:uid in XAML. You don't need to care about keys synchronization between different languages. Also MAT can translate your resources for you.
Ok, I found how to do this and with a sample project found here
Basically the implementation is the following:
- In the ClassLibrary create a folder named "Strings"
- Inside the Strings folder create one for each language (ex: en, fr, pt, etc)
- And add a Resources.resw in each of those folders with your keys/values
Now add a new Class in your ClassLibrary that has the following code(adapted to your project):
using System;
using Windows.ApplicationModel.Resources;
namespace MyClassLibraryName.Tools {
public static class LocalizationTool {
static ResourceLoader resourceLoader = null;
public static string MyStringOne {
get {
String name;
GetLibraryName("MyStringOne", out name);
return name;
}
}
private static void GetLibraryName(string resourceName, out string resourceValue) {
if(resourceLoader == null) {
resourceLoader = ResourceLoader.GetForCurrentView("MyClassLibraryName/Resources");
}
resourceValue = resourceLoader.GetString(resourceName);
}
}
}
And in your ClassLibrary or MainApp just call the following:
string text = LocalizationTool.MyStringOne;
来源:https://stackoverflow.com/questions/26180245/class-library-localization-in-windows-universal-store-app