Ways to get localized content
If you plan to display culture-specific content, your app needs to know how to retrieve content in specific languages.Depending on what you know about your content, there are a few ways to go about this.
Languages and localization
Each language in your Kontent.ai project is uniquely identified by its codename, which can be any string, such asEnglish
, en-US
, or en-GB
.
Your default languageEvery project comes with a single default language that cannot be removed. When you request content without specifying a language, Delivery API returns content in the default language.
Get content in a specific language
To get localized content from your project, specify the language using the language's codename. For example, to get an article named About Us in Spanish, specify the item's codename and the language's codename.// Tip: Find more about Java SDK at https://kontent.ai/learn/java
import kontent.ai.delivery.*;
// Initializes a DeliveryClient
DeliveryClient client = new DeliveryClient("KONTENT_AI_ENVIRONMENT_ID");
// Tip: Create strongly typed models according to https://kontent.ai/learn/net-strong-types
// Registers the model class for articles
client.registerType(Article.class);
// Gets a specific article in Spanish
CompletionStage<Article> item = client.getItem(
"about_us",
Article.class,
DeliveryParameterBuilder.params()
.language("es-ES")
.build()
);
What if the item doesn't exist in the specified language?The Delivery API checks the fallbacks of the specified language. If the content exists in the fallback language set for the requested language, you get the content item's content in the fallback language.
Get items by localized URL slug
Depending on how your app implements navigation and routing, you might want to request content items directly by their URL slugs. To retrieve a localized content item by its URL slug, you need to:- Specify the item's language using the language codename, such as
es-ES
for Spanish. - Filter by the URL slug element’s value.
// Tip: Find more about Java SDK at https://kontent.ai/learn/java
import kontent.ai.delivery.*;
// Initializes a DeliveryClient
DeliveryClient client = new DeliveryClient("KONTENT_AI_ENVIRONMENT_ID");
// Tip: Create strongly typed models according to https://kontent.ai/learn/net-strong-types
// Registers the model class for articles
client.registerType(Article.class);
// Filters all articles to find the Spanish variant by its URL slug
CompletionStage<List<Article>> items = client.getItems(
Article.class,
DeliveryParameterBuilder.params()
.language("es-ES")
.filterEquals("system.type", "article")
.filterEquals("elements.url_pattern", "acerda-de-nosotros")
.build();
);
Ignoring language fallbacks
If you need to ensure you get content in only the specified language, you can filter out the content items that are in another language than specified. This effectively bypasses any language fallbacks.For example, this is how you retrieve content items translated into Spanish without any fallbacks.
// Tip: Find more about Java SDK at https://kontent.ai/learn/java
import kontent.ai.delivery.*;
// Initializes a DeliveryClient
DeliveryClient client = new DeliveryClient("KONTENT_AI_ENVIRONMENT_ID");
// Gets content items in Spanish without following language fallbacks
CompletionsStage<ContentItemsListingResponse> listingResponse = client.getItems(
DeliveryParameterBuilder.params()
.language("es-ES")
.filterEquals("system.language", "es-ES")
.build()
);
Sign in with your Kontent.ai credentials or sign up for free to unlock the full lesson, track your progress, and access exclusive expert insights and tips!