2 minute read

Django의 i18n(Internationalization)은 다국어 지원을 위한 강력한 기능을 제공하며, 사용자 브라우저 설정에 맞게 언어 번역을 제공한다. 그러나 때로는 사용자의 브라우저 설정과는 상관없이 특정 언어로 웹 애플리케이션을 기본 설정하고자 할 때가 있는데 이럴 때는 커스텀 미들웨어를 작성해서 기본 언어 설정을 강제로 변경할 수 있다.

기존 미들웨어에서의 언어 선택 방식

Django에서는 기본적으로 다음과 같은 방식으로 언어를 선택한다.

  • Accept-Language Header 확인: 클라이언트의 브라우저가 요청을 보낼 때 함께 보내는 Accept-Language 헤더를 확인하여 우선적으로 언어를 결정한다.

  • 사용자 설정: 사용자가 사이트 내에서 선택한 언어 설정을 우선으로 적용한다. 이는 주로 세션 변수나 쿠키를 통해 유지된다.

  • 기본 언어 설정: 위 두 가지 방법으로 언어를 결정할 수 없을 경우, Django는 기본으로 설정된 언어를 사용한다.

기본 언어 설정 강제 변경하는 방법

커스텀 미들웨어를 작성하여 기본 언어 설정을 강제로 지정할 수 있다.

from django.conf import settings
from django.conf.urls.i18n import is_language_prefix_patterns_used
from django.http import HttpResponseRedirect
from django.urls import get_script_prefix, is_valid_path
from django.utils import translation
from django.utils.cache import patch_vary_headers
from django.utils.deprecation import MiddlewareMixin


class LocaleMiddleware(MiddlewareMixin):
    """
    Parse a request and decide what translation object to install in the
    current thread context. This allows pages to be dynamically translated to
    the language the user desires (if the language is available).
    """

    response_redirect_class = HttpResponseRedirect

    def process_request(self, request):
        urlconf = getattr(request, "urlconf", settings.ROOT_URLCONF)
        (
            i18n_patterns_used,
            prefixed_default_language,
        ) = is_language_prefix_patterns_used(urlconf)
        language = translation.get_language_from_request(
            request, check_path=i18n_patterns_used
        )
        language_from_path = translation.get_language_from_path(request.path_info)
        # In order to skip the preferred browser language, you can add Force_language = True to your settings.
        if settings.FORCE_LANGUAGES:
            if not language_from_path:
                language = settings.LANGUAGE_CODE
        else:
            if (
            not language_from_path
            and i18n_patterns_used
            and not prefixed_default_language
                ):
                language = settings.LANGUAGE_CODE
        translation.activate(language)
        request.LANGUAGE_CODE = translation.get_language()

    def process_response(self, request, response):
        language = translation.get_language()
        language_from_path = translation.get_language_from_path(request.path_info)
        urlconf = getattr(request, "urlconf", settings.ROOT_URLCONF)
        (
            i18n_patterns_used,
            prefixed_default_language,
        ) = is_language_prefix_patterns_used(urlconf)

        if (
            response.status_code == 404
            and not language_from_path
            and i18n_patterns_used
            and prefixed_default_language
        ):
            # Maybe the language code is missing in the URL? Try adding the
            # language prefix and redirecting to that URL.
            language_path = "/%s%s" % (language, request.path_info)
            path_valid = is_valid_path(language_path, urlconf)
            path_needs_slash = not path_valid and (
                settings.APPEND_SLASH
                and not language_path.endswith("/")
                and is_valid_path("%s/" % language_path, urlconf)
            )

            if path_valid or path_needs_slash:
                script_prefix = get_script_prefix()
                # Insert language after the script prefix and before the
                # rest of the URL
                language_url = request.get_full_path(
                    force_append_slash=path_needs_slash
                ).replace(script_prefix, "%s%s/" % (script_prefix, language), 1)
                # Redirect to the language-specific URL as detected by
                # get_language_from_request(). HTTP caches may cache this
                # redirect, so add the Vary header.
                redirect = self.response_redirect_class(language_url)
                patch_vary_headers(redirect, ("Accept-Language", "Cookie"))
                return redirect

        if not (i18n_patterns_used and language_from_path):
            patch_vary_headers(response, ("Accept-Language",))
        response.headers.setdefault("Content-Language", language)
        return response
# settings.py 에 추가
FORCE_LANGUAGES = True

위의 미들웨어를 사용하면 Django는 항상 설정된 기본 언어로 렌더링 한다.

Leave a comment