Skip to content

LanguageCode Criterion

The LanguageCode Search Criterion searches for content based on whether it is translated into the selected language.

Arguments

  • value - string(s) representing the language codes to search for
  • (optional) matchAlwaysAvailable - bool representing whether content with the alwaysAvailable flag should be returned even if it does not contain the selected language (default true)

Example

PHP

1
$query->query = new Criterion\LanguageCode('ger-DE', false);

REST API

1
2
3
4
5
<Query>
    <Filter>
        <LanguageCodeCriterion>eng-GB</LanguageCodeCriterion>
    </Filter>
</Query>
1
2
3
4
5
"Query": {
    "Filter": {
        "LanguageCodeCriterion": "eng-GB"
    }
}

Use cases

You can use the LanguageCode Criterion to search for articles that are lacking a translation into a specific language:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
        $query = new Query();
        $query->query = new Criterion\LogicalAnd([
            new Criterion\ContentTypeIdentifier('article'),
            new Criterion\LogicalNot(
                new Criterion\LanguageCode($languageCode, false)
            ),
        ]);

        $results = $this->searchService->findContent($query);
        $articles = [];
        foreach ($results->searchHits as $searchHit) {
            $articles[] = $searchHit->valueObject;
        }

        return $this->render('@ibexadesign/list/articles_to_translate.html.twig', [
            'articles' => $articles,
        ]);

You can use the LanguageCode Criterion to search in several languages while ensuring results have a translation in one specific language:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
        $query = new Query(['query' => new Criterion\LogicalAnd([
            new Criterion\FullText($text),
            new Criterion\LanguageCode(['eng-GB'], false),
        ])]);

        $results = $this->searchService->findContent($query, ['languages' => ['eng-GB', 'fre-FR', 'ger-DE']]);
        foreach ($results->searchHits as $searchHit) {
            /** @var \Ibexa\Core\Repository\Values\Content\Content $content */
            $content = $searchHit->valueObject;
            dump($content->getName('eng-GB'));
        }