TLDR
My needs at the moment were to localize some common words found across the system (i.e Cancel, OK, Done, Next, Settings, etc.) in order to deliver them with the Cocoa Touch Framework I was working on.
Since there is no official way to load UIKit localized strings for your personal use, I remembered that Apple offers all the glossaries they're using via the developer portal.
AppleGlot is an extraction tool, it is able to use glossary files from previous localizations in order to substitute the original text in the resource files with the localized text. But this only works on 100% text matches; it will not do partial translations.
Well, this looks like a perfect fit in my case, so lets jump in and document the process for the future me.
Localizing Strings Files Using AppleGlot
To get your environment ready
- Download and Install AppleGlot
- Download the Localization Glossaries Apple is offering
Sneak peeking under the hood ($ man appleglot
), we discover the following commands
list print list of components in NewBase
getlangs get the current base language and target language
setlangs set the current base language and target language
populate create NewLoc, using OldLoc if available
update update NewLoc from Glossaries
finalize remove temporary working files
create create empty environment
At a first glance, there's nothing complex about this tool, so let's dive in.
To translate your strings files using AppleGlot
-
In Terminal, create an AppleGlot environment. This is basically a glorified folder structure used by this CLI tool.
Example:
$ mkdir ToolKit_Environment $ cd ToolKit_Environment/ $ appleglot -w create . Creating empty environment on '.'
-
Now, if we go ahead and check what actually happened on disk, we can see how an empty environment looks like.
Example:
$ ls _ApplicationDictionaries _OldBase _Translations _LanguageGlossaries _OldLoc _Translators _Logs _Projects _WorkGlossary _NewBase _Rules _NewLoc _Temporary
-
Set the source and target languages. For example, if the development language is
English
and the target isRomanian
, passen
for thebase_language_id
andro
fortarget_langauge_id
. Now, to crosscheck the previous command we can usegetlangs
command to question the languages setup on this environment.Example:
$ appleglot -w setlangs en ro $ appleglot getlangs en ro
-
In the Finder, paste the language resource folders into the AppleGlot environment
_NewBase
folder. If you use base internationalization, paste the language-specific folder,[target_language_id].lproj
, into the_NewBase
folder, and change the name of the folder to[base_language_id].lproj
. For example, pasteru.lproj
into_NewBase
, and change the name toen.lproj
.Otherwise, if you don’t use base internationalization, the
[base_language_id].lproj
folder should contain all the strings files that you want translated into the target language.In my case, I had to localize some strings that are used in a Cocoa Touch Framework, hence the second option suited me best.
$ ls _NewBase en.lproj $ cat _NewBase/en.lproj/Localizable.strings "Cancel" = "Cancel"; "OK" = "OK"; "Done" = "Done"; "Next" = "Next"; "Settings" = "Settings";
-
Open the target language glossary
.dmg
, and copy the glossaries (files with the.lg
extension) into the_LanguageGlossaries
folder. In Terminal, populate the_NewLoc
folder.$ appleglot -w populate
This creates a
.ad
file in the_ApplicationDictionaries
folder with previously translated strings and a.wg
file in the_WorkGlossary
folder that contains all the strings in your project with as much of the translations from your Language Glossaries as possible.Optionally, localize the remaining strings in the files with the
ad
andwg
extensions using a third-party localization tool that supports these file formats.Example
en.lproj.ad
<?xml version="1.0" encoding="UTF-8"?> <!-- Comment here. (1.0a10c2) --> <Proj> <ProjName>en.lproj</ProjName> </Proj>
Example
en.lproj.wg
<?xml version="1.0" encoding="UTF-8"?> <!-- Comment here. (1.0a10c2) --> <Proj> <ProjName>en.lproj</ProjName> <!-- --> <!-- en.lproj/../Localizable.strings --> <!-- --> <File> <Filepath>en.lproj/Localizable.strings</Filepath> <TextItem> <Description> !#x000a; Localizable.strings!#x000a; ToolKitUI!#x000a;!#x000a; Created by Dorin Danciu on 08/03/2018.!#x000a; Copyright © 2018 beilmo. All rights reserved.!#x000a;</Description> <Position>Cancel</Position> <TranslationSet> <base loc="en" >Cancel</base> <tran loc="ro" origin="LG matched text">Anulați</tran> </TranslationSet> </TextItem> <TextItem> <Description></Description> <Position>Done</Position> <TranslationSet> <base loc="en" >Done</base> <tran loc="ro" origin="LG matched text">OK</tran> </TranslationSet> </TextItem> <TextItem> <Description></Description> <Position>Next</Position> <TranslationSet> <base loc="en" >Next</base> <tran loc="ro" origin="LG matched text">Înainte</tran> </TranslationSet> </TextItem> <TextItem> <Description></Description> <Position>OK</Position> <TranslationSet> <base loc="en" >OK</base> <tran loc="ro" origin="LG matched text">OK</tran> </TranslationSet> </TextItem> <TextItem> <Description></Description> <Position>Settings</Position> <TranslationSet> <base loc="en" >Settings</base> <tran loc="ro" origin="LG matched text">Configurări</tran> </TranslationSet> </TextItem> </File> </Proj>
-
In Terminal, integrate the translations back into your strings files.
$ appleglot -w update $ appleglot -w finalize
Checking the
_NewLoc/en.lproj/Localizable.strings
we will see that the new translations have been included."Cancel" = "Anulați"; "OK" = "OK"; "Done" = "OK"; "Next" = "Înainte"; "Settings" = "Configurări";
- In the Finder, paste the localized resources into your Xcode project folder.
Paste the contents of the
_NewLoc/[base_language_id].lproj
folder into your[target_language_id].lproj
folder in the Xcode project folder. For example, paste the contents of the_NewLoc/en.lproj
folder into thero.lproj
folder if the target language is Romanian.
That's it. Efficient and fun!