Label in storyboard don't take the right localization value The 2019 Stack Overflow Developer Survey Results Are In Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) The Ask Question Wizard is Live! Data science time! April 2019 and salary with experienceWeb Application Translation, methods and toolsXcode Localization is driving me nuts! Not loading StoryboardBest practice for localization and globalization of strings and labelsStoryboard + base localization + .strings file does not localize at runtimeBase localization + Storyboard + .strings -> no localizationBest practice multi language websiteLocalization for UITextView in storyboardReload storyboard to change localelocalizedStringForKey:value:table ignores Base localizationDon't understand how to localize plist of iOS react-native app
Does the AirPods case need to be around while listening via an iOS Device?
Mortgage adviser recommends a longer term than necessary combined with overpayments
How does this infinite series simplify to an integral?
Sort list of array linked objects by keys and values
Python - Fishing Simulator
system call string length limit
Was credit for the black hole image misattributed?
Does Parliament hold absolute power in the UK?
How is simplicity better than precision and clarity in prose?
Match Roman Numerals
Working through the single responsibility principle (SRP) in Python when calls are expensive
Am I ethically obligated to go into work on an off day if the reason is sudden?
How to politely respond to generic emails requesting a PhD/job in my lab? Without wasting too much time
How do I add random spotting to the same face in cycles?
Typeface like Times New Roman but with "tied" percent sign
Scientific Reports - Significant Figures
How does ice melt when immersed in water
Simulating Exploding Dice
University's motivation for having tenure-track positions
Semisimplicity of the category of coherent sheaves?
Arduino Pro Micro - switch off LEDs
Is above average number of years spent on PhD considered a red flag in future academia or industry positions?
RT6224D-based step down circuit yields 0V - why?
What was the last x86 CPU that did not have the x87 floating-point unit built in?
Label in storyboard don't take the right localization value
The 2019 Stack Overflow Developer Survey Results Are In
Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)
The Ask Question Wizard is Live!
Data science time! April 2019 and salary with experienceWeb Application Translation, methods and toolsXcode Localization is driving me nuts! Not loading StoryboardBest practice for localization and globalization of strings and labelsStoryboard + base localization + .strings file does not localize at runtimeBase localization + Storyboard + .strings -> no localizationBest practice multi language websiteLocalization for UITextView in storyboardReload storyboard to change localelocalizedStringForKey:value:table ignores Base localizationDon't understand how to localize plist of iOS react-native app
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I've built an application and now it's time to translate it to different languages. But some labels that I think I have localised don't get displayed in different languages with the different values.
Image one: So first, I've added the Swedish language inside
"Project -> Localizations"

Than I went inside the "LaunchScreen.storyboard" and edited the file.
This one:
Edited to this:
After that I made sure my launch screen supports the language like this:
And for the last step.. I edited the application language to Swedish like this:
But when I run it on the simulator, the text is not getting translated. It still remains the same text, and I've no idea how to solve it. Do you? Do not hesitate to help me out, I would really appreciate it!
So, I'm not sure if I've to add some code or whatever to make this work, but I followed a guide, and they don't. That's one of the reasons why I'm so confused. And thanks in advance!
BEFORE DISLIKE: At least give me a reason for it, so I can improve it in the future!
ios swift xcode localization storyboard
|
show 4 more comments
I've built an application and now it's time to translate it to different languages. But some labels that I think I have localised don't get displayed in different languages with the different values.
Image one: So first, I've added the Swedish language inside
"Project -> Localizations"

Than I went inside the "LaunchScreen.storyboard" and edited the file.
This one:
Edited to this:
After that I made sure my launch screen supports the language like this:
And for the last step.. I edited the application language to Swedish like this:
But when I run it on the simulator, the text is not getting translated. It still remains the same text, and I've no idea how to solve it. Do you? Do not hesitate to help me out, I would really appreciate it!
So, I'm not sure if I've to add some code or whatever to make this work, but I followed a guide, and they don't. That's one of the reasons why I'm so confused. And thanks in advance!
BEFORE DISLIKE: At least give me a reason for it, so I can improve it in the future!
ios swift xcode localization storyboard
It's your job to make translation for the language you have selected. The app won't automatically make the translation job for you.
– El Tomato
Mar 8 at 11:47
@ElTomato Nono, of course. But as you see I added "123123123123" just to try it out, but when I enter the application with the correct language it dosent change. It remains the normal string.
– Putte
Mar 8 at 11:47
Oh, I see. Sorry about that. Don't make the storyboard localizable, though. Use a localizable string file.
– El Tomato
Mar 8 at 11:50
@ElTomato How can I do that?
– Putte
Mar 8 at 11:50
Create a file titled Localizable. Then change the file type to strings. Put all translation strings in it.
– El Tomato
Mar 8 at 11:52
|
show 4 more comments
I've built an application and now it's time to translate it to different languages. But some labels that I think I have localised don't get displayed in different languages with the different values.
Image one: So first, I've added the Swedish language inside
"Project -> Localizations"

Than I went inside the "LaunchScreen.storyboard" and edited the file.
This one:
Edited to this:
After that I made sure my launch screen supports the language like this:
And for the last step.. I edited the application language to Swedish like this:
But when I run it on the simulator, the text is not getting translated. It still remains the same text, and I've no idea how to solve it. Do you? Do not hesitate to help me out, I would really appreciate it!
So, I'm not sure if I've to add some code or whatever to make this work, but I followed a guide, and they don't. That's one of the reasons why I'm so confused. And thanks in advance!
BEFORE DISLIKE: At least give me a reason for it, so I can improve it in the future!
ios swift xcode localization storyboard
I've built an application and now it's time to translate it to different languages. But some labels that I think I have localised don't get displayed in different languages with the different values.
Image one: So first, I've added the Swedish language inside
"Project -> Localizations"

Than I went inside the "LaunchScreen.storyboard" and edited the file.
This one:
Edited to this:
After that I made sure my launch screen supports the language like this:
And for the last step.. I edited the application language to Swedish like this:
But when I run it on the simulator, the text is not getting translated. It still remains the same text, and I've no idea how to solve it. Do you? Do not hesitate to help me out, I would really appreciate it!
So, I'm not sure if I've to add some code or whatever to make this work, but I followed a guide, and they don't. That's one of the reasons why I'm so confused. And thanks in advance!
BEFORE DISLIKE: At least give me a reason for it, so I can improve it in the future!
ios swift xcode localization storyboard
ios swift xcode localization storyboard
edited Mar 8 at 13:22
enwonder
375
375
asked Mar 8 at 11:36
PuttePutte
19710
19710
It's your job to make translation for the language you have selected. The app won't automatically make the translation job for you.
– El Tomato
Mar 8 at 11:47
@ElTomato Nono, of course. But as you see I added "123123123123" just to try it out, but when I enter the application with the correct language it dosent change. It remains the normal string.
– Putte
Mar 8 at 11:47
Oh, I see. Sorry about that. Don't make the storyboard localizable, though. Use a localizable string file.
– El Tomato
Mar 8 at 11:50
@ElTomato How can I do that?
– Putte
Mar 8 at 11:50
Create a file titled Localizable. Then change the file type to strings. Put all translation strings in it.
– El Tomato
Mar 8 at 11:52
|
show 4 more comments
It's your job to make translation for the language you have selected. The app won't automatically make the translation job for you.
– El Tomato
Mar 8 at 11:47
@ElTomato Nono, of course. But as you see I added "123123123123" just to try it out, but when I enter the application with the correct language it dosent change. It remains the normal string.
– Putte
Mar 8 at 11:47
Oh, I see. Sorry about that. Don't make the storyboard localizable, though. Use a localizable string file.
– El Tomato
Mar 8 at 11:50
@ElTomato How can I do that?
– Putte
Mar 8 at 11:50
Create a file titled Localizable. Then change the file type to strings. Put all translation strings in it.
– El Tomato
Mar 8 at 11:52
It's your job to make translation for the language you have selected. The app won't automatically make the translation job for you.
– El Tomato
Mar 8 at 11:47
It's your job to make translation for the language you have selected. The app won't automatically make the translation job for you.
– El Tomato
Mar 8 at 11:47
@ElTomato Nono, of course. But as you see I added "123123123123" just to try it out, but when I enter the application with the correct language it dosent change. It remains the normal string.
– Putte
Mar 8 at 11:47
@ElTomato Nono, of course. But as you see I added "123123123123" just to try it out, but when I enter the application with the correct language it dosent change. It remains the normal string.
– Putte
Mar 8 at 11:47
Oh, I see. Sorry about that. Don't make the storyboard localizable, though. Use a localizable string file.
– El Tomato
Mar 8 at 11:50
Oh, I see. Sorry about that. Don't make the storyboard localizable, though. Use a localizable string file.
– El Tomato
Mar 8 at 11:50
@ElTomato How can I do that?
– Putte
Mar 8 at 11:50
@ElTomato How can I do that?
– Putte
Mar 8 at 11:50
Create a file titled Localizable. Then change the file type to strings. Put all translation strings in it.
– El Tomato
Mar 8 at 11:52
Create a file titled Localizable. Then change the file type to strings. Put all translation strings in it.
– El Tomato
Mar 8 at 11:52
|
show 4 more comments
1 Answer
1
active
oldest
votes
Since storyboards elements ids changes and they are not managable to be used as "keys" in your localized "key-value" pair one better solution when localizing storyboards (UI) elements could be to provide a custom User Define Attribute defined as a Locale Key to be used for that UIView.
Then you can define in your Storyboard Attribute Inspector for a specific UI View an input field that will be filled with the localized key defined in your Localizable.strings files (one for English one will be for Swedish, both should have the same keys but with different values - in the English one will have english translations in values, and in Swedish one the opposite.
1) For example since you want to localize a UI View UILabel than you can have this in a swift file Localizable.swift for example (the code makes possible:
import UIKit
// MARK: Localizable
public protocol Localizable
var localized: String get
extension String: Localizable
public var localized: String
return NSLocalizedString(self, comment: "")
// MARK: XIBLocalizable
public protocol XIBLocalizable
var localeKey: String? get set
extension UILabel: XIBLocalizable
@IBInspectable public var localeKey: String?
get return nil
set(key)
text = key?.localized
2) You can uncheck your storyboard translations so you will endup having 1 storyboard without duplicating the storyboard into multiple translated version.
3) You can create now a Localizable.strings file containing your keys and translations and localize this file in order to have your translations (as you did with the storyboard, but instead of localizing the storyboard into multiple translated version, you will localize the Localizable.strings file (so you will see two files after you localize that in both languages, one will be called Localizable.strings (English) and the other Localizable.strings (Swedish).
Your Localizable.strings (English) could look like this for example:
"main_page_title_label" = "Main Page Title in English";
Your Localizable.strings (Swedish) could look like this for example:
"main_page_title_label" = "Huvudsida Titel på engelska";
4) Now go to your LaunchScreen.storyboard which you should have un-localized so you only have one version of it and not two like in your example pictures (English, Swedish). Look for the UILabel you want to localize and under the Attribute Inspector you will see a new input field called Locale Key here you can put as a value main_page_title_label. And now you have just localized a UILabel in your storyboard.
5) To test it from the simulator you have to change the language in Edit Scheme > Run > Options > Application Language and after you save it you can now run the app in the simulator and it will simulate that your simulator OS system will be set to Swedish language, so the label set with that key will show the Swedish value for that key.
Supporting more UI Views (UIButton, UITextField, UISearchBar..)
If you want to be able to localize more UI View, and not just UI View of type UILabel than you can add more support to the Localizable.swift file.
If you want to be able to localize also a UI View of type Button you can add this to Localizable.swift:
extension UIButton: XIBLocalizable
@IBInspectable public var localeKey: String?
get return nil
set(key)
setTitle(key?.localized, for: .normal)
To support localization in storyboards for UITextField and UISearchBar placeholder text add this to your Localizable.swift:
// MARK: Special protocol to localizaze UI's placeholder
public protocol UIPlaceholderXIBLocalizable
var localePlaceholderKey: String? get set
extension UITextField: UIPlaceholderXIBLocalizable
@IBInspectable public var localePlaceholderKey: String?
get return nil
set(key)
placeholder = key?.localized
extension UISearchBar: UIPlaceholderXIBLocalizable
@IBInspectable public var localePlaceholderKey: String?
get return nil
set(key)
placeholder = key?.localized
I'll try that out a bit later today, thanks a lot for your effort. Only one question: Do I need to go through all labels. (Since there is a loooooot) and add like: label1.text = NSLocalizedstring("", "") etc? or can I perform it all using one page somehow? Or do I manually has to add the NS-string to all labels and buttons? Thanks!
– Putte
Mar 8 at 13:57
The answer I gave is to be used to localize hard coded strings that are meant to be used in storyboards (UI) and you have to manually add the locale key throught the Attribute Inspector > Locale Key input field for each hard coded text in your UI View. For hard coded strings/text that you set programmatically through swift withmyview.text = ..you have can use as you said NSLocalizedString("your_key", "") for every hard coded string you wish to localize in multiple languages and as well add each different key in all your Localizable.strings version with their respective translated value
– denis_lor
Mar 8 at 14:03
@Putte Can you accept my answer in case you found it relevant to the question you made?
– denis_lor
Mar 10 at 9:41
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55062448%2flabel-in-storyboard-dont-take-the-right-localization-value%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Since storyboards elements ids changes and they are not managable to be used as "keys" in your localized "key-value" pair one better solution when localizing storyboards (UI) elements could be to provide a custom User Define Attribute defined as a Locale Key to be used for that UIView.
Then you can define in your Storyboard Attribute Inspector for a specific UI View an input field that will be filled with the localized key defined in your Localizable.strings files (one for English one will be for Swedish, both should have the same keys but with different values - in the English one will have english translations in values, and in Swedish one the opposite.
1) For example since you want to localize a UI View UILabel than you can have this in a swift file Localizable.swift for example (the code makes possible:
import UIKit
// MARK: Localizable
public protocol Localizable
var localized: String get
extension String: Localizable
public var localized: String
return NSLocalizedString(self, comment: "")
// MARK: XIBLocalizable
public protocol XIBLocalizable
var localeKey: String? get set
extension UILabel: XIBLocalizable
@IBInspectable public var localeKey: String?
get return nil
set(key)
text = key?.localized
2) You can uncheck your storyboard translations so you will endup having 1 storyboard without duplicating the storyboard into multiple translated version.
3) You can create now a Localizable.strings file containing your keys and translations and localize this file in order to have your translations (as you did with the storyboard, but instead of localizing the storyboard into multiple translated version, you will localize the Localizable.strings file (so you will see two files after you localize that in both languages, one will be called Localizable.strings (English) and the other Localizable.strings (Swedish).
Your Localizable.strings (English) could look like this for example:
"main_page_title_label" = "Main Page Title in English";
Your Localizable.strings (Swedish) could look like this for example:
"main_page_title_label" = "Huvudsida Titel på engelska";
4) Now go to your LaunchScreen.storyboard which you should have un-localized so you only have one version of it and not two like in your example pictures (English, Swedish). Look for the UILabel you want to localize and under the Attribute Inspector you will see a new input field called Locale Key here you can put as a value main_page_title_label. And now you have just localized a UILabel in your storyboard.
5) To test it from the simulator you have to change the language in Edit Scheme > Run > Options > Application Language and after you save it you can now run the app in the simulator and it will simulate that your simulator OS system will be set to Swedish language, so the label set with that key will show the Swedish value for that key.
Supporting more UI Views (UIButton, UITextField, UISearchBar..)
If you want to be able to localize more UI View, and not just UI View of type UILabel than you can add more support to the Localizable.swift file.
If you want to be able to localize also a UI View of type Button you can add this to Localizable.swift:
extension UIButton: XIBLocalizable
@IBInspectable public var localeKey: String?
get return nil
set(key)
setTitle(key?.localized, for: .normal)
To support localization in storyboards for UITextField and UISearchBar placeholder text add this to your Localizable.swift:
// MARK: Special protocol to localizaze UI's placeholder
public protocol UIPlaceholderXIBLocalizable
var localePlaceholderKey: String? get set
extension UITextField: UIPlaceholderXIBLocalizable
@IBInspectable public var localePlaceholderKey: String?
get return nil
set(key)
placeholder = key?.localized
extension UISearchBar: UIPlaceholderXIBLocalizable
@IBInspectable public var localePlaceholderKey: String?
get return nil
set(key)
placeholder = key?.localized
I'll try that out a bit later today, thanks a lot for your effort. Only one question: Do I need to go through all labels. (Since there is a loooooot) and add like: label1.text = NSLocalizedstring("", "") etc? or can I perform it all using one page somehow? Or do I manually has to add the NS-string to all labels and buttons? Thanks!
– Putte
Mar 8 at 13:57
The answer I gave is to be used to localize hard coded strings that are meant to be used in storyboards (UI) and you have to manually add the locale key throught the Attribute Inspector > Locale Key input field for each hard coded text in your UI View. For hard coded strings/text that you set programmatically through swift withmyview.text = ..you have can use as you said NSLocalizedString("your_key", "") for every hard coded string you wish to localize in multiple languages and as well add each different key in all your Localizable.strings version with their respective translated value
– denis_lor
Mar 8 at 14:03
@Putte Can you accept my answer in case you found it relevant to the question you made?
– denis_lor
Mar 10 at 9:41
add a comment |
Since storyboards elements ids changes and they are not managable to be used as "keys" in your localized "key-value" pair one better solution when localizing storyboards (UI) elements could be to provide a custom User Define Attribute defined as a Locale Key to be used for that UIView.
Then you can define in your Storyboard Attribute Inspector for a specific UI View an input field that will be filled with the localized key defined in your Localizable.strings files (one for English one will be for Swedish, both should have the same keys but with different values - in the English one will have english translations in values, and in Swedish one the opposite.
1) For example since you want to localize a UI View UILabel than you can have this in a swift file Localizable.swift for example (the code makes possible:
import UIKit
// MARK: Localizable
public protocol Localizable
var localized: String get
extension String: Localizable
public var localized: String
return NSLocalizedString(self, comment: "")
// MARK: XIBLocalizable
public protocol XIBLocalizable
var localeKey: String? get set
extension UILabel: XIBLocalizable
@IBInspectable public var localeKey: String?
get return nil
set(key)
text = key?.localized
2) You can uncheck your storyboard translations so you will endup having 1 storyboard without duplicating the storyboard into multiple translated version.
3) You can create now a Localizable.strings file containing your keys and translations and localize this file in order to have your translations (as you did with the storyboard, but instead of localizing the storyboard into multiple translated version, you will localize the Localizable.strings file (so you will see two files after you localize that in both languages, one will be called Localizable.strings (English) and the other Localizable.strings (Swedish).
Your Localizable.strings (English) could look like this for example:
"main_page_title_label" = "Main Page Title in English";
Your Localizable.strings (Swedish) could look like this for example:
"main_page_title_label" = "Huvudsida Titel på engelska";
4) Now go to your LaunchScreen.storyboard which you should have un-localized so you only have one version of it and not two like in your example pictures (English, Swedish). Look for the UILabel you want to localize and under the Attribute Inspector you will see a new input field called Locale Key here you can put as a value main_page_title_label. And now you have just localized a UILabel in your storyboard.
5) To test it from the simulator you have to change the language in Edit Scheme > Run > Options > Application Language and after you save it you can now run the app in the simulator and it will simulate that your simulator OS system will be set to Swedish language, so the label set with that key will show the Swedish value for that key.
Supporting more UI Views (UIButton, UITextField, UISearchBar..)
If you want to be able to localize more UI View, and not just UI View of type UILabel than you can add more support to the Localizable.swift file.
If you want to be able to localize also a UI View of type Button you can add this to Localizable.swift:
extension UIButton: XIBLocalizable
@IBInspectable public var localeKey: String?
get return nil
set(key)
setTitle(key?.localized, for: .normal)
To support localization in storyboards for UITextField and UISearchBar placeholder text add this to your Localizable.swift:
// MARK: Special protocol to localizaze UI's placeholder
public protocol UIPlaceholderXIBLocalizable
var localePlaceholderKey: String? get set
extension UITextField: UIPlaceholderXIBLocalizable
@IBInspectable public var localePlaceholderKey: String?
get return nil
set(key)
placeholder = key?.localized
extension UISearchBar: UIPlaceholderXIBLocalizable
@IBInspectable public var localePlaceholderKey: String?
get return nil
set(key)
placeholder = key?.localized
I'll try that out a bit later today, thanks a lot for your effort. Only one question: Do I need to go through all labels. (Since there is a loooooot) and add like: label1.text = NSLocalizedstring("", "") etc? or can I perform it all using one page somehow? Or do I manually has to add the NS-string to all labels and buttons? Thanks!
– Putte
Mar 8 at 13:57
The answer I gave is to be used to localize hard coded strings that are meant to be used in storyboards (UI) and you have to manually add the locale key throught the Attribute Inspector > Locale Key input field for each hard coded text in your UI View. For hard coded strings/text that you set programmatically through swift withmyview.text = ..you have can use as you said NSLocalizedString("your_key", "") for every hard coded string you wish to localize in multiple languages and as well add each different key in all your Localizable.strings version with their respective translated value
– denis_lor
Mar 8 at 14:03
@Putte Can you accept my answer in case you found it relevant to the question you made?
– denis_lor
Mar 10 at 9:41
add a comment |
Since storyboards elements ids changes and they are not managable to be used as "keys" in your localized "key-value" pair one better solution when localizing storyboards (UI) elements could be to provide a custom User Define Attribute defined as a Locale Key to be used for that UIView.
Then you can define in your Storyboard Attribute Inspector for a specific UI View an input field that will be filled with the localized key defined in your Localizable.strings files (one for English one will be for Swedish, both should have the same keys but with different values - in the English one will have english translations in values, and in Swedish one the opposite.
1) For example since you want to localize a UI View UILabel than you can have this in a swift file Localizable.swift for example (the code makes possible:
import UIKit
// MARK: Localizable
public protocol Localizable
var localized: String get
extension String: Localizable
public var localized: String
return NSLocalizedString(self, comment: "")
// MARK: XIBLocalizable
public protocol XIBLocalizable
var localeKey: String? get set
extension UILabel: XIBLocalizable
@IBInspectable public var localeKey: String?
get return nil
set(key)
text = key?.localized
2) You can uncheck your storyboard translations so you will endup having 1 storyboard without duplicating the storyboard into multiple translated version.
3) You can create now a Localizable.strings file containing your keys and translations and localize this file in order to have your translations (as you did with the storyboard, but instead of localizing the storyboard into multiple translated version, you will localize the Localizable.strings file (so you will see two files after you localize that in both languages, one will be called Localizable.strings (English) and the other Localizable.strings (Swedish).
Your Localizable.strings (English) could look like this for example:
"main_page_title_label" = "Main Page Title in English";
Your Localizable.strings (Swedish) could look like this for example:
"main_page_title_label" = "Huvudsida Titel på engelska";
4) Now go to your LaunchScreen.storyboard which you should have un-localized so you only have one version of it and not two like in your example pictures (English, Swedish). Look for the UILabel you want to localize and under the Attribute Inspector you will see a new input field called Locale Key here you can put as a value main_page_title_label. And now you have just localized a UILabel in your storyboard.
5) To test it from the simulator you have to change the language in Edit Scheme > Run > Options > Application Language and after you save it you can now run the app in the simulator and it will simulate that your simulator OS system will be set to Swedish language, so the label set with that key will show the Swedish value for that key.
Supporting more UI Views (UIButton, UITextField, UISearchBar..)
If you want to be able to localize more UI View, and not just UI View of type UILabel than you can add more support to the Localizable.swift file.
If you want to be able to localize also a UI View of type Button you can add this to Localizable.swift:
extension UIButton: XIBLocalizable
@IBInspectable public var localeKey: String?
get return nil
set(key)
setTitle(key?.localized, for: .normal)
To support localization in storyboards for UITextField and UISearchBar placeholder text add this to your Localizable.swift:
// MARK: Special protocol to localizaze UI's placeholder
public protocol UIPlaceholderXIBLocalizable
var localePlaceholderKey: String? get set
extension UITextField: UIPlaceholderXIBLocalizable
@IBInspectable public var localePlaceholderKey: String?
get return nil
set(key)
placeholder = key?.localized
extension UISearchBar: UIPlaceholderXIBLocalizable
@IBInspectable public var localePlaceholderKey: String?
get return nil
set(key)
placeholder = key?.localized
Since storyboards elements ids changes and they are not managable to be used as "keys" in your localized "key-value" pair one better solution when localizing storyboards (UI) elements could be to provide a custom User Define Attribute defined as a Locale Key to be used for that UIView.
Then you can define in your Storyboard Attribute Inspector for a specific UI View an input field that will be filled with the localized key defined in your Localizable.strings files (one for English one will be for Swedish, both should have the same keys but with different values - in the English one will have english translations in values, and in Swedish one the opposite.
1) For example since you want to localize a UI View UILabel than you can have this in a swift file Localizable.swift for example (the code makes possible:
import UIKit
// MARK: Localizable
public protocol Localizable
var localized: String get
extension String: Localizable
public var localized: String
return NSLocalizedString(self, comment: "")
// MARK: XIBLocalizable
public protocol XIBLocalizable
var localeKey: String? get set
extension UILabel: XIBLocalizable
@IBInspectable public var localeKey: String?
get return nil
set(key)
text = key?.localized
2) You can uncheck your storyboard translations so you will endup having 1 storyboard without duplicating the storyboard into multiple translated version.
3) You can create now a Localizable.strings file containing your keys and translations and localize this file in order to have your translations (as you did with the storyboard, but instead of localizing the storyboard into multiple translated version, you will localize the Localizable.strings file (so you will see two files after you localize that in both languages, one will be called Localizable.strings (English) and the other Localizable.strings (Swedish).
Your Localizable.strings (English) could look like this for example:
"main_page_title_label" = "Main Page Title in English";
Your Localizable.strings (Swedish) could look like this for example:
"main_page_title_label" = "Huvudsida Titel på engelska";
4) Now go to your LaunchScreen.storyboard which you should have un-localized so you only have one version of it and not two like in your example pictures (English, Swedish). Look for the UILabel you want to localize and under the Attribute Inspector you will see a new input field called Locale Key here you can put as a value main_page_title_label. And now you have just localized a UILabel in your storyboard.
5) To test it from the simulator you have to change the language in Edit Scheme > Run > Options > Application Language and after you save it you can now run the app in the simulator and it will simulate that your simulator OS system will be set to Swedish language, so the label set with that key will show the Swedish value for that key.
Supporting more UI Views (UIButton, UITextField, UISearchBar..)
If you want to be able to localize more UI View, and not just UI View of type UILabel than you can add more support to the Localizable.swift file.
If you want to be able to localize also a UI View of type Button you can add this to Localizable.swift:
extension UIButton: XIBLocalizable
@IBInspectable public var localeKey: String?
get return nil
set(key)
setTitle(key?.localized, for: .normal)
To support localization in storyboards for UITextField and UISearchBar placeholder text add this to your Localizable.swift:
// MARK: Special protocol to localizaze UI's placeholder
public protocol UIPlaceholderXIBLocalizable
var localePlaceholderKey: String? get set
extension UITextField: UIPlaceholderXIBLocalizable
@IBInspectable public var localePlaceholderKey: String?
get return nil
set(key)
placeholder = key?.localized
extension UISearchBar: UIPlaceholderXIBLocalizable
@IBInspectable public var localePlaceholderKey: String?
get return nil
set(key)
placeholder = key?.localized
edited Mar 8 at 13:54
answered Mar 8 at 13:05
denis_lordenis_lor
1,72611130
1,72611130
I'll try that out a bit later today, thanks a lot for your effort. Only one question: Do I need to go through all labels. (Since there is a loooooot) and add like: label1.text = NSLocalizedstring("", "") etc? or can I perform it all using one page somehow? Or do I manually has to add the NS-string to all labels and buttons? Thanks!
– Putte
Mar 8 at 13:57
The answer I gave is to be used to localize hard coded strings that are meant to be used in storyboards (UI) and you have to manually add the locale key throught the Attribute Inspector > Locale Key input field for each hard coded text in your UI View. For hard coded strings/text that you set programmatically through swift withmyview.text = ..you have can use as you said NSLocalizedString("your_key", "") for every hard coded string you wish to localize in multiple languages and as well add each different key in all your Localizable.strings version with their respective translated value
– denis_lor
Mar 8 at 14:03
@Putte Can you accept my answer in case you found it relevant to the question you made?
– denis_lor
Mar 10 at 9:41
add a comment |
I'll try that out a bit later today, thanks a lot for your effort. Only one question: Do I need to go through all labels. (Since there is a loooooot) and add like: label1.text = NSLocalizedstring("", "") etc? or can I perform it all using one page somehow? Or do I manually has to add the NS-string to all labels and buttons? Thanks!
– Putte
Mar 8 at 13:57
The answer I gave is to be used to localize hard coded strings that are meant to be used in storyboards (UI) and you have to manually add the locale key throught the Attribute Inspector > Locale Key input field for each hard coded text in your UI View. For hard coded strings/text that you set programmatically through swift withmyview.text = ..you have can use as you said NSLocalizedString("your_key", "") for every hard coded string you wish to localize in multiple languages and as well add each different key in all your Localizable.strings version with their respective translated value
– denis_lor
Mar 8 at 14:03
@Putte Can you accept my answer in case you found it relevant to the question you made?
– denis_lor
Mar 10 at 9:41
I'll try that out a bit later today, thanks a lot for your effort. Only one question: Do I need to go through all labels. (Since there is a loooooot) and add like: label1.text = NSLocalizedstring("", "") etc? or can I perform it all using one page somehow? Or do I manually has to add the NS-string to all labels and buttons? Thanks!
– Putte
Mar 8 at 13:57
I'll try that out a bit later today, thanks a lot for your effort. Only one question: Do I need to go through all labels. (Since there is a loooooot) and add like: label1.text = NSLocalizedstring("", "") etc? or can I perform it all using one page somehow? Or do I manually has to add the NS-string to all labels and buttons? Thanks!
– Putte
Mar 8 at 13:57
The answer I gave is to be used to localize hard coded strings that are meant to be used in storyboards (UI) and you have to manually add the locale key throught the Attribute Inspector > Locale Key input field for each hard coded text in your UI View. For hard coded strings/text that you set programmatically through swift with
myview.text = .. you have can use as you said NSLocalizedString("your_key", "") for every hard coded string you wish to localize in multiple languages and as well add each different key in all your Localizable.strings version with their respective translated value– denis_lor
Mar 8 at 14:03
The answer I gave is to be used to localize hard coded strings that are meant to be used in storyboards (UI) and you have to manually add the locale key throught the Attribute Inspector > Locale Key input field for each hard coded text in your UI View. For hard coded strings/text that you set programmatically through swift with
myview.text = .. you have can use as you said NSLocalizedString("your_key", "") for every hard coded string you wish to localize in multiple languages and as well add each different key in all your Localizable.strings version with their respective translated value– denis_lor
Mar 8 at 14:03
@Putte Can you accept my answer in case you found it relevant to the question you made?
– denis_lor
Mar 10 at 9:41
@Putte Can you accept my answer in case you found it relevant to the question you made?
– denis_lor
Mar 10 at 9:41
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55062448%2flabel-in-storyboard-dont-take-the-right-localization-value%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
It's your job to make translation for the language you have selected. The app won't automatically make the translation job for you.
– El Tomato
Mar 8 at 11:47
@ElTomato Nono, of course. But as you see I added "123123123123" just to try it out, but when I enter the application with the correct language it dosent change. It remains the normal string.
– Putte
Mar 8 at 11:47
Oh, I see. Sorry about that. Don't make the storyboard localizable, though. Use a localizable string file.
– El Tomato
Mar 8 at 11:50
@ElTomato How can I do that?
– Putte
Mar 8 at 11:50
Create a file titled Localizable. Then change the file type to strings. Put all translation strings in it.
– El Tomato
Mar 8 at 11:52