1 code cloud warehouse address, welcome star

Code cloud warehouse address: gitee.com/mtoooo/i18n

1.1 Cloning a Warehouse
# clonewarehouse
git clone https://gitee.com/mtoooo/i18n.git
Copy the code

2 Using tutorials

2.1 Place the i18n_settings.py file in the project configuration folder

2.2 Configuring the Multilanguage option
  • Configure the list of languages (the default language in i18n_settings.py is 0)

  • i18n_settings.py

# Language number
language_code = {
    "en": 0.# Default language
    "Chinese": 1."Arabic": 2
}
Copy the code

Language_code is a dict data where the value represents the language name and is used below the following number to represent the location in the translate_word list. For example, “en” = 0 # indicates that the default system language is English, which is bit 0 in the translate_word list.

  • Configure the translation
  • i18n_settings.py
translate_word = [
    {  # English Translation
        "word1": "word1"."excel1": "excel1"}, {# Chinese Translation
        "word1": "Text one"."excel1": "Form 1"}, {# Arabic translation
        "word1": "An ل ج د organisation ل an ل أ organisation ل"."excel1": "The organisation ن ب ن ي ي",}]Copy the code

Translate_word is a list of translations for different languages, and the value of language_code is the number of the list. For example, translate_word[0] is translated as “en” = 0 in language_code.

2.3 Multilingual functions
  • i18n_script.py
def language(method, value) :
    if method == "get":
        return translate_word[language_code[value]]
    if method == "put":
        return translate_word[language_code[value]]
    else:
        return translate_word[0]
Copy the code
2.4 Interface Invocation
  • According to user Settings, interface Response corresponds to language information
  • view.py
@csrf_exempt
def language_view(request) :
	# method = request.get("method") # method = request.get("method"
	# value = session.get("language") # value = session.get("language"
    method = "get"
    value = "en"
    result = language(method, value)
    
    return HttpResponse(content=result)
Copy the code
  • User changes language, corrects
@csrf_exempt
def language_view(request) :
	# method = request.get("method") # method = request.get("method"
	# value = session.get("language") # value = session.get("language"
    method = "put"
    value = "Arabic"
    set_session["language"] = "Arabic"  Update session contents synchronously
    result = language(method, value)
    
    return HttpResponse(content=result)
Copy the code

3 Multi-language implementation principles

4 Front-end cache solution (Jinja2 template usage)

Copy the code

Android/IOS client cache solution