NumesSanguis
NumesSanguis

Reputation: 6292

Kivy Encoding Japanese (Unicode)

I'm trying to get Japanese characters in my app. From the documentation and other sources I found that Label(text=u'我是中文', font_name='fonts/ARIALUNI.TTF') should work (arialuni font should be able to handle Japanese/Chinese characters). However I'm wondering how do I specify this in the Kivy language in the .kv file? This is what I tried:

main.py:

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout


class Design1(BoxLayout):
    pass


class KanjiLayoutApp(App):
    def build(self):
        return Design1()


if __name__ == '__main__':
    KanjiLayoutApp().run()

kanjilayout.kv:

<Design1>:
    orientation: "vertical"
    Label:
        font_name: 'data/fonts/ARIALUNI.TTF'
        text: u'速 dsf'

However this gives me "é€Y dsf". Also text: '速 dsf' without u doesn't work.

p.s. adding あい (kana) to the text: text: '速 dsf あい' gives even more errors: UnicodeDecodeError: 'charmap' codec can't decode byte 0x81 in position 369: character maps to < undefined>

Upvotes: 4

Views: 7184

Answers (4)

Padraic Cunningham
Padraic Cunningham

Reputation: 180401

If you want to use Japanese chars, download the this ttf:

Specify the full path to your .ttf file:

<Design1>:
    orientation: "vertical"
    Label:
        font_name:"/path/to/DroidSansJapanese.ttf"
        text: '速'
        

I have not tested as an update today has screwed up something in kivy but it should work.

Upvotes: 0

Shiroi_Kyuketsuki
Shiroi_Kyuketsuki

Reputation: 73

For windows users who want to have their other language in a .kv file you have to manually load a .kv file using utf-8 encoding. Note that to avoid it being auto loaded and throwing a decoding exception anyways the .kv file must be named differently than your app. This is because windows assumes that a file is cp1252 encoded unless told otherwise. You must also use a font that supports the characters you are trying to display. I'm using Meiryo for Japanese. So for example:

main.py

#! /usr/bin/env python3

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.lang import Builder

#These two lines are what opens the .kv file and uses the rules written within.
with open("Japanese.kv", encoding='utf-8') as f: # Note the name of the .kv 
                                                 # doesn't match the name of the App
    Builder.load_string(f.read())

class JapaneseTest(Widget):
    pass

class JapaneseTestApp(App):
    def build(self):
        return JapaneseTest()

if __name__ == '__main__':
    JapaneseTestApp().run()

Japanese.kv

#:kivy 1.10.0

<JapaneseTest>:
    Label:
        size: self.texture_size
        font_size: 36
        font_name: "meiryo.ttc"
        text: "こんにちは!"

Of course make sure that the .kv file is written in the encoding that you specified. Most decent text editors should allow you to change this.

Upvotes: 2

Drako
Drako

Reputation: 768

I had same problem with Baltic letters and manage to find solution:

use u'\u0100' - this for example will create Latvian letter "Ā", so you have to find unicode codes for all your chars. Don't ask me why it needs double unicode declaration, but for me if I use u'Ā' it would work on Android but make crap on win pc, but in case of '\u0100' I would have correct letter on win pc but crap on Android; when double escaped it works on both. Hope this helps someone else too :) cause I wasted few hours before figure it out :) PS. for Japanese you might need to use U'\U(8char hex)'

Upvotes: 0

kitti
kitti

Reputation: 14814

Make sure your kv file is using utf-8 character encoding, and it should work fine. Most decent text editors will allow you to select the encoding.

Do not use u'', as the file is already unicode (attempting to use u'' will result in decode errors).

I had issues with the arialuni font (didn't show kanji, but it did show the kana, and the top half was cut off of all characters, including Western letters). So I used a different font instead (TakaoPMincho - easy to install in Ubuntu).

kanjitest.kv:

<TestWidget>:
    Label:
        text: '速 dsf あい'
        font_name: 'TakaoPMincho.ttf'
        font_size: sp(48)

Result:

enter image description here

Upvotes: 3

Related Questions