Installation
Diese Seite wurde von PageTurner AI übersetzt (Beta). Nicht offiziell vom Projekt unterstützt. Fehler gefunden? Problem melden →
formatjs ist eine Sammlung von Bibliotheken, die Ihnen beim Aufbau von Internationalisierung (i18n) in jedem Projekt helfen – unabhängig davon, ob React verwendet wird oder nicht.
Installation
- npm
- yarn
npm i -S react react-intl
yarn add react react-intl
Minimale Anwendung
Nachdem Sie die oben genannten Schritte befolgt haben, sollten Sie eine minimale Anwendung wie diese zum Laufen bringen können:
- Node
- React
- Vue3
import {createIntl, createIntlCache} from '@formatjs/intl'
// Translated messages in French with matching IDs to what you declared
const messagesInFrench = {
myMessage: "Aujourd'hui, nous sommes le {ts, date, ::yyyyMMdd}",
}
// This is optional but highly recommended
// since it prevents memory leak
const cache = createIntlCache()
// Create the `intl` object
const intl = createIntl(
{
// Locale of the application
locale: 'fr',
// Locale of the fallback defaultMessage
defaultLocale: 'en',
messages: messagesInFrench,
},
cache
)
// Aujourd'hui, nous sommes le 23/07/2020
console.log(
intl.formatMessage(
{
// Matching ID as above
id: 'myMessage',
// Default Message in English
defaultMessage: 'Today is {ts, date, ::yyyyMMdd}',
},
{ts: Date.now()}
)
)
// 19,00 €
console.log(intl.formatNumber(19, {style: 'currency', currency: 'EUR'}))
import * as React from 'react'
import {IntlProvider, FormattedMessage, FormattedNumber} from 'react-intl'
// Translated messages in French with matching IDs to what you declared
const messagesInFrench = {
myMessage: "Aujourd'hui, nous sommes le {ts, date, ::yyyyMMdd}",
}
export default function App() {
return (
<IntlProvider messages={messagesInFrench} locale="fr" defaultLocale="en">
<p>
<FormattedMessage
id="myMessage"
defaultMessage="Today is {ts, date, ::yyyyMMdd}"
values={{ts: Date.now()}}
/>
<br />
<FormattedNumber value={19} style="currency" currency="EUR" />
</p>
</IntlProvider>
)
}
Output
<p>
Aujourd'hui, nous sommes le 23/07/2020
<br />
19,00 €
</p>
import VueIntl from 'vue-intl'
import {createApp} from 'vue'
const app = createApp(App)
app.use(VueIntl, {
locale: 'fr',
defaultLocale: 'en',
messages: {
myMessage: "Aujourd'hui, nous sommes le {ts, date, ::yyyyMMdd}",
},
})
<template>
<p>
{{
$formatMessage(
{id: 'myMessage', defaultMessage: 'Today is {ts, date, ::yyyyMMdd}'},
{ts: Date.now()}
)
}}
<br />
{{ $formatNumber(19, {style: 'currency', currency: 'EUR'}) }}
</p>
</template>
Output
<p>
Aujourd'hui, nous sommes le 23/07/2020
<br />
19,00 €
</p>
Hinzufügen unseres Babel-Plugins/TypeScript-Transformers für die Kompilierung
Unser Tooling unterstützt babel, ts-loader, ts-jest, rollup-plugin-typescript2 und ts-patch für die Nachrichtenkompilierung:
Babel
Falls Sie babel verwenden, fügen Sie babel-plugin-formatjs zu Ihren Abhängigkeiten hinzu:
- npm
- yarn
npm i -D babel-plugin-formatjs
yarn add -D babel-plugin-formatjs
und ergänzen Sie es in Ihrer babel.config.js oder .babelrc:
{
"plugins": [
[
"formatjs",
{
"idInterpolationPattern": "[sha512:contenthash:base64:6]",
"ast": true
}
]
]
}
ts-loader
- npm
- yarn
npm i -D @formatjs/ts-transformer
yarn add -D @formatjs/ts-transformer
import {transform} from '@formatjs/ts-transformer'
module.exports = {
...otherConfigs,
module: {
rules: [
{
test: /\.tsx?$/,
use: [
{
loader: 'ts-loader',
options: {
getCustomTransformers() {
return {
before: [
transform({
overrideIdFn: '[sha512:contenthash:base64:6]',
}),
],
}
},
},
},
],
},
],
},
}
ts-jest in jest.config.js
- npm
- yarn
npm i -D @formatjs/ts-transformer
yarn add -D @formatjs/ts-transformer
Lesen Sie den ts-jest-Leitfaden, um zu erfahren, wie Sie benutzerdefinierte AST-Transformer integrieren.
ts-patch
- npm
- yarn
npm i -D @formatjs/ts-transformer
yarn add -D @formatjs/ts-transformer
{
"compilerOptions": {
"plugins": [
{
"transform": "@formatjs/ts-transformer",
"import": "transform",
"type": "config",
"overrideIdFn": "[sha512:contenthash:base64:6]",
"ast": true
}
]
}
}
rollup-plugin-typescript2
- npm
- yarn
npm i -D @formatjs/ts-transformer
yarn add -D @formatjs/ts-transformer
// rollup.config.js
import typescript from 'rollup-plugin-typescript2'
import {transform} from '@formatjs/ts-transformer'
export default {
input: './main.ts',
plugins: [
typescript({
transformers: () => ({
before: [
transform({
overrideIdFn: '[sha512:contenthash:base64:6]',
ast: true,
}),
],
}),
}),
],
}