Localized website template #9997
Replies: 4 comments 3 replies
-
This is definitely much needed. The localization docs are really confusing as they stand. There's no clear, step-by-step guide to walk users through setting up a fully localized website. The documentation mentions various necessary steps, but it doesn't explain how to actually implement them. It would be a huge improvement if, when you enable localization in the config of a fresh website with the default demo data, a script would automatically update the field configurations of the existing structure. Even better, that script should also add a basic language select component so users can immediately start testing the localization. That would make the whole process so much smoother and more intuitive. |
Beta Was this translation helpful? Give feedback.
-
I would love this! I’m even willing to settle for better documentation or a tutorial, but this feature would be a dream. |
Beta Was this translation helpful? Give feedback.
-
I added {
name: 'url',
localized: true,
...
} to I couldn't find I was not able to print the current language's navigation bar in the header. I'm still trying to figure it out. |
Beta Was this translation helpful? Give feedback.
-
@vatro thank you for your assistance. I successfully translated the website template. It took some time as I needed to modify numerous payload components, but the result turned out well. I'll share the repository once I've finished adding some final touches. |
Beta Was this translation helpful? Give feedback.
-
An additional, localized website template (based on the single language template) would be awesome:
Best practices concerning collections & fields localization, e.g. what should be localized in different scenarios incl. caveats.
Preserving content structure across languages (where to / not add the
localized
property)Different content structure per language (where to / not add the
localized
property)Introduce possible (common / best) localization strategies
SEO remark: SEO meta is localized out-of-the-box, not need to modify anything, see Metadata in Docs
Examples of field migration strategies when changing the
localized
property, see corresponding remark in docs: https://payloadcms.com/docs/configuration/localizationBest practice development workflows for maximum flexibility / painless modifications (e.g. editing / replacing components of a website that already has a ton of content added across multiple languages).
The most awesome would be a new CLI command, like e.g.
npx create-payload-app -n my-project -t website --localized en,xx
generating a basic localized website template with specified languages already set up "properly" (default: preserve content layout across languages) + an optional flag--preserve yes | no
(default:yes
).Robust and flexible localization options are crucial for any company / website based in a non-English speaking country doing international business.
I'm currently converting the single language website template to a localized one, these are the things I had to do / touch so far, not mentioned in the Docs (https://payloadcms.com/docs/configuration/localization) - will update it as I go forward:
Localized Frontend
Use dynamic
[lang]
route: move the contents ofsrc/app/(frontend)/
intosrc/app/(frontend)/[lang]/
Modify
src/app/(frontend)/[lang]/layout.tsx
to setlang
attribute on<html>
:Modify
src/app/(frontend)/[lang]/[slug]/page.tsx
to use thelang
param in:Page
: uselang
generateMetadata
: uselang
queryPageBySlug
: uselang
and setlocale
tolang
generateStaticParams
: setlocale
inpayload.find
to fetch a language / fetch multiple languages.Localized Slugs
src/fields/slug/index.ts
const slugField: TextField = { name: 'slug', type: 'text', + localized: true,
Localized Live Preview / Preview
Modify
src/utilities/generatePreviewPath.ts
Localized Header / Navigation
src/fields/link.ts
localized
label
, customurl
andreference
fieldsname: 'label', type: 'text', + localized: true,
name: 'url', type: 'text', + localized: true,
name: 'reference', type: 'relationship', + localized: true,
optionally localize the whole
header
if you need different nav menu per language:src/Header/config.ts
fields: [ { name: 'navItems', type: 'array', + localized: true, // allow different nav items per locale
src/app/(frontend)/[lang]/layout.tsx
src/utilities/getGlobals.ts
use
locale
and exportgetGlobal
because we need uncachedheader
data insrc/Header/Component.tsx
src/Header/Component.tsx
use
locale
and uncachedheader
data in order to get correct data on locale change.👆 not sure about using
getGlobal
/ not usinggetCachedGlobal
, but it works.Drill down
locale
toCMSLink
ComponentFor localized relational (
reference
) link-urls, e.g.<host>//[locale]/[slug]
instead of<host>//[slug]
.src/Header/Component.tsx
src/Header/Component.client.tsx
src/Header/Nav/index.tsx
src/components/Link/index.tsx
(CMSLink
Component)Beta Was this translation helpful? Give feedback.
All reactions