I try to migrate an existing Universal Windows Class Library project to Uno Cross-Platform Library project. In Universal Windows library I have resource files (.resw) with strings for localization. I copied them to Uno Library project.
I have found the way to use string resources from the Uno library. But syntax is different for UWP and for Android or iOS. As the result I have to use platform-specific C# code and XAML markup. It should be one syntax for using resources from libraries the same for all platforms. And I'm looking for it.
Here is what I have now:
I have created demo solution to find out how to use localized strings in XAML and in code, both for resource files (.resw) stored in Uno App project and in Uno Library (link to demo solution on GitHub).
I have Uno Cross-Platform App project 'ReswUseApplication' with two resource files: 'Resources.resw' and 'Custom.resw'. And I store there two keys in each of them:
SomeLabel.Text
SomeText
I use next syntax to get string resource by key from this files:
XAML:
x:Uid="SomeLabel" //from Resources.resw
x:Uid="/Custom/SomeLabel" //from Custom.resw
In code:
var resourceLoader = ResourceLoader.GetForCurrentView();
DefaultText = resourceLoader.GetString("SomeText"); //from Resources.resw
CustomText = resourceLoader.GetString("/Custom/SomeText"); //from Custom.resw
It works for all platforms I have tested (UWP, Android, iOS)
Also I have Uno Cross-Platform Library project 'LibraryWithResw' with resource file 'Library.resw'. I couldn't create new (.resw) file in Uno library project, so I took an existing (.resw) file and pasted it. I couldn't mark it as PRIResource in Properties, so I had to include it as PRIResource writing by hands in library project file:
<ItemGroup>
<PRIResource Include="$(MSBuildThisFileDirectory)Resources\en\Library.resw" />
</ItemGroup>
Then in 'ReswUseApplication' for all needed heads (UWP, Android and iOS) I added reference to this library project.
I have found one syntax for UWP and the other one for Android and iOS.
UWP XAML:
x:Uid="ms-resource:///LibraryWithResw/Library/SomeLabel"
UWP in code:
var resourceLoader = ResourceLoader.GetForCurrentView("LibraryWithResw");
TextFromLibrary = resourceLoader.GetString("Library/SomeText");
or
var resourceLoader = ResourceLoader.GetForCurrentView("LibraryWithResw/Library");
TextFromLibrary = resourceLoader.GetString("SomeText");
Android, iOS XAML:
x:Uid="/Library/SomeLabel"
Android, iOS in code:
var resourceLoader = ResourceLoader.GetForCurrentView();
TextFromLibrary = resourceLoader.GetString("/Library/SomeText");
or
var resourceLoader = ResourceLoader.GetForCurrentView("Library");
TextFromLibrary = resourceLoader.GetString("SomeText");
As I found: for Android and iOS it doesn't matter, where resource files are located. The syntax is the same for resource files in current project or for files in the library. But in UWP, if project is not current, I have to indicate the project's name in which resource file is located.
Thanks for the question. This is indeed an Uno issue, and the support for ms-resource:///
is missing.
Here's the related Uno issue: https://github.com/unoplatform/uno/issues/3346