Skip to content

[Feature]: josa의 다국어 무시 옵션 #379

@redonearth

Description

@redonearth

Description

저희 회사에서 사용을 해보고 기능 추가 의견을 드리게 되었습니다.

공식 문서의 소개 페이지의 '한글을 다루는 제품을 개발할 때'라는 설명처럼
josa 코드 예제를 사용해보면
한글은 의도대로 동작하지만, 영문의 경우에는 정해진 조사만 달아주게 동작하고 있는데요.

import { josa } from 'es-hangul';

console.log(josa('샴푸', '이/가'));  // "샴푸가"
console.log(josa('외국인', '이/가'));  // "외국인이"
console.log(josa('Shampoo', '이/가'));  // "Shampoo가"
console.log(josa('Foreigner', '이/가'));  // "Foreigner가"

제가 진행 중인 프로젝트는 i18n을 적용하여 한/영 번역이 들어갑니다.
제가 의도하는 동작은,

한글(i18n.language === 'ko')이면 조사를 붙이고
영문(i18n.language === 'en')이면 조사를 붙이지 않는 것입니다.

Possible Solution

객체를 아래와 같이 정의한다면

{
  "ko": {
    "invalidNumber_label": "{{label}} 유효하지 않습니다. 숫자만 입력할 수 있습니다."
  },
  "en": {
    "invalidNumber_label": "{{label}} is invalid. Only numbers are allowed."
  }
}

tMessage('validation.invalidNumber_label', { label: josa(label, '이/가') })
이렇게 사용할 수 있을 것입니다.

의도대로 만들어주기 위해

import { josa as esJosa } from 'es-hangul';

import i18n from '@/i18n';

// es-hangul의 josa 함수에서 두 번째 매개변수 타입 추출
type JosaOption = Parameters<typeof esJosa>[1];

// i18n 언어를 확인하여 한국어일 때만 조사를 붙이는 헬퍼 함수
export function josa<T extends string, U extends JosaOption>(word: T, josaOption: U): string {
  // 현재 언어가 한국어가 아니면 원본 문자열 반환
  if (i18n.language !== 'ko') {
    return word;
  }
  // 한국어일 때만 es-hangul의 josa 함수 사용
  return esJosa(word, josaOption);
}

이렇게 helper를 만들어서 사용하고 있는데,
자체 옵션으로 '한글'이 아니면 무시하는 옵션이 추가되면 좋을 것 같다는 생각에 제안드립니다.

etc.

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions