header-img
Info :
728x90

ํšŒ์‚ฌ์˜ ๊ฒฝ์šฐ ์—ฌ๋Ÿฌ ํ•ด์™ธ ๋ฒ•์ธ์ด ์กด์žฌํ•˜๊ณ  ์žˆ์–ด, ๋‹ค๊ตญ์–ด ์ง€์›์ด ํ•„์š”ํ•˜๋‹ค.

์›นํŽ˜์ด์ง€์˜ ๊ฒฝ์šฐ chrome ๋“ฑ ์ธํ„ฐ๋„ท ๋ธŒ๋ผ์šฐ์ € ๋‚ด ๋ฒˆ์—ญ์œผ๋กœ๋„ ์ง€์›์ด ๊ฐ€๋Šฅํ•˜์ง€๋งŒ,

ํšŒ์‚ฌ์—์„œ๋Š” ์‚ฌ์ „์ ์œผ๋กœ ๋ฒˆ์—ญํ•˜๊ธฐ ๋ณด๋‹ค๋Š”, ๊ฐ ๋‚˜๋ผ์—์„œ ์ดํ•ดํ•˜๊ธฐ ์‰ฌ์šด ๋‹จ์–ด๋กœ ๋ฒˆ์—ญํ•˜๋Š” ์ผ€์ด์Šค๊ฐ€ ํ•„์š”ํ•˜๊ฒŒ ๋œ๋‹ค.

โ€‹

์ด๋•Œ ์‚ฌ์šฉ๋˜๋Š” ๊ฒƒ์ด i18next.

 

i18next๋ž€?

Javascript๋กœ ์ž‘์„ฑ๋œ ๊ตญ์ œํ™” ํ”„๋ ˆ์ž„์›Œํฌ๋กœ ์—ฌ๋Ÿฌ ๊ตญ๊ฐ€์˜ ์–ธ์–ด๋ฅผ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋„๋ก ๋„์™€์ค€๋‹ค.

๋งŽ์ด ์‚ฌ์šฉํ•˜๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋กœ๋Š” react-i18next, next-i18next, next-translation ์ด๋ ‡๊ฒŒ 3๊ฐ€์ง€.

๊ฐ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์— ์žฅ๋‹จ์ ๊ณผ ํŠน์„ฑ์ด ์กด์žฌํ•˜๋ฏ€๋กœ ์•Œ์•„๋ณด๊ณ  ์ฑ„ํƒํ•˜๋Š” ๊ฒƒ์„ ์ถ”์ฒœํ•œ๋‹ค.

โ€‹

์šฐ๋ฆฌ์˜ ๊ฒฝ์šฐ react-i18next ๋ฅผ ์ฑ„ํƒํ•˜๊ฒŒ ๋˜์—ˆ๋Š”๋ฐ.. ์ •ํ™•ํžˆ๋Š” ์ฑ„ํƒ ๋‹นํ–ˆ๋‹ค.

๊ณ ๋กœ react-i18next ์— ๋Œ€ํ•˜์—ฌ ์„ค๋ช…ํ•ด๋ณด๋„๋ก ํ•˜๊ฒ ๋‹ค.

โ€‹

์‚ฌ์šฉ.

Recoil ์ฒ˜๋Ÿผ React ์—์„œ ์ œ๊ณตํ•˜๋Š” Hook์ด ์•„๋‹Œ ์™ธ๋ถ€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ด๋ฏ€๋กœ npm์„ ํ†ตํ•˜์—ฌ ์„ค์น˜๋ฅผ ํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค.

npm install react-i18next i18next --save

 

์ดํ›„ react ๋‚ด json ํŒŒ์ผ๋กœ ๋งŒ๋“ค์–ด์„œ ๊บผ๋‚ด์–ด ์“ฐ๋Š” case ๋„ ๋งŽ์ง€๋งŒ,

ํ˜„์žฌ ์šฐ๋ฆฌ ํ”„๋กœ์ ํŠธ์˜ ๊ฒฝ์šฐ DB์— ์žˆ๋Š” ๋‹ค๊ตญ์–ด ํ…Œ์ด๋ธ”์„ ์ฐธ์กฐํ•˜์—ฌ ๋ฐœ๋ผ์ค˜์•ผ ํ•˜๋ฏ€๋กœ..

 

import i18next from "i18next";
import { initReactI18next } from "react-i18next";
import HttpBackend from 'i18next-http-backend';

i18next.use(initReactI18next).use(HttpBackend).init({
  lng: localStorage.getItem("lng") || "en",
  fallbackLng: "en", // fallback language
  backend: {
    loadPath: "์ฃผ์†Œ์ง€๊ฐ’/{{lng}}",
    parse: (data) => {
       return JSON.parse(data);
    },
    caching: false,
  },
});

// ์—ฌ๋Ÿฌ ์–ธ์–ด๋ฅผ ํ•œ ๋ฒˆ์— ๋กœ๋“œ
const supportedLanguages = ['kr', 'fr', 'en'];
supportedLanguages.forEach(lang => {
  i18next.loadLanguages(lang);
});


export default i18next;

 

์ด๋Ÿฐ ์‹์œผ๋กœ ํ•œ๋ฒˆ์— ๋ถˆ๋Ÿฌ์™€์„œ ์ €์žฅํ•ด๋‘๊ณ , ๊บผ๋‚ด์–ด ์“ฐ๋Š” ํ˜•์‹์œผ๋กœ ์‚ฌ์šฉํ•œ๋‹ค.

 

@GetMapping("/์ฃผ์†Œ์ง€/{language}")
    public ResponseEntity<Object> getTranslations(@PathVariable String language) {
        System.out.println("ํ˜ธ์ถœ:::"+language);
        if ("fr".equals(language)) {
            // French translations
            return ResponseEntity.status(HttpStatus.OK).body(
                    Map.of("welcome", "Bienvenue", "hello", "Bonjour")
            );
        } else if ("kr".equals(language)) {
            // Korean translations
            return ResponseEntity.status(HttpStatus.OK).body(
                    Map.of("welcome", "ํ™˜์˜ํ•ฉ๋‹ˆ๋‹ค", "hello", "์•ˆ๋…•ํ•˜์„ธ์š”")
            );
        } else {
            return ResponseEntity.status(HttpStatus.OK).body(
                    Map.of("welcome", "welcome", "hello", "hello")
            );
        }
    }
 

 

spring ์ธก ๊ตฌํ˜„.

 

์ปจํŠธ๋กค๋Ÿฌ์—์„œ ๋ฐ”๋กœ ํ•ด๋‹น ๋ชจ์–‘์œผ๋กœ body๋ฅผ sending ํ•˜๋„๋ก ํ•ด๋‘์—ˆ๋Š”๋ฐ,

์ •์ƒ์ ์œผ๋กœ DB์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ง€๊ณ  ์˜ค๋ ค๋ฉด controller-service-mapper๋กœ ์—ฐ๊ฒฐํ•ด์ฃผ๊ณ ,

mapper์—์„œ ํ•ด๋‹น ๋ชจ์–‘๋Œ€๋กœ sql์„ ๊ตฌํ˜„ํ•ด์ฃผ๋ฉด ๋œ๋‹ค.

 

 


โ€‹๊ณต์‹ ๋ฌธ์„œ๋ฅผ ์ฝ๊ฑฐ๋‚˜ ๊ฐœ๋ฐœํ•˜๋ฉฐ ๋Š๊ผˆ๋˜ ๋‡Œํ”ผ์…œ๋กœ ์ž‘์„ฑํ•˜๊ณ  ์žˆ์œผ๋‹ˆ, 
๊ถ๊ธˆํ•˜์‹  ์ ์ด๋‚˜ ์ž˜๋ชป๋œ ์ ์ด ์žˆ๋‹ค๋ฉด ๋Œ“๊ธ€ ๋‚จ๊ฒจ์ฃผ์‹œ๋ฉด ๊ฐ์‚ฌํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

 

 

 

728x90
๋”๋ณด๊ธฐ
Document/๋จ•์„ ์ƒ์˜ ์ฝ”๋”ฉ๊ต์‹ค