Skip to content

Commit ddce75e

Browse files
committed
feat: allows an array to be passed to fallbackLocale
1 parent c7795fa commit ddce75e

File tree

9 files changed

+310
-15
lines changed

9 files changed

+310
-15
lines changed

docs/configuration/localization.mdx

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,12 @@ The locale codes do not need to be in any specific format. It's up to you to def
9393

9494
#### Locale Object
9595

96-
| Option | Description |
97-
| -------------------- | ------------------------------------------------------------------------------------------------------------------------------ |
98-
| **`code`** \* | Unique code to identify the language throughout the APIs for `locale` and `fallbackLocale` |
99-
| **`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. |
100-
| **`rtl`** | A boolean that when true will make the admin UI display in Right-To-Left. |
101-
| **`fallbackLocale`** | The code for this language to fallback to when properties of a document are not present. |
96+
| Option | Description |
97+
| -------------------- | ----------------------------------------------------------------------------------------------------------------------------------------- |
98+
| **`code`** \* | Unique code to identify the language throughout the APIs for `locale` and `fallbackLocale` |
99+
| **`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. |
100+
| **`rtl`** | A boolean that when true will make the admin UI display in Right-To-Left. |
101+
| **`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. |
102102

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

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

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

227227
**Example:**
228228

@@ -247,7 +247,7 @@ query {
247247

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

252252
**Example:**
253253

docs/local-api/overview.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ You can specify more options within the Local API vs. REST or GraphQL due to the
7171
| `locale` | Specify [locale](/docs/configuration/localization) for any returned documents. |
7272
| `select` | Specify [select](../queries/select) to control which fields to include to the result. |
7373
| `populate` | Specify [populate](../queries/select#populate) to control which fields to include to the result from populated documents. |
74-
| `fallbackLocale` | Specify a [fallback locale](/docs/configuration/localization) to use for any returned documents. |
74+
| `fallbackLocale` | Specify a [fallback locale](/docs/configuration/localization) to use for any returned documents. This can be a single locale or array of locales. |
7575
| `overrideAccess` | Skip access control. By default, this property is set to true within all Local API operations. |
7676
| `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). |
7777
| `user` | If you set `overrideAccess` to `false`, you can pass a user to use against the access control checks. |

packages/payload/src/collections/operations/local/find.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export type Options<TSlug extends CollectionSlug, TSelect extends SelectType> =
5454
/**
5555
* Specify a [fallback locale](https://payloadcms.com/docs/configuration/localization) to use for any returned documents.
5656
*/
57-
fallbackLocale?: false | TypedLocale
57+
fallbackLocale?: false | TypedLocale | TypedLocale[]
5858
/**
5959
* Include info about the lock status to the result into all documents with fields: `_isLocked` and `_userEditing`
6060
*/

packages/payload/src/collections/operations/local/findByID.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ export type Options<
6262
/**
6363
* Specify a [fallback locale](https://payloadcms.com/docs/configuration/localization) to use for any returned documents.
6464
*/
65-
fallbackLocale?: false | TypedLocale
65+
fallbackLocale?: false | TypedLocale | TypedLocale[]
6666
/**
6767
* The ID of the document to find.
6868
*/

packages/payload/src/config/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,7 @@ export type Locale = {
462462
/**
463463
* Code of another locale to use when reading documents with fallback, if not specified defaultLocale is used
464464
*/
465-
fallbackLocale?: string
465+
fallbackLocale?: string | string[]
466466
/**
467467
* label of supported locale
468468
* @example "English"

packages/payload/src/fields/hooks/afterRead/promise.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,31 @@ export const promise = async ({
158158
let hoistedValue = value
159159

160160
if (fallbackLocale && fallbackLocale !== locale) {
161-
const fallbackValue = siblingDoc[field.name!][fallbackLocale]
161+
let fallbackValue
162162
const isNullOrUndefined = typeof value === 'undefined' || value === null
163+
const fallbackIsArray =
164+
Array.isArray(fallbackLocale) ||
165+
(fallbackLocale.startsWith('[') && fallbackLocale.endsWith(']'))
166+
167+
if (fallbackIsArray) {
168+
const formattedFallback = Array.isArray(fallbackLocale)
169+
? fallbackLocale
170+
: fallbackLocale
171+
.slice(1, -1)
172+
.split(',')
173+
.map((l) => l.trim())
174+
175+
for (const locale of formattedFallback) {
176+
const val = siblingDoc[field.name!]?.[locale]
177+
178+
if (val !== undefined && val !== null && val !== '') {
179+
fallbackValue = val
180+
break
181+
}
182+
}
183+
} else {
184+
fallbackValue = siblingDoc[field.name!][fallbackLocale]
185+
}
163186

164187
if (fallbackValue) {
165188
switch (field.type) {

packages/payload/src/globals/operations/local/findOne.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export type Options<TSlug extends GlobalSlug, TSelect extends SelectType> = {
3737
/**
3838
* Specify a [fallback locale](https://payloadcms.com/docs/configuration/localization) to use for any returned documents.
3939
*/
40-
fallbackLocale?: false | TypedLocale
40+
fallbackLocale?: false | TypedLocale | TypedLocale[]
4141
/**
4242
* Include info about the lock status to the result with fields: `_isLocked` and `_userEditing`
4343
*/

test/helpers/NextRESTClient.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ type RequestOptions = {
1919
auth?: boolean
2020
query?: { [key: string]: unknown } & {
2121
depth?: number
22-
fallbackLocale?: string
22+
fallbackLocale?: string | string[]
2323
joins?: JoinQuery
2424
limit?: number
2525
locale?: string

0 commit comments

Comments
 (0)