If you're developping SharePoint 2013 Apps, may be like me you are interested about best solutions to localize your apps.
First, I recommend to read the following MSDN link : How to Localize apps for SharePoint: http://msdn.microsoft.com/en-us/library/fp179919(v=office.15).aspx
This article presents some things to consider to create apps with different languages.
Please note that I will speak about the localization of SharePoint hosted apps only.
For this kind of apps, you must use resources files with Javascript. With "classical" old SharePoint dev models, you used Resources.resx files with C# wrapper to get values. But with Apps, this kind of files could not be use anymore in your developments because you can not write server side code.
So the MSDN article give you a technicle to write localized pages.
This technicle is simple :
The MSDN article preconize the following code :
In the ASPX (or HTML) page, include Javascript with the following pattern:
<script language='javascript' src='Resources.<%$Resources:wss,language_value%>.js'></script>
After JS inclusion, you can use the localized labels contained in the JS file to map it on a HTML control. For example:
<h1 id="boo2">This is the invariant language title.</h1><script language="javascript">boo2.innerText = homePageTitle;</script>
Ok, this technicle seems to work, but in my opinion, this is not the best way to do that.
This method has a lot of disadvantages :
For all these reasons, I propose to you another method based on Javascript and JQuery to do the same thing.
First, I recommend to add scripts references to JQuery and to a custom Resources.js file. This file will be responsible for including the good resources JS file.
<script type="text/javascript" src="../Scripts/jquery-1.6.4.min.js"></script>
<script type="text/javascript" src="../Scripts/Resources.js"></script>
Now, create two resources files, called for example Resources.en-us.js and Resources.fr-fr.js. Your solution should be something like that:
Open in edit mode the file called Resources.en-us.js, and declare in this file all your English labels with JS vars, like for example:
Now edit your Resources.fr-fr.js file, and declare the same variables, but with french values, like for example :
OK, now open in edit mode the Resources.js file. This file must contain all the logical to load the good Resources.XX-XX.js file. I propose the following code to do that:
Now in your page, the adapted resources file is loaded in the current language. So you need to use javascript code to translate labels Inside the page.
I propose a JQuery method to do that, based on html tag id attribute. For example, if you want to localized a h2 tag content:
With this method, you can translate page title too for example: