mirror of https://github.com/alibaba/ice.git
* fix: compat with the naviator language return with underslash * Update CHANGELOG.md |
||
---|---|---|
.. | ||
src | ||
templates | ||
CHANGELOG.md | ||
README.md | ||
package.json | ||
runtime.d.ts | ||
tsconfig.json | ||
types.d.ts |
README.md
@ice/plugin-intl
@ice/plugin-intl
is a ice.js plugin. It provides a simple way to add internationalization support to your application.
@ice/plugin-intl
is based onreact-intl
.
Install
$ npm i @ice/plugin-intl --save-dev
Usage
Define the plugin in ice.config.mts
:
import { defineConfig } from '@ice/app';
import intl from '@ice/plugin-intl';
export default defineConfig({
plugins: [
intl(),
],
});
Define locale config in src/app.ts
:
import { defineAppConfig } from 'ice';
import type { LocaleConfig } from '@ice/plugin-intl/types';
export default defineAppConfig(() => ({}));
export const locale: LocaleConfig = {
// Cutomize getLocale method and other options supported by react-intl.
getLocale: () => 'en-US',
};
Locales
Locales are defined in the src/locales
directory. Each locale is defined in a separate file, with the locale name as the file name. For example, en-US.ts
:
export default {
'app.title': 'My Application',
'app.welcome': 'Welcome to my application!',
};
Use the useIntl
hook to access the current locale:
import { useIntl } from 'ice';
export default function Home() {
const intl = useIntl();
console.log(intl.formatMessage({ id: 'new' }));
return <h1>home</h1>;
}
Use the intl
function to access the current locale:
import { intl } from 'ice';
function alertMessage() {
alert(intl.formatMessage({ id: 'app.welcome' }));
}