23.01.2018 Views

MICROSOFT_PRESS_EBOOK_PROGRAMMING_WINDOWS_8_APPS_WITH_HTML_CSS_AND_JAVASCRIPT_PDF

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

I highly recommend that you organize your entries by source file like this, and you can also use<br />

multiple resource files if you like, as explained in the next section. Also be careful about how you reuse<br />

the same string that occurs in multiple places. If it’s for the same kind of UI with the same intent, that’s<br />

fine, but if the usage context is different it’s better to duplicate the string as they might translate<br />

differently in other languages. In the resources above, notice how I also included a comment for<br />

location_formatShort because the word “At” by itself probably needs more context to translate properly.<br />

With the strings separated as resources, we can now use the resource loader to obtain those strings<br />

at run time. This can be done in two ways. First is with the WinRT APIs directly, namely<br />

Windows.ApplicationModel.Resources.ResourceLoader.getString:<br />

var loader = new Windows.ApplicationModel.Resources.ResourceLoader();<br />

var text = loader.getString('location_formatShort');<br />

or, more simply, with the WinJS.Resources.getString wrapper:<br />

var text = WinJS.Resources.getString('location_formatShort').value;<br />

that also happens to work in the web context where WinRT isn’t defined (see Scenario 12 of the<br />

Application resources and localization sample.) Note that getString returns an object that includes a<br />

value property with the string along with lang and an empty flag indicating if the resource wasn’t found.<br />

The WinJS method, being one line, is clearly helpful in cases like our settings commands because we<br />

can call it inline. Thus, in our code we just replace the string literals with the WinJS call, such as the<br />

following in pages/home/home.js:<br />

data.properties.title = WinJS.Resources.getString('about_command').value;<br />

and the following inside the object for the Settings commands:<br />

"about": { title: WinJS.Resources.getString('about_command').value,<br />

href: "/html/about.html" },<br />

Note that WinJS, being optimized for common scenarios, supports loading strings in the user’s<br />

default language only. The WinRT ResourceLoader class, on the other hand, is much more flexible and<br />

can load a string for any specific language. You’ll need to use that API when your requirements exceed<br />

what WinJS provides.<br />

And that’s really it for JavaScript. If you’ve made these changes to your app, now is a good time to<br />

use the Build > Build Solution command in Visual Studio. This will compile the resources.resjson file into<br />

a more efficient binary format called resources.pri, ignoring entries that begin with an underscore.<br />

Doing an occasional build (without string the app) is a good practice when working with resources so<br />

that you can clean up any problems in your files, such as duplicate entries or syntax errors. Then you can<br />

run the app to see the resource loader in action—mostly by seeing no difference from the app as it was<br />

before! Be sure to test all the code paths that were affected, however, to ensure that all the strings are<br />

being loaded properly.<br />

808

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!