JavaScript - Internationalization API

신동환·2022년 5월 2일

js

목록 보기
15/18

Intl

  • Intl 객체는 각 언어에 맞는 문자비교, 숫자, 시간, 날짜비교를 제공하는 ECMAScript 국제화 API다

1. Collator Objects

  • 언어 구분 문자열 비교를 가능하게 하는 객체
  • 생성자: Intl.Collator([locales[,options]])

2. DateTimeFormat Objects

2-1. format(date)

  • 로케일 및 형식 지정 옵션에 따라 날짜 형식을 지정하는 Getter 함수

    console.log(new Intl.DateTimeFormat('sr-RS', { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' }).format(new Date()));
    => уторак, 3. мај 2022. (세르비아 날짜 표현)

  • 날짜 타입 값의 배열이 있을 경우 map을 이용하여 콜백함수로 표현 가능

    var a = [new Date(2012, 08), new Date(2012, 11), new Date(2012, 03)];
    var dateTimeFormat = new Intl.DateTimeFormat('pt-BR', { year: 'numeric', month: 'long' });
    var usingFormatMap = a.map(dateTimeFormat.format);
    console.log(usingFormatMap);
    => (3) ['setembro de 2012', 'dezembro de 2012', 'abril de 2012']
    0: "setembro de 2012"
    1: "dezembro de 2012"
    2: "abril de 2012"

2-2. formatToParts(date)

  • 형식이 지정된 날짜를 부분적으로 포함하는 Array 객체를 반환하는 함수

    console.log(new Intl.DateTimeFormat('pt-BR', { year: 'numeric', month: 'long' }).formatToParts(new Date()));
    => (3) [{…}, {…}, {…}]
    0: {type: 'month', value: 'maio'}
    1: {type: 'literal', value: ' de '}
    2: {type: 'year', value: '2022'}

3. DisplayNames Objects

  • 언어, 지역 및 스크립트 표시 이름의 일관된 변역을 가능하게하는 객체
  • 생성자: Intl.DisplayNames([locales[,options]])

4. ListFormat Objects

  • 언어 구분 목록 형식을 사용하는 객체
  • 생성자: Intl.ListFormat([locales[,options]])

5. Locale Objects

  • 유니코드 로케일 식별자를 나타내는 객체
  • 생성자: Intl.Locale([locales[,options]])

6. NumberFormat Objects

6-1. format(number)

  • 로케일 및 형식 지정 옵션에 따라 숫자 형식을 지정하는 Getter 함수

    console.log(new Intl.NumberFormat('de-DE', { style: 'currency', currency: 'EUR'}).format(3500));
    => 3.500,00 €

6-2. formatToParts(number)

  • 형식이 지정된 숫자를 부분적으로 포함하는 Array 객체를 반환하는 함수

    console.log(new Intl.NumberFormat('de-DE', { style: 'currency', currency: 'EUR'}).formatToParts(3500));
    => (7) [{…}, {…}, {…}, {…}, {…}, {…}, {…}]
    0: {type: 'integer', value: '3'}
    1: {type: 'group', value: '.'}
    2: {type: 'integer', value: '500'}
    3: {type: 'decimal', value: ','}
    4: {type: 'fraction', value: '00'}
    5: {type: 'literal', value: ' '}
    6: {type: 'currency', value: '€'}

7. PluralRules Objects

  • 복수형에 대한 언어 별 규칙 및 복수 구분 서식을 활성화하는 객체
  • 생성자: Intl.PluralRules([locales[,options]])

8. RelativeTimeFormat Objects

  • 언어 구분 상대 시간 형식을 활성화하는 객체
  • 생성자: Intl.RelativeTimeFormat([locales[,options]])

8. Segmenter Objects

  • 로케일 구분 텍스트 분할을 활성화하여 문자열에서 의미 있는 항목을 가져올 수 있는 객체
  • 생성자: Intl.Segmenter([locales[,options]])

9. 정적 메소드

  1. Intl.getCanonicalLocales()
    • 표준 로케일 이름을 반환하는 메소드

      console.log(Intl.getCanonicalLocales('EN-US'));
      // output: Array ["en-US"]

  2. Intl.supportedValuesOf()
    • 지원되는 고유 달력, 데이터 정렬, 통화, 번호 매기기 시스템 또는 구현에서 지원하는 단위 값을 포함하는 정렬된 배열을 반환하는 메소드

      console.log(Intl.supportedValuesOf('currency'));
      // output: Array ["AED", "AFN", "ALL", "AMD", "ANG", "AOA", "ARS", ...]

profile
안녕하세요! Hello, World!

0개의 댓글