Handling Translated Content
The JSON:API Client offers a number of features to make it easy to work with localized content provided by Drupal. Let’s adapt our grid of recipes to make use of content translated in both English and Spanish from the Umami Demo profile.
The defaultLocale Option
In our previous examples we have not specified a locale, which means that the default locale was used. If you want to work with content in a different locale by default, you can specify the defaultLocale
option when creating an instance of the client.
Umami Recetas
Quiche mediterráneo profundo
Dificultad: medium
Ver RecetaBizcochos veganos de chocolate y nueces
Dificultad: medium
Ver RecetaPasta vegetariana al horno súper fácil
Dificultad: easy
Ver RecetaSopa de berro
Dificultad: easy
Ver RecetaPastel Victoria
Dificultad: easy
Ver RecetaPizza sin gluten
Dificultad: medium
Ver RecetaCurry verde tailandés
Dificultad: medium
Ver RecetaCrema catalana
Dificultad: medium
Ver RecetaSalsa de chile ardiente
Dificultad: easy
Ver RecetaBorscht con costillas de cerdo
Dificultad: medium
Ver RecetaBy providing the defaultLocale
option, we can now work with content in Spanish by default. As we’ll see next, it is still possible to specify a different locale when when making individual requests.
The locale Option
Using our client defaulted to Spanish, we can still request content in English when necessary. By specifying the locale
option with getResource
we can override the default locale for individual requests.
Umami Recipes
Deep mediterranean quiche
Difficulty: medium
View RecipeVegan chocolate and nut brownies
Difficulty: medium
View RecipeSuper easy vegetarian pasta bake
Difficulty: easy
View RecipeWatercress soup
Difficulty: easy
View RecipeVictoria sponge cake
Difficulty: easy
View RecipeGluten free pizza
Difficulty: medium
View RecipeThai green curry
Difficulty: medium
View RecipeCrema catalana
Difficulty: medium
View RecipeFiery chili sauce
Difficulty: easy
View RecipeBorscht with pork ribs
Difficulty: medium
View RecipeAll of our examples thus far have dealt with collections of multiple resources. Let’s next see the ways we can use the client to retrieve only a single resource.