https://vue-i18n.intlify.dev/guide/introduction.html
npm install vue-i18n@10
// main.js
import { createI18n } from 'vue-i18n';
const i18n = createI18n({});
app.use(i18n);
locale은 현재 선택된 로케일,
fallbackLocale은 못찾는 로케일 선택되면 사용할 로케일
messages안에 로케일별로 내용을 집어넣는다 (en, ko)
컴포넌트에서 쓸 때는 $t('아이템 키')처럼 한다.
locale값에 해당하는 키의 값을 가져와 보여줌.
// main.js
...
const options = {
locale: 'ko',
fallbackLocale: 'en',
messages: {
en: {
helloworld: 'Hello World',
nested: {
item: 'hi',
}
},
ko: {
helloworld: '안녕 세계',
nested: {
item: 'ㅎㅇ',
}
},
}
}
const i18n = createI18n(options);
app.use(i18n);
...
// App.js
<span>
{{ $t('helloworld') }}
{{ $t('nested.item') }}
</span>
옵션은 한 개 로케일 안의 내용만 쓴다.
// option
{
msg: 'I am {name}'
}
// ui
$t('msg', {name: 'Alice'}) // I am Alice
// option
{
msg: '{0} is not {1}'
}
// ui
$t('msg', ['A', 'B']) // A is not B
특수하게 사용되는 문자를 그대로 표시하고 싶을 때 (c의 \% escape string처럼)
{ } @ % |// option -- use '@' literal
msg: "{'@'}{location}"
// ui
$t('msg', {location: 'Seoul'})
// option
last_name: '이',
first_name: '원진',
full_name: '@:last_name @:first_name'
// ui
$t('full_name') // 이 원진
@.modifier:key 로 쓴다
// option
name: 'aBc'
msg: '@.upper:name @.lower:name @.capitalize:name'
// ui
$t('msg') // ABC abc Abc
// option
{
locale: 'en',
messages: {
en: {
greet: 'hello world',
msg: '@.onlyFirstWord:greet'
}
ko: { ... }
},
modifiers: {
onlyFirstWord: (str) => str.split(' ')[0]
}
}
// ui
$t('msg') // hello
| pipe 사용해서 단복수 표현 정의함
A|B : 1개일 때 A, 0개 또는 2개 이상이면 BA|B|C : 0개일 때 A, 1개일 때 B, 2개 이상일 때 C// option
car: 'car | cars',
bike: 'no bike | bike | bikes'
// ui
$t('car', 0) // cars
$t('car', 1) // car
$t('car', 2) // cars
$t('bike', 0) // no bike
$t('bike', 1) // bike
$t('bike', 2) // bikes
plural 메시지 신택스에서 n, count를 사용해 개수 표시 가능
$t에서 넣어줄 때는 방법 두가지
{named: { n: 2, count: '문자도 됨'}}named로 주는거 아니면 메시지에서 둘 중 아무거나 써도 됨.
// option
baguette: 'une baguette | {n} baguettes',
pain: 'sans pain | un pain | {count} pains',
// ui
$t('baguette', 10) // 10 baguettes
$t('baguette', 1) // une baguette
$t('pain', 0) // sans pain
$t('pain', 1) // un pain
$t('pain', 10) // 10 pains
$t('pain', 10, { named: { count: 10 } }) // 10 pains
$t('pain', 60, { named: { count: 2 } }) // 2 pains
https://vue-i18n.intlify.dev/guide/essentials/pluralization.html#custom-pluralization
https://tc39.es/ecma402/#datetimeformat-objects
datetimeFormats로 넣어준다. (번역은 messages였던 반면)short, long처럼 표시 유형 이름 정하고, 그 안에 year, month, day, weekday, hour, minute, hour12 형식 정해준다.$d로 한다. (번역은 $t였던 반면)// option
const datetimeFormats = {
'en-US': {
short: {
year: 'numeric', month: 'short', day: 'numeric'
},
long: {
year: 'numeric', month: 'short', day: 'numeric',
weekday: 'short', hour: 'numeric', minute: 'numeric'
}
},
'ko-KR': {
...
},
};
const i18n = createI18n({
locale: 'en-US',
messages: { ... },
datetimeFormats,
});
// ui
/* Sun, Feb 2, 2025, 10:00 PM */
<span> {{ $d(new Date(), 'long') }} </span>
/* Feb 2, 2025 */
<span> {{ $d(new Date(), 'short') }} </span>
<i18n-d> </i18n-d> 컴포넌트를 사용하면 각 부분을 포맷팅할 수 있다.
그러나 기본 날짜 스트링의 부분을 태그로 만드는 것에 그친다.
완전한 커스텀이 되지 않는 것 같음.
결국 완전한 맞춤형 날짜 포맷팅을 하고 싶다면 직접 구현하는게 빠른듯.
https://tc39.es/ecma402/#numberformat-objects
createI18n 옵션의 numberFormats을 넣어줘야 함.
$n() 사용.
<i18n-n>컴포넌트로 할 수 있다.
$i18n.locale로 접근 가능.i18n.global.locale.value로 접근 가능<i18n> 커스텀태그로 로컬 로케일 설정 가능