- Keep all your translated strings in one XML file;
- Generate
Localizable.strings
for each language; - Generate Swift's
enum
with strings identifiers.
Sample input strings XML file:
<?xml version="1.0" encoding="UTF-8"?>
<strings>
<string id="login">
<en>Log in</en>
<nl>Inloggen</nl>
<ru>Вход</ru>
</string>
<string id="sign_in">
<en>Sign in</en>
<de>Anmelden</de>
</string>
</strings>
Generating Localizable.strings:
$ ./make.sh strings.xml output output/Strings.swift
This will generate given enum
to have auto-completion in code and avoid any typo errors:
/// Auto-generated
import Localizable
enum Strings {
@Localizable static var login = "login"
@Localizable static var sign_in = "sign_in"
}
- Copy
Resources/Localizable
folder into project'sResources
- Place your translations
Strings.xml
file intoResources
- Add new Build Phase to generate strings
cd $SRCROOT/$TARGETNAME/Resources/Localizable
./make.sh $SCRIPT_INPUT_FILE_0 .. $SCRIPT_OUTPUT_FILE_0
- Input Files:
$(SRCROOT)/$(TARGETNAME)/Resources/Strings.xml
- Output Files:
$(SRCROOT)/$(TARGETNAME)/Strings.swift
As an option here is possible to reformat input source XML to keep languages in sorted order:
$ format.sh Strings.xml
This will overwrite existing file.
- In Xcode select File ⭢ Swift Packages ⭢ Add Package Dependency...
- Copy-paste repository URL: https://github.com/avdyushin/Localizable
- Hit Next two times, under Add to Target select your build target.
- Hit Finish