Swift - How to use localisation from Shared Extension - ios

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.

Related

Xcode 10 - Unable to Localize UINavigation Controller Title

I have an app that I want to translate to Spanish. The app is very simple and uses a basic UINavigation Controller. The title is set in the main story board:
To add Spanish I did the followings:
Added Spanish in the project Localizations
My first question to you all is why is showing 4 files in English and 3 Files in Spanish, when I actually have 2 files for English and 2 Files for Spanish
I enabled location to the view controller
I added string file to the project as well. This is for labels and dynamic text within the views, and this is working as expected.
Edited the Main.strings (Spanish)
When running the app, I edited the scheme, and set the application Language to Spanish.
When the app launched and got to the main screen, I still see Welcome instead of "Bienvenidos" What am I missing?
Other things you should know, when adding localization I enabled and disabled this feature a few times for the storyboard trying different options. Also, when testing the app, I change the settings language to Spanish, I deleted and installed the app multiple times as well.
I'm using Xcode 10.1, Project Format is Xcode 9.3-compatible, iOS deployment target is 12.1
Hope someone can help.
Hope this helps someone else. With Xcode 10 the recommended way to do any localization is by selecting your project and perform an export for localization, just go to Editor > Export For Localization
Once you exported the project, make any changes to the .xliff files, images, etc.
In the .xliff file, each translation should have a <source>, <target> and <note> tag. In my case, many tags are missing. I manually added them, did the translation and import the translation into my project.
To import the localization just select your project and go to Editor > Import Localizations. Now everything should work as expected.
This video from WWDC helped me resolve my issue
https://developer.apple.com/videos/play/wwdc2018/404/
Hope this helps
For your first question, you probably have also localized your LaunchScreen.storyboard file. So you should have 3 localized files in total, except that for your Main.storyboard you have checked all three checkboxes (Base, English and Spanish) and since English is your development language, it counts for two.
As for your second question, if you're running in the simulator, just editing the scheme should be fine. Since you told that you enabled and disabled localization for your project something might have broken with Xcode. I've been there as well when I tried to remove a localization. Try to clean you project (Product -> Clean Build Folder) before running the app or even start the project from the scratch (if possible) without this adding/removing flow. Hope this helps.

Localizing custom SiriKit intent

My Xcode is v10 and I'm developing a custom intent for SiriKit, targeting iOS 12.
In Xcode 10, custom intents are designed in .intentdefinition files, where you can define your own parameters for intent phrases as well as the title of the intent and Siri shortcut for that intent. Building a project with a .intentdefinition file makes Xcode automatically generate an INIntent derived class for that intent.
My issue is that in order to support other languages than English so that the intent's title is show in the system's language in General > Siri & Search when the user is able to manipulate shortcuts for custom intents, I need to provide localized intent title for my .intentdefinition file.
When I try to add another language for my .intentdefinition file in the File Inspector and then build the project I get:
duplicate output file '/Users/.../IntentDefinitionGenerated/Intents/MyCustomIntent.swift' on task: IntentDefinitionCodegen /Users/.../ru.lproj/Intents.intentdefinition
This is likely happening because Xcode sees multiple .intentdefinition files in .lproj folders and tries to generate a same named class for each of them.
How do I localize my .intentdefinition file for iOS 12?
Go to intentdefinition file -> File Inspector -> Select Localize -> Now select current language for your intentdefinition file.
This will convert your existing intentdefinition file for current language.
Now again select the same intentdefinitionfile, add other language -> it will create a new Intents.strings file where you can customize the text for other locales.
I had the same issue as #desmond-hume. In my case, I have an old xcode project that already contained some localizations before "Base" was available. The initial .intentdefinition file must be added as the Base localization. From there, further localization can be added and Xcode will add only a .strings file for each localization.
Without a base localization, Xcode adds a new .intentdefinition for each localization which causes the "multiple commands produce...." error.
You can add a Base localization under the main project settings via a checkbox.
Some old projects (like mine) may also run into this issue:
iOS - Using Base localization pane is always empty
It looks like you've already localized your Intent Definition file (since Xcode has put it into a ru.lproj folder) so the problem might be that you've two copies of the file in the "Compile Sources" build phase.
Go here:
Project -> App target -> Build Phases -> Compile Sources
and look for duplicate entries. If you find a duplicate, delete one of the copies
You can also try Editor > Validate Settings… to find duplicates
You can also try cleaning the build
I've recently Localised the intents for my App, however after the original localisation, I added another Intent.
In Xcode 10.2.1 at least that I've checked, there is an "Export for Localization" option when you select the top level of your project.
It will ask you to save the output.
When you investigate your output, you should see an .xcloc folder for EACH of the languages you support.
Dig down to the .xliff under "Localized Contents" and open it.
There you will find your treasure trove of the items and you're looking for and you're looking for items that don't have a 'target'
The engineers at Apple at dub dub showed me this, however I personally wasn't happy of this process, as you have to edit it, and then re-import it (assuming you send off your files to a translator and get an .xliff back.
The annoyance though with Intents is the fact that it uses arbitrary IDs and not something legible
I haven't found an easier way (yet) to locate the missing translations.
Hope this helps!
you must be Project->info checked "Use Base Internationalization"
Next use the top answer method to create

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).

Disable English localization with Xcode 5

I have searched related questions and tried many suggested methods but none seems to work.
I have a japanese-only App that when run on an english device gets UI items such as back buttons translated ("Back").
To achieve this I have:
Deleted the English from "Localizations" from my project and chose to delete localized files (was a single *.strings file).
Changed CFBundleDevelopmentRegion in my Info.plist from jp_JPto jp as I read it should match the name of the *.lproj folder.
Cleaned, deleted, closed Xcode, etc. But default UI items are still getting translated to english.
Also I get some weird behavior in Xcode 5 now:
Checking the "Use Base Internationalization" checkbook presents an empty "Choose files and reference language to create Base localization". Choosing either Cancel or Finish has no effect and nothing happens.
Clicking on the "+" button to add the Other > "Base (Base)" localization does nothing.
I thing that my problem seems to be that my Base localization is still somehow english but I can't find a way to change that. Any ideas?
I found that to truly remove English support I have to properly set the CFBundleDevelopmentRegion into a full valid region (e.g. ja_JP) and not just a language (just ja still resulted in English showing up).
When you have a valid region Xcode will show you the name of your country instead of the raw value in the Plist editor.
Seems like some modules, such as MFMailComposeViewController, get localized to English no matter what (tested on iOS 6 and 7).
I encountered the same issue. Here are the steps I did to get past them.
Localize your storyboard file.
Select your Storyboard file in Xcode.
In the “File Inspector”, under the “Localization” sub header, press the “Localize…” button.
A dialog appears that says “Do you want to localize this file? The file will be moved into the lproj folder for the following language.” It lists a language. For me it was “English” and I chose that. For you, I suspect it will be “Japanese”.
Press the “Localize” button.
If it warns you that your storyboard file has changed outside of the project, choose “Save anyway”. If you don't it deletes the storyboard from your project. This happened to me with a source controlled project, but not with a new empty project.
Go back to your project and check the “Use Base Internationalization” checkbox. Your storyboard should show up in the "Choose files and reference language to create Base localization" sheet.
If you go back to your storyboard, you will see “Base” as the checked localization. I had my language (English) show up as an option. When I checked it, a storyboard.strings file was created. Since my base was already in my preferred language, I didn’t need a strings file so I removed it. I presume that if I had another language, I could select that and get a storyboard.strings file for that language.

iOS localized images not showing up

I am localizing an app and while the process seems straight forward (I followed http://www.raywenderlich.com/2876/localization-tutorial-for-ios with minor adjustments for new Xcode interface), and worked for strings with no issues, I cannot get my localized images to show up.
In Xcode, I already have a set of images in the original language and clicked the "Localize..." button for each of them. Accepted the default selection of "English" and noted that all the images were moved into a new "en.lproj" folder. All these images are used by calling [UIImage imageNamed:#"image1"] within the code.
Then I collected all my localized, French versions of these images and placed them into a folder "fr.lproj" at the same level as the "en.lproj" Xcode created. File -> Add Files to "Project" and point it to that folder. At this point Xcode actually automatically detected that these are French localized files and the right side bar correctly identifies them as such when I select any of those files. Project info tab correctly shows the expected number of localized files for each language. Yet when I run the app with French language selected in the simulator, it shows me localized French text, but English versions of images.
Am I missing any steps that should have been performed, or should something be done differently?
This happens often due to caching. Make sure to
A) delete the app from the simulator. You can also "Reset content and settings" on the simulator, but that shouldn't normally be needed
B) run Project > Clean
Sometimes it is also necessary to
C) run Project > Clean Build Folder (use Ctrl key)
D) delete the Derived Data from your project. To do this, open Organizer and choose your Project. Sometimes you even have to do this manually, i.e. navigate in finder to the folder and delete all files and folders.
Usually, this should do it. Sometimes a little trick works as well to quickly refresh index and cache, especially precompiled headers - open the .pch file in your project, and "break" it (e.g. comment out a line) so that it fails to compile. This will trigger headers to recompile as well.
Hope this helps.
Image localization doesn't worked correctly for me when there are 2(or more) PREFERRED LANGUAGE ORDER in the simulator Settings.
I'm new to iOS localization so.. I don't know why it is working like that.
Anyways try to maintain only 1 PREFERRED LANGUAGE ORDER to resolve the issue.

Resources