ice/packages/plugin-intl
ClarkXia a7a885d9a3
Feat: refactor ice runtime (#7044)
* feat: refactor ice runtime

* fix: update scripts

* fix: unique typescript version

* fix: lauch pupeteer with no-sandbox

* feat: support custom runtime

* feat: runtime kit

* fix: update api

* fix: add file for export path

* fix: test

* fix: update type import source
2025-02-11 13:55:37 +08:00
..
src Feat: refactor ice runtime (#7044) 2025-02-11 13:55:37 +08:00
templates feat: support get locale messages from global (#6898) 2024-05-29 17:16:08 +08:00
CHANGELOG.md chore: update versions (#6939) 2024-07-25 10:27:46 +08:00
README.md feat: support the simple solution of intl (#6953) 2024-07-25 10:10:02 +08:00
package.json chore: update versions (#6939) 2024-07-25 10:27:46 +08:00
runtime-simple.d.ts feat: support the simple solution of intl (#6953) 2024-07-25 10:10:02 +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' }));
}

Simple mode

Simple mode for remove the dependency of react-intl:

Define the plugin in ice.config.mts:

import { defineConfig } from '@ice/app';
import intl from '@ice/plugin-intl';

export default defineConfig({
  plugins: [
    // Add intlSolution to remove the dependency of react-intl
    intl({ intlSolution: 'simple' }),
  ],
});

When you use the simple mode, you can only use the intl.formateMessage function to get the locale message:

import { ice } from 'ice';

export default function Home() {
  console.log(intl.formatMessage({ id: 'new' }));
  return <h1>home</h1>;
}

Function intl.formatMessage is limited, you can only use the syntax below to get the locale message:

Simple Usage:

intl.formatMessage({ id: 'app.welcome', defaultMessage: 'Welcome to my application!' });
intl.formatMessage('app.welcome');

With Variables:

intl.formatMessage({ id: 'app.welcome' }, { name: 'icejs' });

Caution: the message Syntax only support the pattern like {name}.