Xcode 10 localization - ios

I am trying to add localization on my project. Right now I have my main project, and all the text in the Main Storyboard and the .swift classes is in language Norwegian. I have another project where everything is in English, but I want to only use one project with both languages, so I can have one app only on the app store. I have about 20 different classes, and some classes contain multiple arrays with over 400 strings each. How exactly do I add English language to my existing project?

According to Apple guidelines, the default language of your app will be based on the user device default language settings.
To add localization to your xcode project, start by the adding the language you want from Project navigator -> Info -> Localizations tab and select the language you want to add. After selecting the language, a popup will appear to select the resource that you want to include in your localization:
You will notice that you will have a new .string file under your selected resource (for example, Main.storyboard) where you can do all the translation you want.
There are other topics like localizing strings from your code (for example, having a .string and an extension to your String) or image localization.

Related

What are the steps involved in localization in Xcode?

I am trying to localize my application with out storyBoard in my native language urdu (Pakistan). I watch many videos and links.
I am trying follwing steps:
First Add the language
then make a new String File named Localizable.string
Go to View > Utilities > Show File Inspector (⌥⌘1)
Locate Localization section and select "Localize..."
Choose base language (I chose English), and click the required language and then click the localize.
After that go to "Editscheme" select the Application language, there is no option to select the urdu(Pakistan) language.
Some one says go to project.pbxpro file and make changes to it. I tried to make changes to the file but I can't succeed.
Totally confused. What can I do?
I suggest you to create the Localizable file first, then add the language. This is because you don't have a storyboard, Xcode might not know what you want to localize when you try to add the language in the project settings.
So,
create the Localizable.strings file first, press "Localize" and select "Base"
Go to project settings' Localization section:
Select the language:
Now select Localizable.strings file to localize (in the picture I have other files as well, but you should only have Localizable.strings)
Now you should have two Localizable.strings files, one says "Base", one says "Urdu (Pakistan)". Write your localizations in the corresponding files!

Swift - How to use localisation from Shared Extension

How can I use container apps localized strings from shared extension in Swift. I have already added containers strings files into extension -> build phase -> compile sources but its still not working. Should I set anything in configuration? From main container app strings are translated but from shared extension it shows only the string keys/ids for texts.
You need to change target membership of the Localizable.strings file. Select Localizable.strings in the Project Navigator on the left, then open File Inspector on the right and add checkmark next to your extension target in the "Target Membership" field.
There are a couple things to check:
One is the solution offered here by #ovejka - click on your string
localization file and ensure, in the rightside settings pane, that
the file applies to your widget as well as your main app (target
membership).
Another is to ensure that your storyboard strings file, which should
have been created automatically when you added a second language to
your app, has the proper translations.
If neither of those work, given that a widget is quite small and
likely only has few labels, you could add a switch statement to 'viewWillAppear' to translate each label based on the detected language
Finally, none of those seemed to be working for me in the simulator or even by switching the schema language and running it on my actual phone.
It turns out, it was working all along and the simulated language wasn't applying to the widget. I turned my actual phone's language to Spanish, run the new version of my app, and the widget was translated.

Different storyboard localization per target

I have a project with 3 targets. Each target needs to suppport only one language, but the language is not the same for each target.
Currently I have
Target 1 (es)
Target 2 (es)
Target 3 (pt)
Most of my strings are in a Localizable.strings file, and I simply target a different file for each target. However, a number of strings are in my storyboards. My problem is that it does not seem possible to create different storyboard localization files for each target. If I change the targeting of the portuguese file, the targeting of the spanish file changes automatically.
A possible solution is to duplicate each Storyboard for each target, but this won't scale well as the number of tagets grow. I could also modify all the text with NSLocalizedString in each ViewController, but this seems tedious and error prone.
Is there a better solution?
Shared Storyboard across targets
While this answer is the preferred approach, you can assign different Storyboard localizations to different targets, while sharing the same Storyboard across all targets.
Prerequisites:
Follow this instructions in this answer.
Concept:
You want to have the Main.storyboard in each target, but a different Main.strings in each target. Additionally, you do not want all localizations to be available in each target.
To achieve this, you must manipulate the .lproj directly, a process which is hidden when merely using the File Inspector.
Step by step:
Create all the pieces by following the steps in this answer.
From any target, remove the Main.storyboard. Of course, select Remove Reference to keep the storyboard around.
In the Finder, locate Base.lproj. Drag and drop Main.storyboard back to your project, and select every target.
In the File Inspector, ensure that the languages are not selected, and that you are still using Localizable Strings.
In the Finder again, locate en.lproj, es.lproj, fr.lproj, etc. Notice that they each contain a version of Main.strings. Drag these .lproj (the entire directories) back into your project. This time, do not select any target
One last time, in File inspector, associate each Main.strings to the desired target. Repeat for each .strings.
Conclusion
By decoupling the .lproj from the .storyboard in the Project Navigator, you can associate files and targets freely.
Demo:
See it at work using a French target, on a device with language set to Français in the Settings:
► Find this solution on GitHub and additional details on Swift Recipes.
Shared localizations across targets
Assuming you already have multiple languages in your project, in the Project Navigator, select your storyboard (say Main.storyboard)
In the File Inspector, under Localization, add English. Pick Localizable Strings which is the default. This will create a Main.string for each language, and share your Storyboard across all languages.
Merely clicking that checkbox in step 2. will add a Main.string (English) for that language, specifically for that Main.Storyboard. Pay special attention to the new hierarchy, these are not Localizable.strings but Main.strings, derived from Main.storyboard:
Localize in each language file to your heart content.
For example, starting with a UILabel which message was in Esperanto, it will appear in English when the device runs in English:
/* Class = "UILabel"; text = "Tiu mesaĝo estas en Esperanto"; ObjectID = "acC-pA-eMt"; */
"acC-pA-eMt.text" = "This message is in English";
Start here to add multiple languages to your project:
In the event you need to add more languages, follow these steps:
In the Project Navigator, select your Project
In the Project and Target List, select, once again, the Project
Select the Info tab
Under Localizations, keep Use Base Internationalization (unless your UI itself must change across languages)
Click +, pick a new language. In the dialog presented to you, select every Storyboard you want to have multiple localizations for.
If you missed Storyboards at Step 5. you can simply add a new language to that a storyboard by following the steps in Assuming you already have multiple languages.
All you need: to make common xcode workspace for your app, then to create a duplicates of main xcodeproj file for your set of languages and make different targets for all of them, also then you can set language for individual project settings in new xcodeproj's. Also you easy can tune build phase 'Copy Bundle Resourses' and others. I use this method. If you have a Xcode's crash during adding project in workspace - open project with Xcode, rename it and rename or duplicate targets (I think same names cause this issue).

Xcode-How i can add other language on localization window

I'm trying to create a localization file for strings. I added a file named Localizable.strings in project. But localization window does not appear in other languages.I can only choose some languages. What do I need to be able to select other languages. (Ex: Turkish,Polish...)
You can add a lot of languages in project properties. Look at the picture. Hope this help.

Set Base Language to Settings.Bundle iOS

How do i add base language in settings bundle.
In my project, i have added two .strings file (french and english) inside settings bundle. I could see the languages reflecting on my app under iPhone settings screen. But when i select different language other than above two languages, the last shown language is displaying. for eg, if i select french as my default language, it is showing french but when i select swedish as my language (i have no swedish language in project), it still shows french. But i need to show English as Base Language. This is working on Localizable.strings inside the project. But this is not reflecting on settings bundle.
Any Idea!
Base language is not a "default language". In fact, in iOS doesn't exist the concept of default language as you think. See the answer of this question.

Resources