@gasket/intl
Provides internationalization managers for translation files and locale handling.
Installation
npm i @gasket/intl @gasket/plugin-intl
See @gasket/plugin-intl for more information on how to configure the plugin.
Usage
With a intl.js
built by @gasket/plugin-intl, you can use the IntlManager to
get messages for locales.
Example messages for a Locale
import intlManager from '../path/to/intl.js';
const localeHandler = intlManager.handleLocale('en-US');
const messages = localeHandler.getAllMessages();
Example resolving a supported locale
The IntlManager can also be used to resolve locale based on the supported locales that have been configured for
@gasket/plugin-intl.
import intlManager from '../path/to/intl.js';
const resolvedLocale = intlManager.resolveLocale('fr-CA');
To list all supported locales, you can use:
import intlManager from '../path/to/intl.js';
const supportedLocales = intlManager.locales;
Advanced Usage
While the above examples cover the most common use cases, the @gasket/intl
package also provides advanced features for
managing locale files and their loading status. These are useful for scenarios where the React components from
@gasket/react-intl are not available or when you need to manage locale files directly.
Loading Multiple Locale Files
import intlManager from '../path/to/intl.js';
const localeHandler = intlManager.handleLocale('en-US');
await localeHandler.load(
'locales/common',
'locales/homepage',
'locales/user-profile'
);
const messages = localeHandler.getAllMessages();
Checking Loading Status
import intlManager from '../path/to/intl.js';
import { LocaleFileStatus } from '@gasket/intl';
const localeHandler = intlManager.handleLocale('en-US');
localeHandler.load('locales/common');
const status = localeHandler.getStatus('locales/common');
if (status === LocaleFileStatus.loading) {
console.log('Locale file is still loading...');
} else if (status === LocaleFileStatus.loaded) {
console.log('Locale file has been loaded successfully!');
} else if (status === LocaleFileStatus.error) {
console.error('Failed to load locale file');
}
Using Static Locale File Paths
The staticLocaleFilePaths
configuration in @gasket/plugin-intl allows you to specify which locale files should be
preloaded for server-side rendering (SSR) and available immediately when the app starts.
Configuration
Configure staticLocaleFilePaths
in your gasket.js
:
export default makeGasket({
intl: {
locales: ['en-US', 'fr-FR'],
staticLocaleFilePaths: [
'locales/common',
'locales/navigation',
'locales/errors'
]
}
});
Server-Side Rendering
import intlManager from '../path/to/intl.js';
const localeHandler = intlManager.handleLocale('en-US');
await localeHandler.loadStatics();
await localeHandler.loadStatics('locales/common', 'locales/navigation');
const staticsRegister = localeHandler.getStaticsRegister();
License
MIT