Internationalize your app and then localize it for English, Spanish, and Arabic. (For example, the above Arabic is pronounced “Ahalan bil-Alam!” I went to the Hello, World! page in Wikipedia and clicked on Arabic in the left panel. It’s right above Azərbaycanca.) See Internationalizing Your App and the much more colorful About Internationalization and Localization.
The
International
Organization for Standardization
(ISO)
has two-letter codes
(ISO 639–1)
for the natural languages:
EN
for English,
ES
for Spanish,
AR
for Arabic.
Look up the two-letter code for your language.
AppDelegate.swift
:
unchanged.ViewController.swift
:
unchanged.View.swift
:
the
drawRect
method draws the text below the status bar
(exercise 3).
Main.storyboard
:
changed the class of the view controller’s
UIView
to my class
View
.LaunchScreen.storyboard
:
I changed the
background
color
to yellow (rgb 1, 1, 0, with an alpha of 1).
Assets.xcassets
,
an Xcode
asset
catalog
file.
AppIcon.appiconset
Contents.json
:
a
JSON
file listing the six
.png
files I added to the app as icons.
The JSON file gives the following information for each
.png
file:
.png
file in points.png
file is intended for use on iPhone or iPad.png
fileIcon29x29@2x.png
(58 × 58 pixels)Icon29x29@3x.png
(87 × 87 pixels)Icon40x40@2x.png
(80 × 80 pixels)Icon40x40@3x.png
(120 × 120 pixels)Icon60x60@2x.png
(120 × 120 pixels)Icon60x60@3x.png
:
(180 × 180 pixels)Localizable.strings
InfoPlist.strings
Info.plist
:
unchanged.Hello.zip
from the previous project
(Icon).
In the
drawRect(_:)
method of class
View
,
change
let s: String = "Hello, World!";to
let s: String = NSLocalizedString("Greeting", comment: "displayed when app is launched");See Separating User-Facing Text from Your Code.
View.swift
file that you just edited.
Select
View.swift
in the Xcode
Project
Navigator
and pull down
View.swift
is
/Users/myname/Desktop/Hello/Hello/View.swift
,
assuming that the project is named
Hello
and is stored on your Desktop.
This means that the folder that contains
View.swift
is
/Users/myname/Desktop/Hello/Hello
.
Localizable.strings
and place it in the
Base.lproj
folder.
See
Creating
Strings Files for User-Facing Text in Your Code.
Open the Mac Terminal application and go to the folder you just found.
cd /Users/myname/Desktop/Hello/Hello pwd /Users/myname/Desktop/Hello/Hello ls -l *.swift -rw-r--r--@ 1 myname mygroup 2050 Jul 5 20:03 AppDelegate.swift -rw-r--r--@ 1 myname mygroup 911 Jul 17 08:18 View.swift -rw-r--r--@ 1 myname mygroup 469 Jul 5 20:03 ViewController.swift ls -ld Base.lproj drwxr-xr-x@ 5 myname mygroup 170 Jul 17 09:07 Base.lproj which genstrings /usr/bin/genstrings genstrings Usage: genstrings [OPTION] file1.[mc] ... filen.[mc] etc. genstrings -o Base.lproj *.swift ls -l Base.lproj/Localizable.strings -rw-r--r-- 1 myname mygroup 128 Jul 17 09:51 Localizable.strings cat Base.lproj/Localizable.strings ??/* displayed when app is launched */ "Greeting" = "Greeting"; od -c Base.lproj/Localizable.strings 0000000 377 376 / \0 * \0 \0 d \0 i \0 s \0 p \0 0000020 l \0 a \0 y \0 e \0 d \0 \0 w \0 h \0 0000040 e \0 n \0 \0 a \0 p \0 p \0 \0 i \0 0000060 s \0 \0 l \0 a \0 u \0 n \0 c \0 h \0 0000100 e \0 d \0 \0 * \0 / \0 \n \0 " \0 G \0 0000120 r \0 e \0 e \0 t \0 i \0 n \0 g \0 " \0 0000140 \0 = \0 \0 " \0 G \0 r \0 e \0 e \0 0000160 t \0 i \0 n \0 g \0 " \0 ; \0 \n \0 \n \0The
Localizable.strings
file consists of 16-bit characters.
The first 16-bit character in the file is the
byte order mark.
od -c
displays each 16-bit character as two separate bytes.
Localizable.strings
,
add it to the project.
Select the project
Hello
at the top of the Xcode
Project
Navigator
and pull down
Hello/Base.lproj/Localizable.strings
and press Add.
Note that
Localizable.strings
is now listed in the Xcode
Project
Navigator.
InfoPlist.strings
and place it in the
Base.lproj
folder with
Localizable.strings
.
At the top of the Xcode
Project
Navigator,
select the project
Hello
and pull down
InfoPlist.strings
in the folder
Hello/Hello/Base.lproj
.
Note that
InfoPlist.strings
is now listed in the Xcode
Project
Navigator.
es.lproj
and
ar.lproj
.
At the top of the Xcode
Project
Navigator,
select the project
Hello
.
At the top of the center panel of Xcode,
to the left of the word General,
click on the word Hello and select Project Hello from the list that pops up.
At the bottom of the center panel,
make sure Use Base Internationalization is checked.
Under Localizations, press the plus sign and select Spanish (es).
A window will pop up that says
Localizable.strings
and
InfoPlist.strings
are checked and press Finish.
Also add Arabic (ar) to the project.
Then at the upper left of the center panel of Xcode,
where we selected Project Hello from the pop-up list,
change it back to the original Targets Hello.
Base.lproj
,
es.lproj
,
and
ar.lproj
,
each containing files named
Localizable.strings
and
InfoPlist.strings
.
Select
Localizable.strings
(Base)
in the Xcode
Project
Navigator
and change it from
/* displayed when app is launched */ "Greeting" = "Greeting";to
/* displayed when app is launched */ "Greeting" = "Hello, World!";Select
Localizable.strings
(Spanish)
in the Xcode
Project
Navigator
and change its last line to
"Greeting" = "¡Hola, Mundo!";Select
Localizable.strings
(Arabic)
in the Xcode
Project
Navigator
and change its last line to
"Greeting" = "!أهﻼ بالعالم";
Info.plist
file.
Select
InfoPlist.strings
(Base)
in the Xcode
Project
Navigator
and change it from
/* InfoPlist.strings Hello Created by Mark Meretzky on 7/18/16. Copyright © 2016 Mark Meretzky. All rights reserved. */to
/* InfoPlist.strings Hello Created by Mark Meretzky on 7/18/16. Copyright © 2016 Mark Meretzky. All rights reserved. */ CFBundleDisplayName = "Hello, World!";Select
InfoPlist.strings
(Spanish)
in the Xcode
Project
Navigator
and append the following line to it.
CFBundleDisplayName = "¡Hola, Mundo!";Select
InfoPlist.strings
(Arabic)
in the Xcode
Project
Navigator
and append the following line to it.
CFBundleDisplayName = "!أهﻼ بالعالم";
Settings →
General →
International →
Region Format →
Spanish → Spain
Done
Localizable.strings
files to
"Greeting" = "Wait %d minutes.";
"Greeting" = "Espere %d minutos.";
"Greeting" = "دقائق %d انتظري";
For the
%d
,
see the
list
of formats.
In the
drawRect:
method of class
View
,
change the
string
to
let n: Int = 5; //minutes let format: String = NSLocalizedString("Greeting", comment: "displayed when app is launched"); let s: String = String(format: format, n);See Formatting String Resources.