Search code examples
localetruetypeietf-bcp-47

converting TrueType Macintosh Language Codes to BCP 47 language tags


Truetype fonts use "Macintosh Language Codes" to describe the language of localised strings in the "name" table. A list of language codes can be found in in the TrueType spec. I need to convert these language codes to BCP 47 language tags. Is there a table somewhere which shows the corresponding language tags for each code? Or some pre-existing code which does this translation somewhere? Or maybe a MacOS API which can translate codes to tags for me?


Solution

  • You can use the CFLocaleCreateCanonicalLocaleIdentifierFromScriptManagerCodes to map from legacy IDs to BCP 47 tags.

    Edit by the OP: the following code works for me.

    #import <Foundation/Foundation.h>
    
    int main(int argc, const char * argv[]) {
        CFAllocatorRef alloc = CFAllocatorGetDefault();
        for (int i = 0; i <= 150; i++) {
            CFLocaleIdentifier bcp47 = CFLocaleCreateCanonicalLocaleIdentifierFromScriptManagerCodes(alloc, i, -1);
            const char *s = CFStringGetCStringPtr(bcp47, kCFStringEncodingUTF8);
            if (s) {
                printf("%d: \"%s\",\n", i, s);
            }
        }
        return 0;
    }