Skip to content

Instantly share code, notes, and snippets.

@andresr-dev
Created January 30, 2023 00:34
Show Gist options
  • Save andresr-dev/df1be4332b1ac325eef9b761fce11381 to your computer and use it in GitHub Desktop.
Save andresr-dev/df1be4332b1ac325eef9b761fce11381 to your computer and use it in GitHub Desktop.
This is how to apply automatic grammar agreement on a localizable file
// Localizable.string
"small" = "small";
"medium" = "medium";
"espresso" = "espresso";
"cappuccino" = "cappuccino";
// 1 small cappuccino - $2.99
"^[%lld %@ %@](inflect: true) - **$%@**" = "%1$lld %2$@ %3$@ - **$%%4$@**";
// Localizable.string - Spanish
"small" = "chico";
"medium" = "mediano";
"espresso" = "expresos";
"cappuccino" = "capuchino";
"^[%lld %@ %@](inflect: true) - **$%@**" = "%1$lld %3$@ %2$@ - **$%%4$@**";
let coffeeCount = 2
let coffeeSize = String(localized: "medium")
let coffeeType = String(localized: "espresso")
let price = 4.99
let attributedString =
AttributedString("^[\(coffeeCount) \(coffeeSize) \(coffeeType)](inflect: true) - **\(price)**")
// English will print:
// 2 medium espressos - $4.99
// Spanish will print:
// 2 expresos medianos - $4.99
// Source: https://medium.com/geekculture/automatic-grammar-agreement-b953cbf5d101
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment