Adding localized string to UITextView in swift - ios

I am following the instructions on this blog post to localize my swift app. http://evgenii.com/blog/localizing-text-in-swift/
In my Swift file I have
let my_interests = "Reading: Children's books, biographies, history and science fiction.\n\nProgramming Languages: Learning new programming languages."
....
textView.text = NSLocalizedString("my_interests", comment: "Just a Test")
When I go to the Editor and select Export For Localization
I get an error.

You probably don't have a Localizable.strings in your project.
In your XCode project go to File -> New -> File -> iOS -> Resource -> Strings.file -> Next
Name the file Localizable.strings

Related

Localization in Xcode 10

I have an app where all the user facing strings use NSLocalizableString(). I've never localized an app so I watched WWDC 2018 video New Localization Workflows in Xcode 10. From there I created a new project, a Single View App. Base localization is on by default. On the storyboard I added one UILabel and constrained it in the center of the view. I added an outlet to it in the view controller:
#IBOutlet weak var label: UILabel!
And I set its text in viewDidLoad:
label.text = NSLocalizedString("Hello World", comment: "")
Build and run and it works fine.
Now I add a localization. I select the project and add Spanish.
Then I Export For Localization... on the Editor menu. I open the file through this path:
es.xcloc -> LocalizedContents -> es.xliff
In there I find the "Hello World" key and I change the value to "Hola Mundo" and save the file.
...
<file original="MyProject/en.lproj/Localizable.strings" datatype="plaintext" source-language="en" target-language="es">
<header>
<tool tool-id="com.apple.dt.xcode" tool-name="Xcode" tool-version="10.1" build-num="10B61"/>
</header>
<body>
<trans-unit id="Hello World">
<source>Hola Mundo</source>
<note>No comment provided by engineer.</note>
</trans-unit>
</body>
</file>
...
Then back to Xcode and Import Localizations. Here's the first problem:
Why is this a Mismatched Translation?
I go ahead and import anyway. I edit the scheme as follows:
When I run I get "HELLO WORLD". It's uppercase because that's the localization debugging indicating there's no translation.
What am I missing?
We have a few problems here. First, when you do NSLocalizedString("Hello World", comment: "") what is actually happening is that you try to fetch a localized string with key Hello World and since Xcode can't find that key, it returns the key itself, which you set for the label. So, for best practices, consider using a key that doesn't contain a white space on it. Maybe something like hello-world-label. Then, it would be:
NSLocalizedString("hello-world-label", comment: "")
This will generate the following after you export for localization:
<trans-unit id="hello-world-label">
<source>hello-world-label</source>
<note>No comment provided by engineer.</note>
</trans-unit>
And now you have to add <target> tag to specify the actual translation. Therefore:
<trans-unit id="hello-world-label">
<source>hello-world-label</source>
<target>Hola Mundo</target>
<note>No comment provided by engineer.</note>
</trans-unit>
Then just import back the localizations and you should be fine.
EDIT
After doing that, a Localizable.strings file should have been generated. Click on it and take a look into the File Inspector on the right side of Xcode under the Localization section. You will that the file is only localized for Spanish. So go ahead and select English checkbox. Then, your Localizable.strings will be editable for both English and Spanish, as you can see on the figure below:
Now you edit the English version of that file for having both languages in your app.

How to use a string defined in project into UILabel defined in Storyboard?

Basically in android studio there is a string.xml file in which you can define the strings that you will be using in your project.
Below I am giving the Android examples of how this is done:
String defined in string.xml:
<string name="lbl_login_banner">Find Professionals</string>
In the layout.xml (which would be equivalent of .storyboard, .xib or .nib):
android:text="#string/lbl_login_banner"
Is it possible to replicate this in Xcode version 9.2 or Swift 4.
In iOS you have to create Localizable.strings file, and inside that file you can define your strings.
"lbl_login_banner" = "Find Professionals";
How you can use it -
In Storyboard you can use the constant in label value like -
In Code -
myLabel.text = NSLocalizedString(lbl_login_banner, comment: "")
You can read documentation on how to use NSLocalizedString.
You can follow this tutorial on how to create and use Localizable.strings file.

How do you add a group in iOS playground?

In an Xcode project, files in the left Project Navigator can be organized into virtual (and actual) folders (Groups).
In Playground, while a new folder can be added, it does not seems to be possible to do the same.
Can it be done in Playground?
And if you can't, what would be similar?
You can create an Xcode Project with a built in Playground, information found here
In Xcode, File -> New -> Project… and then iOS -> Application -> Single View Application
File -> Save as Workspace… Save it as SuperProject.xcworkspace in the same directory as SuperProject.xcproject file.
File -> New -> File… and then iOS -> Source -> Playground. Call it SuperProject.playground, use the same directory as SuperProject.xcproject, and select the top-level “SuperPlayground” under Group (this is not the default option).
File -> New -> Target… and then iOS -> Framework & Library -> Cocoa Touch Framework. Name it SuperPlaygroundiOS and uncheck Unit Tests.
Optional: Control-click on ViewController.swift in the Project Navigator1 and click New File… and then iOS -> Source -> Swift File. Call it SuperClass.swift and make sure SuperProject and SuperProjectiOS are checked under Targets. Replace the contents of this file with this:
import Foundation
class SuperClass{
func greetMe() -> String{
return "Hello"
}
}
Select the framework target (SuperPlaygroundiOS) in the File Inspector for any other files you want to access from your new playground.
Select SuperPlaygroundiOS > iPhone 6S Plus beside the Play/Stop buttons, then Product -> Build.
In SuperProject.playground, type the following at the top of the file:
#testable import SuperPlaygroundiOS
If you did step #5, you can now type this at the bottom of the file too.
SuperClass.greetMe()

Advance new file in sublime text 2

I have beed using Advance new file package in Sublime Text 2 and when I press shortcut for creating new file is in my main directory C\users\%name\.
Is it possible (or with another package) to set path to be in folder that I'm currently at.
Example, if I'm at
C:\Users\%user\Desktop\Notebook\Ruby programs\Ruby\test.rb
to set the path to
C:\Users\Bane\Desktop\Notebook\Ruby programs\Ruby\
Open Preferences -> Package Settings -> AdvancedNewFile -> Settings - User and add the following to the file.
{"default_root": "current"}
You can see more about settings on the GitHub page.

Trouble localizing iOS application, clarification needed

Please let me know what in the following sequence is incorrect.
Somewhere in code, i have self.label.text = NSLocalizedString(#"TITLE", nil);
Localizable.strings created ( new -> Resource -> String file)
Localizable.strings (English) created // contains "TITLE" = "Hello";
Localizable.strings (Russian) created // contains "TITLE" = "Привет";
Simulator started and application deployed
Xcode closed
Simulator settings changed from English to Russian (Settings -> General -> International -> Language -> Russian)
Xcode opened
Application deployed (icon labels appear in Russian) .. when ran it says "Hello" instead of "Привет"
Am i missing a step? Did i do something in the wrong order? Please let me know
Those steps look fine. Things to try:
The folder name for Russian needs to match the language code
(ru.lproj -> Localizable.strings)
Check that the label is hooked up properly (if you're using interface builder) and not nil when you are setting self.label.text

Resources