Matt demonstrates the proper usage of the NSLocalizedString macro for basic strings. Internationalizing basic text is one of the most common tasks you perform as you prepare your app for localization.
- [Instructor] Internationalizing your basic user facing strings is going to be one of the most common and time consuming tasks that you'll need to do in order to internationalize your app. So I'm going to demonstrate to you the NSLocalizeString macro, and that's what iOS uses to identify strings that need to be localized. So when you use the NSLocalizeString macro, iOS is going to retrieve the string for the key that you provide from the appropriate strings file, and recall that I've already included Spanish, and Portuguese, and French strings files in this project for you, and even though you're not going to have any of those strings files for your app until you internationalize everything, I've included them for these lessons just so that we can demonstrate exactly how everything works.
Now I've already wrapped most of the text in this app in the NSLocalizeString macro, but there are still a few places where I have hardcoded user facing text that we need to change. The first place that I'm going to look at is here inside the ProductManager class. I'm going to minimize this file tree, just give us a little more space. But inside this ProductManager class, all of the names of the products, and the product categories, and all of their descriptions are all defined, and you can see, if I look down here, if you look at this array of category short titles, you can see, as an example, I've already wrapped most of the text in the NSLocalizeString macro.
If you look at this CategoryTitles array, you can see that this still has hardcoded text in it, and I'm going to go ahead and wrap each one of these in the NSLocalizeString macro. So I'm just going to cut this out of here, localize string, I'm going to post that string right back in there, and then the other parameter is a comment. It's actually very important and it's for the benefit of your translator, and it gives them context about what the string means, because they're not going to be running your app. They won't necessarily know how this text is being used, and it just gives them some insight into what it means.
So I'm just going to add a comment for this string, and this just lets the translator know exactly what you're referring to with this string. I'm going to go ahead and do the same thing here for the next strings. I'm going to cut that out, use my localize string macro, copy that back in, and add an appropriate comment for that string.
One more time here, I'm going to do this for the men's active wear. Cut that string out, use my localize string macro, copy the key back in, and add a comment. Now I'm going to quickly show you what this looks like when I run it in a different language, and you're going to be able to see that these three titles that I have just localized will appear in a different language whereas the active wear for the women is still going to be be in English, because it's a hardcoded string.
If I look at my products, here are my product category titles. Notice the first three are all localized whereas Active Wear Women is still in English, so I'm going to go ahead and just make sure that that gets localized as well. I'm going to cut that out, use my macro, and add an appropriate comment. So the last thing I'm also going to demonstrate to you, this file is a Swift file obviously.
You can use the same exact macro for Objective-C files, just has a slightly different syntax. So now I want to take a look at the product detail controller, which is the view controller for this product detail view right here, and there's a couple strings in this class, specifically this quantity label right here, and the title for this Place Order button. Right now, those are still hardcoded strings.
So let me collapse this file tree here for a second, and I'm going to go ahead and do the same thing here in Objective-C. I'm going to use the NSLocalizeString macro to localize this text. So I'm just going to cut that out for a second, use the localized string macro, and the syntax is very close to the same, just slightly different since this is Objective-C. Now I'm going to add an appropriate comment for this label, and one more time down here at line 60, where we set the title for that Place Order button, I need to cut that out and use my localized string macro.
Now I'm going to add an appropriate comment for that button title as well. Now I'm going to go ahead and run that for you in the French language, and you'll be able to see that both of those two pieces of text are appropriately localized as well . And there you go. So now that you know how to use the NSLocalizeString macro, you're going to want to go through all of the code in your project and use it to internationalize all of your basic user facing text.
- Internationalization vs. localization
- Internationalizing strings, dates, and measurements
- Localizing storyboards and nibs
- Exporting XLIFF files for translation
- Importing translated XLIFF files
- Testing localization