Grammatical Agreement

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

You need grammatical agreement techniques when words in your app’s UI need to change their form based on another word. If you need to display information about an item, and there could be one or more of the item, you need both its singular and its plural forms, as in “1 apple” and “2 apples”. If your displayed string uses an interpolation like \(item.name), then you need a way to display the correct plural form of every possible value of item.name.

In languages like French and Spanish, every noun is either feminine or masculine, and any adjectives used with it must agree with its gender and number — “une chemise verte”, “deux chemises vertes”. This agreement extends to adjectives that appear later in the string or elsewhere on the screen.

Grammatical agreement involves at least two words:

  • The word that must be agreed with, usually a variable or argument.
  • One or more words that depend on this argument, changing their form to agree with it.

Using Agreement Attributes

There are three tools for grammatical agreement. Which one you use depends on the proximity of the dependent words to the argument:

Extending Markdown

Since iOS 15, you can use Markdown in Text views to easily display formatted text. You’re probably familiar with this syntax for creating a link:

[Kodeco](https://www.kodeco.com)
^[bleu %$@](inflect: true)

Inflecting

Inflection: A change in the form of a word (typically the ending) to express a grammatical function or attribute such as tense, mood, person, number, case, and gender.

^[%1$lld %2$@ %3$@](inflect: true)

Agreeing With Argument

Use agreeWithArgument when a string contains a word and the argument it needs to agree with, but the word and the argument are separated by a lot of text. Again, annotate strings directly in Localizable:

C'est ^[un %@](inflect: true) d'homme ... ^[porté](agreeWithArgument: 1) ...

Agreeing With Concept

The new LocalizationOptions property concepts enables you to specify objects that affect grammatical agreement of a string but aren’t formatted into the string as an argument. Unlike the inflect and agreeWithArgument attributes, the new agreeWithConcept attribute requires some code changes.

var options: AttributedString.LocalizationOptions {
  var options = AttributedString.LocalizationOptions()
  options.concepts = [.localizedPhrase(item.name)]
  return options
}
^[noir](agreeWithConcept: 1)
^[bleu](agreeWithConcept: 1)
^[vert](agreeWithConcept: 1)
^[rouge](agreeWithConcept: 1)

Being Inclusive With TermsOfAddress

A different concept — termsOfAddress — enables your app to accommodate grammatical gender agreement. Your app automatically uses the preferred pronoun of the user or another person associated with your app, such as a delivery person or clothes model.

options.concepts = [.termsOfAddress(model.preferredTermsOfAddress)]
Text(
  AttributedString(
    localized:
      "🏃🏻‍➡️\(model.name) is \(model.height) cm tall. He wears size \(model.size).", 
    options: options))
... ^[He](referentConcept: 1) ...
See forum comments
Download course materials from Github
Previous: Introduction Next: Demo: Grammatical Agreement