API languages and links: one article across three routes

Connect the locale, API card content, and reader address without hiding a missing or withdrawn translation behind language fallback.

Different language paths lead to versions of the same article.
Different language paths lead to versions of the same article.

A client requests an English card, receives an English title, and follows a link to a Ukrainian page. The JSON is formally valid and the route responded successfully, but one response describes two different language versions. Another error is less noticeable: the English translation does not exist, so the server returns Ukrainian text under an English label.

Both cases arise when the locale, content, and addresses are selected independently. For the reader, this is one journey: discover an article and open the very version that the API offered. The entire relationship therefore needs checking.

Continue one article_demo on Symfony 8.1 and PHP 8.4+: LocaleRead adds a synthetic catalogue, an API card, and a minimal HTML page. The earlier Reading module and its routes remain available. This checks consistency between selection and links without making claims about a production API.

One article, several addresses

The identifier guide-1 connects three translations. It does not change when an editor revises the title or slug. The locale selects a particular public representation, and the slug belongs to that translation. In this example, the Ukrainian and English addresses deliberately have different endings.

Value Ukrainian version English version
Article identity guide-1 guide-1
Locale uk en
Slug notatky-pro-mist bridge-notes
API /api/locale-demo/uk/articles/guide-1 /api/locale-demo/en/articles/guide-1
Reader page /locale-demo/uk/read/notatky-pro-mist /locale-demo/en/read/bridge-notes

The public ID is a teaching key here, not a database row number. The API reads by ID and locale; the reader page reads by locale and slug. Replacing the uk prefix with en alone does not create the correct link, because the rest of the path may also differ.

This question differs from tracking an outdated translation. Editorial revision checks explain whether the texts agree in meaning. Our contract explains which available language version has been selected and where its link leads. Neither check replaces the other.

A locale in the route is an explicit choice

A successful request must contain one of the supported locales: uk, ru, en. A missing segment is not filled with a default language. Unsupported de does not become English, and uk-UA is not automatically shortened to uk. This is the example's deliberately narrow set, not a universal language-tag validator.

Symfony routing supports parameters, requirements, and path generation. In this exercise, _locale is passed explicitly both when reading and when building every link. The code does not rely on the previous request's locale or a browser session.

Fallback for translated interface messages serves a different purpose. Symfony Translator can look for a message in a fallback language catalogue. That does not decide whether an article in another language is published. In our catalogue, a missing English record remains missing even when the Ukrainian text is ready.

The card and its links describe one selected version

Every response field is built from the selected published translation. The controller does not take the title from one record, the slug from another, and the locale from a global setting. Below is the synthetic catalogue's expected English response:

{
  "article_id": "guide-1",
  "locale": "en",
  "title": "Bridge notes",
  "slug": "bridge-notes",
  "links": {
    "api": "/api/locale-demo/en/articles/guide-1",
    "reader": "/locale-demo/en/read/bridge-notes"
  },
  "translations": [
    {
      "locale": "uk",
      "api": "/api/locale-demo/uk/articles/guide-1",
      "reader": "/locale-demo/uk/read/notatky-pro-mist"
    },
    {
      "locale": "ru",
      "api": "/api/locale-demo/ru/articles/guide-1",
      "reader": "/locale-demo/ru/read/zametki-o-moste"
    },
    {
      "locale": "en",
      "api": "/api/locale-demo/en/articles/guide-1",
      "reader": "/locale-demo/en/read/bridge-notes"
    }
  ]
}

links.api returns to this card, while links.reader opens the text for a human. The translations array describes available journeys for the same article. The array's locale order is fixed for this exercise; the order of JSON object keys does not define its meaning.

The field set is closed: the editor's internal note is not serialized. However, having the correct structure does not itself prove that a translation is accurate. If the source title is wrong, the API can consistently transmit that error. This exercise therefore checks the relationship between selection and addresses; the content still needs editorial acceptance.

A link is built from its target translation

A separate ArticleLinks service receives a language version that has already been selected. Here is the independent example's complete class:

<?php

declare(strict_types=1);

namespace App\LocaleRead;

use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

final readonly class ArticleLinks
{
    public function __construct(private UrlGeneratorInterface $router)
    {
    }

    /** Root-relative links on this teaching application's origin. */
    public function forTranslation(LocalizedArticle $article): array
    {
        return [
            'locale' => $article->locale,
            'api' => $this->router->generate('app_locale_demo_api', [
                '_locale' => $article->locale,
                'articleId' => $article->articleId,
            ], UrlGeneratorInterface::ABSOLUTE_PATH),
            'reader' => $this->router->generate('app_locale_demo_reader', [
                '_locale' => $article->locale,
                'slug' => $article->slug,
            ], UrlGeneratorInterface::ABSOLUTE_PATH),
        ];
    }
}

The target locale is passed to the generator explicitly. As a result, a Ukrainian alternative does not inherit an English prefix during an English request. The API uses the stable ID, while the page uses that translation's slug. A client template should not duplicate these rules through string substitution.

The links start at the root of the same origin. Both routes belong to one application in this exercise. When the API and Frontend use different addresses, the Frontend address needs an agreed base and the complete URL needs a separate check. Mechanically taking the host of the current API request is insufficient. The teaching generator does not establish the correctness of that distributed configuration.

An incomplete translation set stays incomplete

For another article, the Ukrainian translation is published, the Russian translation has been withdrawn, and the English translation is absent. The Ukrainian card will contain only the Ukrainian journey. The application's list of supported languages is not the list of available translations for every article.

Direct reading and alternative-link construction use the same condition: the article is published and that particular translation is published. If the entire article is withdrawn, its language versions are no longer returned either. Another translation's internal state and title are not added to an error merely to explain the refusal.

Situation Expected response
The requested public translation exists 200 and the corresponding card
The translation is missing or withdrawn 404, without fallback
The article is unknown or withdrawn 404, without internal fields
A query tries to override the language 400
The slug belongs to another locale 404, without redirection

The identical application error not_found is a policy of this route. It does not mean that every framework failure uses the same JSON: a missing segment, for example, is rejected by the router. The example also has no user accounts and does not implement access to private articles. Checking public state does not replace authorization.

Headers confirm the selection instead of changing it

Under HTTP Semantics, Content-Language describes the language of the representation's intended audience; Accept-Language conveys a client's language preferences. In our contract, the path has already made the selection, so the preference header does not override it.

Successful JSON and HTML responses obtain Content-Language from the selected translation. An English card does not become Ukrainian because of a browser header. Requests containing ?locale=en, ?_locale=en, or any other query are rejected: this exercise has no second way to choose a language.

Controller responses include Cache-Control: no-store. The example does not implement caching or invalidation. If card caching is introduced later, its locale and available journeys become part of the freshness requirements. Withdrawing a translation must account for previously stored links as well as that translation's own body.

Check both ends of the journey

The Symfony tests send requests through the kernel. For each of the three languages, they must check the ID, title, locale, slug, Content-Language, and links. The test then follows every advertised API and reader path and compares the target article and locale. Comparing attractive JSON alone is insufficient: the address might not open.

Separate scenarios cover a missing or withdrawn translation, a withdrawn article, an unknown language, a missing segment, another locale's slug, a conflicting header, and extra query parameters. Synthetic text containing HTML markers checks that the minimal reader displays them as text. Repeated requests by one client should change language only when the path changes.

19 tests with 276 assertions were executed through one Symfony Kernel. They follow actual returned API/reader links and compare the target article and locale, alongside Content-Language, refusals, and escaping. They do not exercise an external reverse proxy, network, or browser navigation.

Add src/LocaleRead, LocaleArticleController, templates/locale_read, and fixtures/locale-catalog.json to the existing application. Add the entries below under the existing services block. The names app_locale_demo_api/app_locale_demo_reader differ from earlier routes. tests/LocaleRead/Fixtures/expected-en.json declares an expected payload rather than captured HTTP output.

services:
    App\LocaleRead\FixtureCatalog:
        arguments:
            $fixturePath: '%kernel.project_dir%/fixtures/locale-catalog.json'
    App\LocaleRead\ArticleLinks: ~
    App\Controller\LocaleArticleController: ~

What remains outside this contract

A public set of alternatives is not complete SEO markup. Canonical links, hreflang, sitemaps, redirect history, and search-engine behavior need their own rules. Here, a reader receives the correct path to an available version; that is not yet evidence of indexing.

Likewise, a successful local request does not guarantee that state will remain unchanged until the next navigation. An editor may withdraw a translation between receiving the card and opening the page. A client must handle that refusal instead of treating a received link as permanent permission.

The useful boundary is straightforward: article identity, selected locale, card content, and its journeys should describe one consistent reading result. A missing translation then remains visibly missing, while an address change has a specific place for updating and checking.

  1. Editorial Stable content identity: Content ID, slug, revision and DB ID
  2. API An API response is not an Entity: designing an article card
  3. Symfony Core The product owns the route: changing URLs without rewriting the capability
  4. SEO Canonical and hreflang: checking reciprocal links across three languages

A project by the author · Weather Solution200

After reading, a look outside.

Temperature, rain and wind in your city. Check the forecast and plan the hours ahead.

Check the forecast
Next article