question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. ItΒ collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support for localization (e.g. for Contentful)

See original GitHub issue

While I guess that it is currently possible to localize MDX based on frontmatter tags, doing this would mean that the filenames would need to be different, resulting in different slugs which would certainly not be ideal. I guess there are other ways to achieve it, but none of them seem ideal, that’s why it would be great to support l10n (and also just because Contentlayers goal is to be the abstraction layer between data and code).

I assume that there could be several ways for structuring the MDX files, but what comes to my mind first is either via file name eg. /pages/about.mdx /pages/about.de.mdx or via subfolders eg. /pages/en/about.mdx /pages/de/about.mdx.

Edit: the following structure seems pretty nice, but would depend on #11

β”œβ”€β”€ data
β”‚   β”œβ”€β”€ pages
β”‚   β”‚   β”œβ”€β”€ about
β”‚   β”‚   β”‚   β”œβ”€β”€ index.mdx
β”‚   β”‚   β”‚   β”œβ”€β”€ index.de.mdx
β”‚   β”‚   β”‚   β”œβ”€β”€ picture.jpeg
β”‚   β”‚   β”œβ”€β”€ privacy
β”‚   β”‚   β”‚   β”œβ”€β”€ index.mdx
β”‚   β”‚   β”‚   β”œβ”€β”€ index.de.mdx
β”‚   β”‚   β”‚   β”œβ”€β”€ cover.jpeg

For Contentful one just needs to add &locale= https://www.contentful.com/developers/docs/references/content-delivery-api/#/reference/localization

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:10

github_iconTop GitHub Comments

5reactions
timlrxcommented, Dec 30, 2021

@rsipakov, here’s a psuedocode assuming β€˜en’ as the default:

const getLocale = (path) => {
  const pathArray = path.split('.')
  return pathArray.length > 2 ? pathArray.slice(-2)[0] : 'en'
}

// In contentlayer.config.js
export const Post = defineDocumentType(() => ({
  name: 'Post',
  fields: {
   . . .
  },
  computedFields: {
    locale: {
      type: 'string',
      resolve: (doc) => {
        return getLocale(doc._raw.sourceFilePath)
      },
    },
  },
}))

This would generate a new locale field for each post which you can use in your pages/routing.

1reaction
schicklingcommented, Nov 8, 2021

Thanks a lot for the update @henniaufmrenni. I’ll keep this issue open since some CMS (e.g. Contentful) have a first-class localization feature that’s currently not yet supported by Contentlayer but should be.

I also agree that we should better document how to implement localization/internationalization scenarios when using the source-files plugin. (cc @seancdavis)

Read more comments on GitHub >

github_iconTop Results From Across the Web

Localization with Contentful
In this tutorial, creation, management, and enablement of locales is explained | Localization with Contentful documentation.
Read more >
Best Practices for Contentful Localization | GPI Translation Blog
For example, if you are adding Spanish (Spain) as the locale and you select English (US) as the fallback, then, if there is...
Read more >
Headless CMS Localization with Phrase and Contentful
Our demo app: RBrite. We'll create a simple React app to walk through a working example of localized content with Phrase and Contentful....
Read more >
Contentful - Lokalise Docs
Currenty we do not support Contentful environments and always revert to using ... Select and download the required content types for further localization...
Read more >
Lokalise + Contentful: best practices for using a ... - YouTube
Contentful makes it easy to manage, publish, and scale your content ... session on how to use a headless CMS in your localization...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found