Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions docs/configuration/localization.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,12 @@ The locale codes do not need to be in any specific format. It's up to you to def

#### Locale Object

| Option | Description |
| -------------------- | ------------------------------------------------------------------------------------------------------------------------------ |
| **`code`** \* | Unique code to identify the language throughout the APIs for `locale` and `fallbackLocale` |
| **`label`** | A string to use for the selector when choosing a language, or an object keyed on the i18n keys for different languages in use. |
| **`rtl`** | A boolean that when true will make the admin UI display in Right-To-Left. |
| **`fallbackLocale`** | The code for this language to fallback to when properties of a document are not present. |
| Option | Description |
| -------------------- | ----------------------------------------------------------------------------------------------------------------------------------------- |
| **`code`** \* | Unique code to identify the language throughout the APIs for `locale` and `fallbackLocale` |
| **`label`** | A string to use for the selector when choosing a language, or an object keyed on the i18n keys for different languages in use. |
| **`rtl`** | A boolean that when true will make the admin UI display in Right-To-Left. |
| **`fallbackLocale`** | The code for this language to fallback to when properties of a document are not present. This can be a single locale or array of locales. |

_\* An asterisk denotes that a property is required._

Expand Down Expand Up @@ -222,7 +222,7 @@ The `locale` arg will only accept valid locales, but locales will be formatted a
values (dashes or special characters will be converted to underscores, spaces will be removed, etc.). If you are curious
to see how locales are auto-formatted, you can use the [GraphQL playground](/docs/graphql/overview#graphql-playground).

The `fallbackLocale` arg will accept valid locales as well as `none` to disable falling back.
The `fallbackLocale` arg will accept valid locales, an array of locales, as well as `none` to disable falling back.

**Example:**

Expand All @@ -247,7 +247,7 @@ query {

You can specify `locale` as well as `fallbackLocale` within the Local API as well as properties on the `options`
argument. The `locale` property will accept any valid locale, and the `fallbackLocale` property will accept any valid
locale as well as `'null'`, `'false'`, `false`, and `'none'`.
locale, array of locales, as well as `'null'`, `'false'`, `false`, and `'none'`.

**Example:**

Expand Down
2 changes: 1 addition & 1 deletion docs/local-api/overview.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ You can specify more options within the Local API vs. REST or GraphQL due to the
| `locale` | Specify [locale](/docs/configuration/localization) for any returned documents. |
| `select` | Specify [select](../queries/select) to control which fields to include to the result. |
| `populate` | Specify [populate](../queries/select#populate) to control which fields to include to the result from populated documents. |
| `fallbackLocale` | Specify a [fallback locale](/docs/configuration/localization) to use for any returned documents. |
| `fallbackLocale` | Specify a [fallback locale](/docs/configuration/localization) to use for any returned documents. This can be a single locale or array of locales. |
| `overrideAccess` | Skip access control. By default, this property is set to true within all Local API operations. |
| `overrideLock` | By default, document locks are ignored (`true`). Set to `false` to enforce locks and prevent operations when a document is locked by another user. [More details](../admin/locked-documents). |
| `user` | If you set `overrideAccess` to `false`, you can pass a user to use against the access control checks. |
Expand Down
2 changes: 1 addition & 1 deletion packages/payload/src/collections/operations/local/find.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export type Options<TSlug extends CollectionSlug, TSelect extends SelectType> =
/**
* Specify a [fallback locale](https://payloadcms.com/docs/configuration/localization) to use for any returned documents.
*/
fallbackLocale?: false | TypedLocale
fallbackLocale?: false | TypedLocale | TypedLocale[]
/**
* Include info about the lock status to the result into all documents with fields: `_isLocked` and `_userEditing`
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export type Options<
/**
* Specify a [fallback locale](https://payloadcms.com/docs/configuration/localization) to use for any returned documents.
*/
fallbackLocale?: false | TypedLocale
fallbackLocale?: false | TypedLocale | TypedLocale[]
/**
* The ID of the document to find.
*/
Expand Down
2 changes: 1 addition & 1 deletion packages/payload/src/config/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,7 @@ export type Locale = {
/**
* Code of another locale to use when reading documents with fallback, if not specified defaultLocale is used
*/
fallbackLocale?: string
fallbackLocale?: string | string[]
/**
* label of supported locale
* @example "English"
Expand Down
15 changes: 14 additions & 1 deletion packages/payload/src/fields/hooks/afterRead/promise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,22 @@ export const promise = async ({
let hoistedValue = value

if (fallbackLocale && fallbackLocale !== locale) {
const fallbackValue = siblingDoc[field.name!][fallbackLocale]
let fallbackValue
const isNullOrUndefined = typeof value === 'undefined' || value === null

if (Array.isArray(fallbackLocale)) {
for (const locale of fallbackLocale) {
const val = siblingDoc[field.name!]?.[locale]

if (val !== undefined && val !== null && val !== '') {
fallbackValue = val
break
}
}
} else {
fallbackValue = siblingDoc[field.name!][fallbackLocale]
}

if (fallbackValue) {
switch (field.type) {
case 'text':
Expand Down
2 changes: 1 addition & 1 deletion packages/payload/src/globals/operations/local/findOne.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export type Options<TSlug extends GlobalSlug, TSelect extends SelectType> = {
/**
* Specify a [fallback locale](https://payloadcms.com/docs/configuration/localization) to use for any returned documents.
*/
fallbackLocale?: false | TypedLocale
fallbackLocale?: false | TypedLocale | TypedLocale[]
/**
* Include info about the lock status to the result with fields: `_isLocked` and `_userEditing`
*/
Expand Down
18 changes: 16 additions & 2 deletions packages/payload/src/utilities/sanitizeFallbackLocale.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { SanitizedLocalizationConfig } from '../config/types.js'
import type { TypedLocale } from '../index.js'

interface Args {
fallbackLocale: false | TypedLocale
fallbackLocale: false | TypedLocale | TypedLocale[]
locale: string
localization: SanitizedLocalizationConfig
}
Expand All @@ -26,7 +26,10 @@ export const sanitizeFallbackLocale = ({
hasFallbackLocale = Boolean(localization && localization.fallback)
}

if (fallbackLocale && !['false', 'none', 'null'].includes(fallbackLocale)) {
if (
fallbackLocale &&
!['false', 'none', 'null'].includes(!Array.isArray(fallbackLocale) ? fallbackLocale : '')
) {
hasFallbackLocale = true
}

Expand All @@ -52,5 +55,16 @@ export const sanitizeFallbackLocale = ({
fallbackLocale = null
}

if (
typeof fallbackLocale === 'string' &&
fallbackLocale.startsWith('[') &&
fallbackLocale.endsWith(']')
) {
fallbackLocale = fallbackLocale
.slice(1, -1)
.split(',')
.map((l) => l.trim())
}

return fallbackLocale as null | string
}
2 changes: 1 addition & 1 deletion test/helpers/NextRESTClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type RequestOptions = {
auth?: boolean
query?: { [key: string]: unknown } & {
depth?: number
fallbackLocale?: string
fallbackLocale?: string | string[]
joins?: JoinQuery
limit?: number
locale?: string
Expand Down
Loading