The Portable Class Library (PCL) is the recommended solution for sharing code between Windows Store and Windows Phone 8 applications. The previous blog I walked through the setup of the projects but I didn’t realize that my favorite tool, Multilingual App Toolkit doesn’t seem to be available in the PCL, and the PCL is where I have setup the Resources.resx file that provides PortableStrings.
I did a bit of research and it appears several people are asking similar questions but there wasn’t any answers.
The bad news
At this point resource files are handled differently on the two platforms. Windows Phone and the PCL use .resx files, and Windows Store uses .resw. This isn’t an issue if all strings come from the PCL which is how we setup our projects.
In the PCL project the Multilingual App Toolkit isn’t available.
The good news
Seems like we have painted ourselves into a corner where none of the code sharing techniques seem to apply. You cannot share a file that isn’t the same, and we cannot setup our handy translation tool in the PCL library.
It is 4 steps:
- Since the PCL and Windows 8 use .resx files, we can link to the ‘PCL’ .resx file for strings in our Windows 8 Project.
- Enable the Multilingual App Toolkit in the Windows Phone 8. Add a language and ‘Build’ or ‘ReBuild’ to generate a translation file for that language. Note that the .resx file that is generated is a link and actually exists in the PCL Resources folder.
- Include all language .resx files that were created in the PCL project.
- Enable the Multilingual App Toolkit in the Windows Store application and enable it for the languages it supports.
It can be done! It is a little ‘tricky’ and there are compromises that I will cover next.
I’m open to suggestions so leave comments. I offer an approach for those who see the need and are looking for a working pattern
Remove LocalizedStrings from the project. Exclude the class file from the project, and edit the App.xaml file. This eliminates some confusion by removing unused code.
Add the PCL ‘Strings.resx’ as a Linked file resource.
The AppResources.resx file in the Windows Phone 8 project shouldn’t be left in place. You must copy the values into ‘Strings.resx’ before removing this from the WP8 project. InitializeLanguage() method in App.xaml.cs, which will also need to be changed, and then remove the ‘using’ references that generate errors.
Enable Multilingual App Toolkit on the Windows Phone 8 project
Add a translation language by right clicking the WP8 project and selecting ‘Add translation languages…’.
Select the Portable Class Library project and ‘Show All Files’ and ‘Refresh’ to see the files created in the previous step actually exist in the Linked file folder. This is important because you must include this file in the PCL manually. We added Italian, so the ‘Strings.it.resx file is the one that needs to be included.
Now from within the WP8 project ‘
At this point is it important to understand that ALL localization is done in the WP8 project. You must Build or ReBuild the WP8 application after making changes to Strings.resx so fresh .resx files are created from the .xlf content in the PCL project but this will only work in the WP8 project.
Within the Win8 project Enable the Multilingual App Toolkit and Add the Italian language. This creates the ‘MultilingualResources’ directory with an .xlf file for each language. These will never be populated. I don’t know of any other way to notify the Win8 application that it has a localization file for a given language and this seemed to work.
The projects have a common resource file for handling translations. Ideally the Multilingual App Toolkit should work in the Portable Class Library but it doesn’t and even if it did I suspect there would still be manual steps. Here are the compromises to this approach.
- You must maintain resource strings for multiple languages in the Windows Phone 8 project. While natural for working on the Phone application it adds additional complication when working on the Win8 project.
- You must manually include the .resx file in the PCM project after adding a new language.
- You must add the language to both the WP8 and Win8 projects.
- You must Build or Rebuild the WP8 project after making a change to Strings even if the change only affects the Win8 version.
- Since there is only one Strings resource it must contain all strings for both platforms.
The big advantage is that many differences in the scripting between the two platforms related to applying localization to controls is addressed by moving the localization into the PCL. This by itself increases true code sharing and maintaining a single file for translation makes it all worth the compromises.
Simply stated, the benefits make it easy to overlook the cons!