6

I am suppose to change some of these characters in English to Chinese in this setting.py file but then error happened.

I know that it's saying because no encoding is declared in the error message but I have been reading a few posts and still have no idea how / where I can make it happen.

in my setting.py I have something like this

OSCAR_DASHBOARD_NAVIGATION = [
{
    'label': _('dashboard'),
    'icon': 'icon-th-list',
    'url_name': 'dashboard:index',
},

but need to change the navigation to Chinese so ended up looking like this

OSCAR_DASHBOARD_NAVIGATION = [
{
    'label': _('仪表板'),
    'icon': 'icon-th-list',
    'url_name': 'dashboard:index',
},

Edit, I already read the post which thought might be duplicated and tried what's in that post and instead of getting errors, I get a no show in my page but regarding to Pedru's answer. It works like a charm now.

Dora
  • 6,776
  • 14
  • 51
  • 99
  • @Ilja tried, gives me no error but in browser it just shows blank totally nothing and if I type in English, the Characters will show up if I combine English with Chinese or Chinese only, it just shows a space nothing else – Dora Apr 04 '16 at 09:34
  • you should keep `_('dashboard')` in the source code and provide Chinese translation instead (`gettext`). You don't need to hardcode the language. Here's [code example where `gettext('world')` from the source code can be shown using a chosen language.](https://github.com/zed/flask-multilang-hello-world)—the word `'world'` is always in English in the code but it can be shown in any language in the browser if the corresponding translation is available. – jfs Apr 05 '16 at 10:20

1 Answers1

19

Try adding

# -*- coding: utf-8 -*-

at the beginning of the file, make sure your file is really encoded in utf-8

Pedru
  • 1,430
  • 1
  • 14
  • 32
  • tried, gives me no error but in browser it just shows blank totally nothing and if I type in English, the Characters will show up if I combine English with Chinese or Chinese only, it just shows a space nothing else – Dora Apr 04 '16 at 09:34
  • 1
    I see you use the _( ) function, try replacing `'label': _('仪表板')` with `'label': '仪表板'` – Pedru Apr 04 '16 at 09:58
  • Bravo! Thanks, this worked like a charm – Dora Apr 04 '16 at 12:59
  • I don't know if you have access to the code that reads the setting.py file, but I think it does something like `_ = gettext.gettext`, gettext does not support unicode, instead this should be used `_ = gettext.ugettext` – Pedru Apr 04 '16 at 13:03
  • actually what I posted in inside the `setting.py` file and the `json` is pasted in there to override the default `json` info (Sorry first time working with python kind of have no idea what you really meant) but so if it's `ugettext` instead of `gettext` I didn't have to change that much things then? – Dora Apr 04 '16 at 14:33
  • The issue here is twofold: the `_( )` function is normally an alias for `gettext.gettext()` which is a function which allows to keep your strings in a sort of register to make them easily translatable. The idea is to have your strings created with _() in english like `label = _('apple')` and then in another file you can say that in french 'apple' is 'pomme'. This allows you to have your software translated without changing the code. So if you wanted the label in Chinese, the proper way would be to make a file for chinese translations and add the translation for 'dashboard'. – Pedru Apr 04 '16 at 14:52
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/108195/discussion-between-pedru-and-dora). – Pedru Apr 04 '16 at 14:54
  • I am getting the same error *ON THIS LINE ITSELF* what should I do? – Gulzar May 02 '19 at 13:46
  • I know that this will solve the general problem but yet if you have to process those unft8 strings this will cause problems. – sajed zarrinpour Oct 04 '19 at 10:26