adamduncan / eleventy-plugin-i18n

Eleventy plugin to assist with internationalization and dictionary translations
MIT License
104 stars 10 forks source link

Serving one language version at the root of the website and the others in separate subdirectories #3

Closed fabiomrbarbosa closed 3 years ago

fabiomrbarbosa commented 4 years ago

I was attempting to restructure my website's output tree by outputting the main language (in this case, English) into the root directory and all other translations into their respective subdirectory, named after their locale — I understand this might be a questionable approach, but this started because I wanted to avoid forcing a redirect on most of my visitors.

However, setting the permalinks to anything other than a subdirectory starting with the same key as the locale (therefore, if I wish to serve English content anywhere but under /en ) produces a terminal error stating that Eleventy cannot find the translation entry anywhere the i18n filter is invoked, which means it has to rely on the fallback language.

I've gone as far as creating a separate variable to handle the creation of permalinks (if a translation, output a subdir slug, if English, output nothing); still, as long as the slug I pass doesn't match the locale key exactly, the same error is produced.

Am I perhaps overlooking something here? Thanks in advance.

adamduncan commented 4 years ago

Hey @fabiomrbarbosa. Interesting approach. So you'd have English language site at / and then other language sites at their respective subdirectories (/pt/)?

Understand the desire to avoid a redirect. Did you make any ground on this one? One might consider a fork of this plugin that handled the locale deduction more explicitly. I.e. could check the contextLocale matched one of the "supporting" language site's locale codes, if not, fell back to something explicit 🤔

fabiomrbarbosa commented 3 years ago

Hey! I ended up dropping the plugin on the website version I'm currently working on.

Basically I kept the structure of the dictionary .json file I was using before and now I'm declaring the localised strings with native Nunjucks like so: {{ dictionary.home.welcome[ locale ]}}, where locale is defined on a data file in the root of each locale's content folder. This data file also declares what the subdirectory looks like (/pt or just /?) and I adjusted all urls with that extra subDir variable accordingly.

adamduncan commented 3 years ago

Great, glad you found a solution that works 👍 These alternate use-cases will be ones to consider as the plugin matures, so thanks for raising.