简体   繁体   中英

Django: Not working the translation of strings

im study the Django and faced with a problem translating strings. This code is send keyboard for the Telegram bot. In the code you will see SQL request, since the bot was writeed on clear python. Im need translate the keyboard text on "ru" or "en" (default) depending on what text the user sent.

def bot_message(request):
    def settinngs(chat_id, message):
       con = lite.connect('db.sqlite3')
       cur = con.cursor()
       sql = "SELECT City, Lang FROM Userprofile WHERE Id={} ".format(chat_id)
       cur.execute(sql)
       result = cur.fetchall()[0]

       keyboard = types.ReplyKeyboardMarkup(row_width=2, resize_keyboard=True)
       button_change_city = types.KeyboardButton(text=_('Change name city'))
       button_subs = types.KeyboardButton(text=_('Subscriptions'))
       button_change_language = types.KeyboardButton(text=_('Change language'))
       backs_button = types.KeyboardButton(text=_('Back'))
       keyboard.add(button_change_city, button_subs, button_change_language, backs_button)
       bot.send_message(message.chat.id,
                     '{}{}\n{}{}'.format(_('Your city: '), result[0].capitalize(), _('Language: '),
                                         result[1]), reply_markup=keyboard)

I created translate in .po and compilemessages .mo file

LANGUAGES = (
    ('ru', 'Russian'),
    ('en', 'English'),
)

USE_I18N = True
LOCALE_PATHS = (
     os.path.join(BASE_DIR, 'locale'),
) 

And set in MIDDLEWARE

MIDDLEWARE = [
   'django.middleware.locale.LocaleMiddleware',
   'django.middleware.security.SecurityMiddleware',
   'django.contrib.sessions.middleware.SessionMiddleware',
   'django.middleware.common.CommonMiddleware',
   'django.middleware.csrf.CsrfViewMiddleware',
   'django.contrib.auth.middleware.AuthenticationMiddleware',
   'django.contrib.messages.middleware.MessageMiddleware',
   'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

What am I doing wrong?

Not understand fully your question, you can may be bit describe more but as you follow the translation module, the code should work.

As I see in your code, here are some setting you missing in your code:

# in your setting file
# define default language code 'ru' or 'en'

LANGUAGE_CODE = 'en'

In you view or controller, don't forget to import the translation module-

# for translation, in your case
from django.utils.translation import ugettext as _

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM