I have a data model called Podcast which doesnât have translation currently enabled, however it is queried within a generic function that often does have translated content, so it is queried for various locales.
My issue is that no matter what locale is queried, the content is returned.
For example, this query returns 3 results despite no Japanese content being available:
query MyQuery {
allPodcasts(locale: ja) {
id
}
}
Actually this is worse than I thought. The locale filter doesnât work right at all. Even on translated content types itâs returning results that arenât of the locale requested.
Hi @kaushik
records do not have a âlocaleâ per se. Localization is a setting you enable/disable on a single field. So, this means that itâs a field that has some locales, not a record.
I suppose that the Podcast model has a localized field. Letâs call it title
. If you wanna fetch all of the podcasts ids where the title has a japanese locale, then you should do a graphql query like this:
query MyQuery {
allPodcasts(locale: ja, filter: {title: {neq: ""}}) {
id
}
}
Hope this clarifies a bit!
So itâs normal/expected that on a model with no localized fields, that query you posted returns results?
Because that seems wrong to me.
Yes itâs normal. A query like this:
query MyQuery {
allPodcasts(locale: ja) {
id
title
}
}
means: fetch all podcasts and for each podcast, get me the id
and the title
. If title
has a japanese content, show me that content in japanese, otherwise returns me an empty string.
If you wanna retrieve only the podcasts with a title in japanese, then you need to filter like in the previous post
The filter that rejects records with an empty title does not seem to work anymore.
But using the filter filter: {title: {isPresent: true}}
does work.
Another filter that also works is: {_locales: {allIn: [ja]}
. In my case I use {_locales: {allIn: [$locale]}
and provide the locale as a variable that I use both for the locale
property and for the filter:
query MyQuery($locale: SiteLocale!) {
allSomerecords(
locale: $locale
filter: {_locales: {allIn: [$locale]}}
) {
âŚ
}
}
This is how I tested the filter: filter: {title: {neq: ""}}
- Create a new model called LocaleTest
- Add the localizable, single line field
title
- Create two records, one in English with the title âOnly Englishâ and one in two languages (in my case English and Swedish) with the title âMultilingualâ in both languages
- Run the below query and note that both records are returned, one with an empty title
query LocaleTest {
allLocaletests(
locale: sv
orderBy: _firstPublishedAt_DESC
filter: {title: {neq: ""}}
) {
title
}
}