sveltekit-i18n / lib

Internationalization library built for SvelteKit.
MIT License
447 stars 28 forks source link

Non-standard locale provided warning (mn case) #109

Closed mckabi closed 1 year ago

mckabi commented 1 year ago

(It does not duplicate #75)

I received the following warning:

[18n]: Non-standard locale provided: 'mn'. Check your 'translations' and 'loaders' in i18n config...

'mn' is Mongolian (Монгол) which is also in the IANA Language subtag registry

  // base/src/utils.ts
  return locales.filter((locale) => !!locale).map((locale) => {
    let current = `${locale}`.toLowerCase();
    try {
      const [sanitized] = Intl.Collator.supportedLocalesOf(locale);

      if (!sanitized) throw new Error(`'${locale}' is non-standard.`);

      current = sanitized;
    } catch (error) {
      logger.warn(`Non-standard locale provided: '${locale}'. Check your 'translations' and 'loaders' in i18n config...`);
    }

    return current;
  });
Intl.Collator.supportedLocalesOf(['mn', 'ko', 'en'])
// ['ko', 'en'] :-(
arnthor3 commented 1 year ago

I had the same problem with is, I used a noop for console.warn on prod to remove this.

if (process.env.NODE_ENV === 'production') {
 console.warn = () => {}
}
jarda-svoboda commented 1 year ago

As far as the mn locale is not supported by Intl.Collator.supportedLocalesOf you could avoid console warnings for production like this:

import type { Config } from 'sveltekit-i18n';

const config: Config = {
  // ...
  log: {
    level: process.env.NODE_ENV === 'production' ? 'error' : 'warn',
  }
};
baisong commented 9 months ago

Hello, thank you @jarda-svoboda for this solution.

Where is the recommended place to add this warning suppression configuration? Just once, inside /src/lib/translations/index.js?

Is there anything different recommended for non-typescript implementation?

jarda-svoboda commented 9 months ago

Hi @baisong!

Yes, the right place to avoid warning logs is log setting in your config (you can place this config wherever you want, just import it for this lib).

There is no difference in comparison to typescript.