Y
Y
Yeldos Adetbekov2015-07-15 20:36:41
Django
Yeldos Adetbekov, 2015-07-15 20:36:41

How to implement multilingual interface with Django?

Hello, oh great uncles. I need your help. How to implement multilingual interface with Django? I have my own idea, but more than once in other languages ​​​​I faced the fact that I reinvented the wheel again. Decided to laugh. I've only been studying Django for 5 days. Now, if that's an idea:
There is some kind of page, a view, but in a separate application. Well, for example, there are many of these pages. They are all separate and independent.
And for multilingualism, I created a new APP named Languages. Inside created a new 'language.py'

RU={
  'main_title':'Главная',
  'lang_panel_index':'Язык',
  'reg_panel_index':'Регистрация',
  'text_index':'Добро пожаловать в социальную сеть Dos',
  'login_index':'Вход',
  'about':'о проекте',
  'advert':'реклама',
  'dev':'разработчикам',
  'contacts':'контакты',
  'rools':'правила'
}
EN={
  'main_title':'Main',
  'lang_panel_index':'Language',
  'reg_panel_index':'Registration',
  'text_index':'Welcome to the Dos',
  'login_index':'Enter',
  'about':'about',
  'advert':'advert',
  'dev':'development',
  'contacts':'contacts',
  'rools':'rools'
}
KZ={
  'main_title':'Басты',
  'lang_panel_index':'Тілді тандау',
  'reg_panel_index':'Тіркелу',
  'text_index':'Dos интернет-бағдарламасына қош келдіңіздер',
  'login_index':'Кіру',
  'about':'жоба туралы',
  'advert':'жарнама',
  'dev':'дамытушыларға',
  'contacts':'байланыс',
  'rools':'ережелер'
}
class langpack(object):
  def lang(l):
    return {
      'ru' : RU,
      'en' : EN,
      'kz' : KZ
    }.get(l, "none")

This is like an analogy of an independent function))
Next, here is the view itself that asks for all this:
from django.shortcuts import render_to_response
from languages.language import langpack
from django.contrib.sessions import *

default_lang='ru'
context=langpack.lang(default_lang)

def index(request):
  response = render_to_response('index/index.html', context)
  response.set_cookie('language',default_lang)
  return response
  # return render(request, 'index/index.html',context)

As you can see, at the first login, I would rather remember the SESSION language of the interface, and write JSON in cookies with translation. And then pass this CONTEXT(JSON) -> HTML(template).
And you know, I just could not cope with sessions and cookies to return them and check if it was recorded. And so I have If you manually change Deafult_lang in the code, then the Site is translated without problems.
NXw36FaONLA.jpg
yZa3zW3_65g.jpg
What do you recommend?
PS: I recently graduated from school, so don't scold me for being perverted with Backend)))

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Roman Kitaev, 2015-07-15
@dosya97

Well, there are built-in tools for localization. Read documentation. And install PyCharm, it's hard to work with Dzhanga in Sublim.
PS Advertising in Kazakh is translated as "zharnama".

D
dzh21, 2015-10-01
@dzh21

everything is very intelligible here:
www.marinamele.com/taskbuster-django-tutorial/inte...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question