Localization in Swift 2
Asked Answered
Z

3

10

I want to localize my application; I am using Swift 2. I followed this tutorial but I experience 2 issues.

1. Localized image disappears for both languages

I localized multiple images the same way for German and English. One set of images (the en and de versions of an image) disappear for both languages. I added them, they are in the project's folder and I can not spot any difference to other images I localized.

I tried

  • cleaning and running the project (no errors)
  • removed the images and added them again
  • removed and added the localization

…nothing helped. Any idea?

2. Error w/ NSLocalizedString & Localizable.strings

I created a Localizable.strings for localizing my app's strings. Here is how I make them localizable:

button.setTitle(NSLocalizedString("buttonTitle", comment: "MyButton"), forState: .Normal)

In the Localizable.strings for English I added:

"buttonTitle" = "MyButton"

…and for German:

"buttonTitle" = "MeinButton"

Now, Xcode does not show any errors in code but it says

Read failed:
The data couldn't be read because it isn't in the correct format.


Any ideas?
Thank you in advance :)


Edit

Issue #2 was resolved, I missed a ; there. Thanks to codingVoldemort!

Zymo answered 11/6, 2015 at 8:40 Comment(2)
For change language "on the fly" you can use cocoapods.org/pods/L10n-swift.Thibodeau
Cool, thanks. I'll try that! @AdrianBobrowskiZymo
P
9
  1. I think you have missed the semi-colon on the Localizable.strings after each key-value pair.
Purpleness answered 11/6, 2015 at 8:45 Comment(2)
Damn :( Thanks, thought I don't need it in swift. Any idea for the first issue?Zymo
indeed ; and name of string file should not be missed ! here is a sample library if you wish to use localization with out changing system language of phone - github.com/pr0gramm3r8hai/DGLocalizationEclogite
H
2

In the Localizable.strings for English and German you have missed the semi-colon at the end of statement.

It should be like :

"buttonTitle" = "MyButton"; and

"buttonTitle" = "MeinButton";

You can refer Apple docs for this.

Screenshot

Hyperesthesia answered 6/8, 2016 at 16:39 Comment(1)
Thanks for your answer. The second issue - which you addressed here - was solved already though.Zymo
E
0

I'm a bit late, but if you're still searching for a solution for task #1:

You could use the solutions presented in that tutorial; there's a Internationalization of Images section.

"After selecting [your image] in Project navigator, click Localize button under File Inspector. This should provide you with the option to Localize the file in English and German."

That being said, I'm unsure whether this would work on Swift 2 (if you're still on there); but that should be possible with an up-to-date Xcode/IDE.

Embay answered 23/5, 2019 at 18:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.