Apple Glot 4

Posted by Dorin Danciu.

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

  1. Download and Install AppleGlot
  2. 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

  1. 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 '.'
  2. 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
  3. Set the source and target languages. For example, if the development language is English and the target is Romanian, pass en for the base_language_id and ro for target_langauge_id. Now, to crosscheck the previous command we can use getlangs command to question the languages setup on this environment.

    Example:

    $ appleglot -w setlangs en ro
    $ appleglot getlangs
    en
    ro
  4. 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, paste ru.lproj into _NewBase, and change the name to en.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";
  5. 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 and wg 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>
  6. 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";
  7. 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 the ro.lproj folder if the target language is Romanian.

That's it. Efficient and fun!


References

This article is my 3rd oldest. It is 767 words long