작가:
    생성:2024-08-11마지막 업데이트:2025-06-29

    열거형 / 복수형 처리

    열거형 작동 방식

    Intlayer에서 열거형은 특정 키를 해당 콘텐츠에 매핑하는 enu 함수를 통해 구현됩니다. 이 키들은 숫자 값, 범위 또는 사용자 정의 식별자를 나타낼 수 있습니다. React Intlayer 또는 Next Intlayer와 함께 사용될 때, 애플리케이션의 로케일과 정의된 규칙에 따라 적절한 콘텐츠가 자동으로 선택됩니다.

    열거형 설정하기

    Intlayer 프로젝트에서 열거형을 설정하려면, 열거형 정의를 포함하는 콘텐츠 모듈을 생성해야 합니다. 다음은 자동차 수에 대한 간단한 열거형 예시입니다:

    **/*.content.ts
    import { enu, type Dictionary } from "intlayer";
    
    const carEnumeration = {
      key: "car_count",
      content: {
        numberOfCar: enu({
          "<-1": "마이너스 1대 미만의 자동차",
          "-1": "마이너스 1대의 자동차",
          "0": "자동차 없음",
          "1": "자동차 1대",
          ">5": "몇 대의 자동차",
          ">19": "많은 자동차",
          "fallback": "대체 값", // 선택 사항
        }),
      },
    } satisfies Dictionary;
    
    export default carEnumeration;

    이 예제에서 enu는 다양한 조건을 특정 콘텐츠에 매핑합니다. React 컴포넌트에서 사용할 때, Intlayer는 주어진 변수에 따라 적절한 콘텐츠를 자동으로 선택할 수 있습니다.

    Intlayer 열거형에서 선언 순서는 매우 중요합니다. 첫 번째로 유효한 선언이 선택됩니다. 여러 조건이 적용될 경우, 예상치 못한 동작을 방지하기 위해 올바른 순서로 정렬되어야 합니다.
    대체 값(fallback)이 선언되지 않은 경우, 일치하는 키가 없으면 함수는 undefined를 반환합니다.

    React Intlayer에서 열거형 사용하기

    To use enumeration in a React component, you can leverage the useIntlayer hook from the react-intlayer package. This hook retrieves the correct content based on the specified ID. Here's an example of how to use it:

    **/*.tsx
    import type { FC } from "react";
    import { useIntlayer } from "react-intlayer";
    
    const CarComponent: FC = () => {
    const { numberOfCar } = useIntlayer("car_count");
    
    return (
      <div>
        <p>
          {
            numberOfCar(0) // Output: No cars
          }
        </p>
        <p>
          {
            numberOfCar(6) // Output: Some cars
          }
        </p>
        <p>
          {
            numberOfCar(20) // Output: Many cars
          }
        </p>
        <p>
          {
            numberOfCar(0.01) // Output: Fallback value
          }
        </p>
      </div>
    );
    };

    추가 자료

    구성 및 사용법에 대한 자세한 정보는 다음 자료를 참조하세요:

    이 자료들은 다양한 환경과 여러 프레임워크에서 Intlayer의 설정 및 사용법에 대한 추가적인 통찰을 제공합니다.

    Using Ordinal Enumeration

    To use this in a React component, call the enumeration with the last digit of the number to get the correct suffix, then pass the full count as the insertion value:

    **/*.tsx
    import type { FC } from "react";
    import { useIntlayer } from "react-intlayer";
    
    const RankingComponent: FC<{ count: number }> = ({ count }) => {
    const { ordinal } = useIntlayer("ranking_component");
    
    // Get the last digit to determine the correct suffix
    const lastDigit = Math.abs(count) % 10;
    
    return (
      <div>
        <p>
          {
            ordinal(lastDigit)({ count }) // e.g., "5th place" for count=5
          }
        </p>
      </div>
    );
    };

    Additional Resources

    For more detailed information on configuration and usage, refer to the following resources:

    These resources provide further insights into the setup and usage of Intlayer in different environments and with various frameworks.