When developing apps for Windows 8.1, Windows Phone 8.1 or Universal Windows apps, the recommended approach for app localization is to use .resw resource files located in Strings/[culture code]/Resources.resw path. This approach is quite similar to the previous .resx localization system used in .NET/Silverlight apps with one big difference – there is no generated code-behind file now that you can use for compiletime-safe access of localized strings. If you want to get the string named “ListHeader”, you need to access it like this:
var loader = new Windows.ApplicationModel.Resources.ResourceLoader(); var str = loader.GetString("ListHeader"); |
As you can see this is not exactly convenient.
Simple solution for this problem is to use the ResW File Code Generator extension for Visual Studio. All you need is download the ReswCodeGen.VSPackage.vsix file and install it.
Now when you open properties of any .resw file in your app and enter ReswFileCodeGenerator in the Custom Tool field, the code behind file will be created automatically. It’s also possible to define the namespace of the generated class using the Custom Tool Namespace field.
Now it’s possible to access the string ListHeader like this:
var str = Vlak.Resources.AppResources.ListHeader; |
Not just this is much shorter to use, it’s also safer. If you now rename the property to ListTitle or delete the string from resources, the app won’t compile, which is good, because you know immediately that the string is not in resources anymore. With the original approach using loader.GetString the app would launch but throw an exception when accessing the no longer present string.
ResW File Code Generator in Visual Studio 2015
Installing the addon to Visual Studio 2012 or Visual Studio 2013 is supported out of the box. The problem, is when you want to install this addon to Visual Studio 2015 – it won’t work using the available installer. Update, new version of ResW File Code Generator for Visual Studio 2015 was released just day after my article, strange coincidence 🙂
Luckily there is a simple fix for this:
- Download the ReswCodeGen.VSPackage.vsix addon, rename it to ReswCodeGen.VSPackage.zip and extract it as a zip file.
- Open the extension.vsixmanifest file in any text editor.
- Change the line
InstallationTarget Version=”[11.0,12.0]”
to
InstallationTarget Version=”[14.0]”
and save the file. - Pack again the content of the folder as a zip archive and name it ReswCodeGen.VSPackage.vsix
- Now if you double click the new ReswCodeGen.VSPackage.vsix installer, it will install as expected into Visual Studio 2015 just like the original installer in Visual Studio 2013.
That’s all, simple solution for better productivity when developing localized Windows Store apps that you can start using right now even in Visual Studio 2105!