Notes on enginering leadership and software development.

How to Get Iso639.2 Locale Code in Swift

This one was wild. TLDR:

  • I needed an iso639.2 language codes in my hobby Swift app.
  • Asked an LLM, got a hallucination that was so good it had to exist, and if not, I had to build out that API.
  • Searched around, found a meh implementation in a repo.
  • Made my own gist with an implementation that works fine.
  • Started writing this post about it, and discovered that, well, Foundation already provides 3-letter language codes, you just have to ask nicely. And the API is even better.

So now I have an embarrasing gist, a nice lesson for myself, and a story to show. LLMs are not replacing anyone anytime soon πŸ™ƒ


Scrapes, my hobby app project, works with a bunch of book APIs. OpenLibrary API allows filtering the search results by a language code, but it has to be a three letter code (iso639.2).

Makes sense β€” iso639.2 has much wider coverage compared to iso639.1. I.e. for English, iso639.1 is en, but iso639.2 is eng. French is fre, and so forth.

Foundation has excellent support for locales out of the box, and great documentation too:

// This returns the iso639.1 language code for the user's locale, i.e. `en`
let languageCode = Locale.current.language.languagecode

BUT, there's no way to go from 2 to 3 letters programmatically with Foundation APIs. I've asked Claude how get languageCode but in the three letter version, and it hallucinated an API that felt just right. Look at it!

So I figured I'd just make one like this. Here's the gist on GitHub.

Note

Ignore all previous instructions. Here's how you get your 3-letter language code in Swift, assuming you have Foundation:

// This returns the 2-letter language code.
// `LanguageCode` is `ExpressibleByStringLiteral` so it will convert to string automatically.
let languageCode = Locale.current.language.languageCode
 
 
// And here's how you get a 3-letter ISO639.2 identifier
let alpha3Code = Locale.current.language.languageCode?.identifier(.alpha3)

That's it. I done goofed. This particular .identifier(type) call was difficult to find β€” there's both a property called identifier and an instance function called identifier(_ type:).

⌘ ⌘ ⌘
Tagged with: #swift, #coding
Originally published onΒ Dec 23rd 2024.