Open gremo opened 3 years ago
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?
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['*'];
},
});
@adamduncan any update on this? Have you seen my PR, what do you think?
Hey @gremo. We need to patch something else with the library (#10), so will try to absorb this properly when we do that 👍
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,Here the products is simple the page url (
/prodotti
).I'll make a PR for it.