์ž‘๊ฐ€:
    ์ƒ์„ฑ:2024-08-11๋งˆ์ง€๋ง‰ ์—…๋ฐ์ดํŠธ:2025-06-29

    React ํ†ตํ•ฉ: useI18n ํ›… ๋ฌธ์„œ

    ์ด ์„น์…˜์—์„œ๋Š” React ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋‚ด์—์„œ useI18n ํ›…์„ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ์ž์„ธํžˆ ์•ˆ๋‚ดํ•˜๋ฉฐ, ํšจ์œจ์ ์ธ ์ฝ˜ํ…์ธ  ํ˜„์ง€ํ™”๋ฅผ ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•ฉ๋‹ˆ๋‹ค.

    React์—์„œ useI18n ๊ฐ€์ ธ์˜ค๊ธฐ

    useI18n ํ›…์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ƒํ™ฉ์— ๋งž๊ฒŒ React ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์— ๊ฐ€์ ธ์™€ ํ†ตํ•ฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค:

    • ํด๋ผ์ด์–ธํŠธ ์ปดํฌ๋„ŒํŠธ:

      typescript
      import { useI18n } from "react-intlayer"; // ํด๋ผ์ด์–ธํŠธ ์ธก React ์ปดํฌ๋„ŒํŠธ์—์„œ ์‚ฌ์šฉ
      
    • ์„œ๋ฒ„ ์ปดํฌ๋„ŒํŠธ:

    ๋งค๊ฐœ๋ณ€์ˆ˜

    ์ด ํ›…์€ ๋‘ ๊ฐœ์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ๋ฐ›์Šต๋‹ˆ๋‹ค:

    1. namespace: ๋ฒˆ์—ญ ํ‚ค์˜ ๋ฒ”์œ„๋ฅผ ์ง€์ •ํ•˜๋Š” ์‚ฌ์ „ ๋„ค์ž„์ŠคํŽ˜์ด์Šค์ž…๋‹ˆ๋‹ค.
    2. locale (์„ ํƒ ์‚ฌํ•ญ): ์›ํ•˜๋Š” ๋กœ์ผ€์ผ์ž…๋‹ˆ๋‹ค. ์ง€์ •ํ•˜์ง€ ์•Š์œผ๋ฉด ์ปจํ…์ŠคํŠธ์˜ ๋กœ์ผ€์ผ์ด ๊ธฐ๋ณธ๊ฐ’์œผ๋กœ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.

    ์‚ฌ์ „

    ๋ชจ๋“  ์‚ฌ์ „ ํ‚ค๋Š” ํƒ€์ž… ์•ˆ์ „์„ฑ์„ ๋†’์ด๊ณ  ์˜ค๋ฅ˜๋ฅผ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•ด ์ฝ˜ํ…์ธ  ์„ ์–ธ ํŒŒ์ผ ๋‚ด์— ์„ ์–ธ๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์„ค์ • ์ง€์นจ์€ ์—ฌ๊ธฐ์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

    React์—์„œ์˜ ์‚ฌ์šฉ ์˜ˆ์‹œ

    React ์ปดํฌ๋„ŒํŠธ ๋‚ด์—์„œ useI18n ํ›…์„ ์‚ฌ์šฉํ•˜๋Š” ์˜ˆ์‹œ:

    src/App.tsx
    import type { FC } from "react";
    import { ClientComponentExample, ServerComponentExample } from "@components";
    import { IntlayerProvider } from "react-intlayer";
    import { useI18n } from "react-intlayer/server";
    import { Locales } from "intlayer";
    
    const App: FC<{ locale: Locales }> = ({ locale }) => {
      const t = useI18n("home-page", locale);
    
      return (
        <>
          <p>{t("introduction")}</p>
          <IntlayerProvider locale={locale}>
            <ClientComponentExample />
          </IntlayerProvider>
          <ServerComponentExample />
        </>
      );
    };
    
    src/App.tsx
    import type { FC } from "react";
    import { ClientComponentExample, ServerComponentExample } from "@components";
    import { IntlayerProvider } from "react-intlayer";
    import { useI18n, IntlayerServerProvider } from "react.intlayer/server";
    import { Locales } from "intlayer";
    
    const App: FC<{ locale: Locales }> = ({ locale }) => {
      const t = useI18n("home-page", locale);
    
      return (
        <>
          <p>{t("introduction")}</p>
          <IntlayerProvider locale={locale}>
            <ClientComponentExample />
          </IntlayerProvider>
          <IntlayerServerProvider locale={locale}>
            <ServerComponentExample />
          </IntlayerServerProvider>
        </>
      );
    };
    
    src/components/ComponentExample.tsx
    import type { FC } from "react";
    import { useI18n } from "react-intlayer";
    
    const ComponentExample: FC = () => {
      const t = useI18n("component-example");
    
      return (
        <div>
          <h1>{t("title")}</h1> {/* ์ œ๋ชฉ์„ ํ‘œ์‹œํ•ฉ๋‹ˆ๋‹ค */}
          <p>{t("description")}</p> {/* ์„ค๋ช…์„ ํ‘œ์‹œํ•ฉ๋‹ˆ๋‹ค */}
        </div>
      );
    };
    
    src/components/ServerComponentExample.tsx
    import { useI18n } from "react-intlayer/server";
    
    const ServerComponentExample = () => {
      const t = useI18n("server-component");
    
      return (
        <div>
          <h1>{t("title")}</h1> {/* ์ œ๋ชฉ์„ ํ‘œ์‹œํ•ฉ๋‹ˆ๋‹ค */}
          <p>{t("description")}</p> {/* ์„ค๋ช…์„ ํ‘œ์‹œํ•ฉ๋‹ˆ๋‹ค */}
        </div>
      );
    };
    

    ์†์„ฑ ์ฒ˜๋ฆฌ

    ์†์„ฑ์„ ํ˜„์ง€ํ™”ํ•  ๋•Œ๋Š” ๋ฒˆ์—ญ ๊ฐ’์„ ์ ์ ˆํžˆ ์ ‘๊ทผํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค:

    html
    <!-- ์ ‘๊ทผ์„ฑ ์†์„ฑ(์˜ˆ: aria-label)์˜ ๊ฒฝ์šฐ, ์ˆœ์ˆ˜ ๋ฌธ์ž์—ด์ด ํ•„์š”ํ•˜๋ฏ€๋กœ .value๋ฅผ ์‚ฌ์šฉํ•˜์„ธ์š” -->
    <button aria-label={t("button.ariaLabel").value}>{t("button.text")}</button>
    

    ์ถ”๊ฐ€ ์ž๋ฃŒ

    • Intlayer ๋น„์ฃผ์–ผ ์—๋””ํ„ฐ: ๋ณด๋‹ค ์ง๊ด€์ ์ธ ์ฝ˜ํ…์ธ  ๊ด€๋ฆฌ ๊ฒฝํ—˜์„ ์œ„ํ•ด ๋น„์ฃผ์–ผ ์—๋””ํ„ฐ ๋ฌธ์„œ๋ฅผ ์—ฌ๊ธฐ์—์„œ ์ฐธ๊ณ ํ•˜์„ธ์š”.

    ์ด ์„น์…˜์€ React ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ useI18n ํ›…์˜ ํ†ตํ•ฉ์„ ๊ตฌ์ฒด์ ์œผ๋กœ ๋‹ค๋ฃจ๋ฉฐ, ํ˜„์ง€ํ™” ๊ณผ์ •์„ ๋‹จ์ˆœํ™”ํ•˜๊ณ  ๋‹ค์–‘ํ•œ ๋กœ์ผ€์ผ ๊ฐ„ ์ฝ˜ํ…์ธ  ์ผ๊ด€์„ฑ์„ ๋ณด์žฅํ•ฉ๋‹ˆ๋‹ค.