Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to customize the context locale (custom directory structure) #8

Open
gremo opened this issue Feb 15, 2021 · 4 comments · May be fixed by #9
Open

Ability to customize the context locale (custom directory structure) #8

gremo opened this issue Feb 15, 2021 · 4 comments · May be fixed by #9

Comments

@gremo
Copy link

gremo commented Feb 15, 2021

My directory structure doesn't have the folder named "it" (my default / fallback locale) because I'd like to not prefixing my default locale. The plugin, however, will complain about undefined locale,

[i18n] Could not find 'foo' in 'prodotti'. Using 'it' fallback.

Here the products is simple the page url (/prodotti).

I'll make a PR for it.

@gremo gremo linked a pull request Feb 15, 2021 that will close this issue
@adamduncan
Copy link
Owner

adamduncan commented Feb 19, 2021

Interesting. This seems partly related to a suggestion from @unconfident re: localeContext.

I'm trying to get a better idea of how.

Can you please share an example of how you'd leverage the contextLocale function in your case?

@gremo
Copy link
Author

gremo commented Feb 22, 2021

Here is my configuration for your plugin:

  eleventyConfig.addPlugin(require('eleventy-plugin-i18n'), {
    translations: glob.sync('./translations/*.json')
      .map(pathname => ({ key: path.basename(pathname, '.json'), contents: require(pathname) }))
      .reduce((prev, curr) => ({...prev, [curr.key]: curr.contents }), {}),
    fallbackLocales: {
      '*': 'it'
    },
    contextLocale: (page, config) => {
      // Test if string starts with a locale prefix (i.e. /en/ or /fr/ with a leading slash)
      const isLocalizedUrl = /^\/([\w]{2})\//g.exec((page && page.url) || '');
      if (isLocalizedUrl) {
        return isLocalizedUrl[1];
      }

      // ... otherwise assume it's the fallback locale
      return config.fallbackLocales['*'];
    },
  });

@gremo
Copy link
Author

gremo commented Feb 26, 2021

@adamduncan any update on this? Have you seen my PR, what do you think?

@adamduncan
Copy link
Owner

Hey @gremo. We need to patch something else with the library (#10), so will try to absorb this properly when we do that 👍

@adamduncan adamduncan linked a pull request Mar 4, 2021 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants