-
장고 다국어 i18n 적용기록이라도 하자 2023. 9. 22. 14:04
* 2019년 9월 19일에 개인 노션에 작성했던 글을 옮긴 것으로 지금 적용한다면 다를 수 있음
장고 i18n 지원 튜토리얼 참고 링크
https://kimdoky.github.io/django/2018/08/30/django-taskbuster-5/
https://docs.djangoproject.com/en/2.2/topics/i18n/translation/
적용 방법
다국어를 적용하고 싶은 html 파일에 i18n을 적용을 위한 명시 {% load i18n %}
다국어 적용 문구를 특정 키워드로 감싸기
{% load i18n %}. {% comment %}Translators: View verb{% endcomment %} {% trans "View" %} {# Translators: This is a text of the base template #} <p>{% blocktrans %}A multiline translatable literal.{% endblocktrans %}</p>
터미널에 번역 파일을 생성하는 명령어 입력
python manage.py makemessages -l ja #번역 파일 생성하는 command python manage.py compilemessages -l ja #번역 파일 컴파일하는 command
위 커맨드 입력시, CommandError: Can't find msguniq. Make sure you have GNU gettext tools 0.15 or newer installed. 에러가 뜬다면 아래 링크에서 static 다운로드 하고 환경변수에 /bin 추가
참고 : https://mlocati.github.io/articles/gettext-iconv-windows.html
현재 설정된 언어코드 html 내부에서 확인하고 싶다면 {{ LANGUAGE_CODE }} 을 사용하면 됨
These tags also require a {% load i18n %}. get_available_languages {% get_available_languages as LANGUAGES %} returns a list of tuples in which the first element is the language code and the second is the language name (translated into the currently active locale). get_current_language {% get_current_language as LANGUAGE_CODE %} returns the current user’s preferred language as a string. Example: en-us. See How Django discovers language preference.
해당 언어로 변경된 것을 확인하고 싶다면
브라우저에서 언어 설정을 변경해주어야 함.
크롬 기준으로 크롬 설정 - 언어 - 기본 언어 설정
chrome://settings/languages
위처럼 지정시 헤더에
Accept-Language: ja,ko-KR;q=0.9,ko;q=0.8,en-US;q=0.7,en;q=0.6 이렇게 표시된다.
'기록이라도 하자' 카테고리의 다른 글
Netlify Git Branch 이름 기준으로 배포 무시하기 (netlify ignore) (1) 2023.10.17 빌드시 메모리 부족으로 빌드 실패하는 현상 해결 (1) 2023.10.07 드러커 엑서사이즈 (0) 2023.10.02 leaflet-rotate로 leaflet 지도의 각도를 변경해보자. (1) 2023.08.14 센트리 로컬 환경에서 사용할 수 있게 설정하기 (0) 2023.08.02