Lingui.js lets you use your Laravel localization on JavaScript.
npm i lingui
// app.ts
import en from './lang/en.json';
import pt from './lang/pt.json';
lingui.init({
locale: 'pt',
resources: { en, pt },
});
Then, you can use translations in any place of your application.
// pages/welcome.tsx
import { __, choice } from 'lingui';
export default function Welcome() {
return (
<p>
{__('Welcome :NAME! ', { name: 'John' })}
{choice('{0} All clear.|{1} You have :count message to read.|[2,*] You have :count messages to read.', 5)}
</p>
);
}
default
: Lingui singletontrans(key, replases?, locale?)
: Translate using a key or string literal with placeholderstransChoice(key, number, replaces?, locale?)
: Pluralization translate based onnumber
and currentlocale
__
: Alias oftrans
choice
: Alias oftransChoice
setLocale(locale)
: Change the current localecurrentLocale()
: Get the current locale
lingui
.use({
type: '3rdParty',
init(instance) {
// init hook
},
})
.init({ resources: { en, pt } });