ice/packages/plugin-intl
ClarkXia 8780bc8269
Fix: compat with the navigator language return with underslash (#6907)
* fix: compat with the naviator language return with underslash

* Update CHANGELOG.md
2024-06-05 12:22:08 +08:00
..
src Fix: compat with the navigator language return with underslash (#6907) 2024-06-05 12:22:08 +08:00
templates feat: support get locale messages from global (#6898) 2024-05-29 17:16:08 +08:00
CHANGELOG.md Fix: compat with the navigator language return with underslash (#6907) 2024-06-05 12:22:08 +08:00
README.md Feat: plugin for intl (#6863) 2024-05-06 09:56:22 +08:00
package.json Fix: compat with the navigator language return with underslash (#6907) 2024-06-05 12:22:08 +08:00
runtime.d.ts Feat: plugin for intl (#6863) 2024-05-06 09:56:22 +08:00
tsconfig.json Feat: plugin for intl (#6863) 2024-05-06 09:56:22 +08:00
types.d.ts Feat: plugin for intl (#6863) 2024-05-06 09:56:22 +08:00

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 on react-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' }));
}