next-translate-plugin icon indicating copy to clipboard operation
next-translate-plugin copied to clipboard

Can imported config data from i18n.js be awaited?

Open rolandjlevy opened this issue 2 years ago • 4 comments

What version of this package are you using? 2.0.5

What problem do you want to solve?

In the i18n.js config file I want to make the locales array dynamic by getting the array from an API call to a Postgres db table. So when I use module.exports instead of exporting an object, I want to await a function which includes that API call.

I don't know how i18n.js is imported but is awaiting the config data from i18n.js already possible? If not, could it be added?

I have attempted the code below but get this error: "Error: Specified i18n.defaultLocale should be included in i18n.locales."

// contents of i18n.js
const i18nNextTranslateConfig = async () => {
  try {
    const allDomains = await getLocalesForDomains(); // API call to get domain data
    const allLocales = allDomains.map((item) => item.defaultLocale);
    const locales = [...new Set(allLocales)];
    const defaultLocale = 'en-gb';
    if (!locales.includes(defaultLocale)) {
      locales.push(defaultLocale);
    }
    return {
      locales,
      defaultLocale,
      extensionsRgx: /\.(tsx|ts|js|mjs|jsx)$/,
      keySeparator: '.',
      pages: {
        // Namespaces to import for each page
        '*': ['common', 'app'],
        '/': ['home'],
        'rgx:^/search.*': ['search'],
        '/basket': ['basket'],
        '/account/overview': ['accountMenu'],
        '/shop/[[...Shop]]': ['shopCategory', 'shopProduct', 'shopFamily'],
        '/brands/[brandLetter]': ['brands'],
      },
    };
  } catch (err) {
    return err;
  }
};

module.exports = (async () => {
  const result = await i18nNextTranslateConfig();
  return result;
})();

What do you think is the correct solution to this problem? Can the imported config data from i18n.js be awaited?

Are you willing to submit a pull request to implement this change? I don't know enough about the package to implement any changes but I am willing to help in any way I can. Thank you

rolandjlevy avatar Jun 13 '23 09:06 rolandjlevy

I realised that is not supported. Feel free to do a PR to support this feature. You need to touch this line of code:

https://github.com/aralroca/next-translate-plugin/blob/9ad6592f10cfa90da4f7d0bc20032831a220b6af/src/index.ts#L78

I'm going to move this issue to next-translate-plugin.

aralroca avatar Jun 13 '23 11:06 aralroca

Hi Aral Roca Gomez,

Thanks so much for getting back to me. I will give it a try. Any tips would be very welcome.

Do you know where I can find an explanation of how to run the project locally after forking it?

Many thanks, Roland

rolandjlevy avatar Jun 13 '23 14:06 rolandjlevy

Thanks @rolandjlevy, I recommend you start with the failing test and do "yarn test". In order to try inside a local project, you need to run "yarn build", and in your side project instead of using a specific next-translate-plugin version in the package.json you can put a symbolic link of yarn and run your project. Another way is to use one of the examples inside the next-translate repo.

aralroca avatar Jun 13 '23 15:06 aralroca

Hi @aralroca,

That's great - thanks so much for your advice. I will take a look and let you know how I get on.

Roland

rolandjlevy avatar Jun 14 '23 16:06 rolandjlevy