Skip to content

Commit

Permalink
feat: add requireLocale option to allow working with non-translatable…
Browse files Browse the repository at this point in the history
… page types even with i18n enabled (#57)

* feat: add requireLocale option to allow working with non-translatable page types even with i18n enabled

* fix: add requireLocale field to PagesNavigatorPluginOptions

* chore: add changeset
  • Loading branch information
marcusforsberg authored Jun 4, 2024
1 parent 29a848a commit ac331cf
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/slimy-keys-check.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@tinloof/sanity-studio": minor
---

Add requireLocale option to allow working with non-translatable page types even with i18n enabled. Thanks @marcusforsberg!
17 changes: 17 additions & 0 deletions packages/sanity-studio/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,23 @@ export default defineType({
});
```

#### Support documents without a locale

By default, when internationalization is enabled, only pages whose `locale` field matches the currently selected locale will be shown in the list. If you have page types that are not translated but you still want them to show up in the list, you can set the `requireLocale` option to false in your `i18n` config:

```ts
const i18nConfig = {
locales: [
{ id: "en", title: "English" },
{ id: "fr", title: "French" },
],
defaultLocaleId: "en",
requireLocale: false,
};
```

Now all documents with a `pathname` field will show up in the list regardless of the filtered locale, even if they don't have a `locale` field (or their `locale` is `null`).

### Lock folder renaming

By default, folders can be renamed. Set the `folder.canUnlock` option to `false` to disable this.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,11 @@ export const NavigatorProvider = ({
const [state, dispatch] = useReducer(reducer, initialState);

const filteredData = i18nEnabled
? data.filter((page) => page.locale === state.locale)
? data.filter(
(page) =>
page.locale === state.locale ||
(!page.locale && i18n.requireLocale === false)
)
: data;

const rootTree = searchTree({
Expand Down
2 changes: 2 additions & 0 deletions packages/sanity-studio/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export type PagesNavigatorOptions = {
i18n?: {
locales: Locale[];
defaultLocaleId?: string;
requireLocale?: boolean;
localizePathname?: LocalizePathnameFn;
};
creatablePages?: Array<NormalizedCreatablePage>;
Expand All @@ -34,6 +35,7 @@ export type PagesNavigatorPluginOptions = PresentationPluginOptions & {
i18n?: {
locales: Locale[];
defaultLocaleId?: string;
requireLocale?: boolean;
localizePathname?: LocalizePathnameFn;
};
navigator?: Pick<PresentationNavigatorOptions, "maxWidth" | "minWidth">;
Expand Down

0 comments on commit ac331cf

Please sign in to comment.