Re: Display Japanese Characters in GtkComboBox



Thanks for the reply.

You get this strange squares because your strings don't seem to be having
valid UTF-8 characters. Gtk2 is expecting to get UTF-8 strings and what has
been provided doesn't seem to be too valid. Your perl strings are probably
not valid UTF-8. This can happen if the mysql driver fails to return proper
UTF-8 data. If you know the encoding of the database then decode each string
to UTF-8 as soon as you read them from the mysql database.


I thought my data was already in utf8 format.  I had similar issues
when connecting with a Perl script from the shell.
But if I do the following:

#!/usr/bin/perl
use strict;
use DBI();

# Connect to the database.
my $dbh = DBI->connect("DBI:mysql:database=japanese;host=localhost",
                                "root", "root",
                                {'RaiseError' => 1});

# Without this kanji appear as '?' marks.
my $sth_unicode = $dbh->prepare("SET NAMES utf8");
$sth_unicode->execute();

# Now retrieve data from the table.
my $sth = $dbh->prepare("SELECT * FROM vocab LIMIT 5");
$sth->execute();
while (my $ref = $sth->fetchrow_hashref()) {
        print "id = $ref->{'id'}, eng = $ref->{'english'} ";
        print "kanji = $ref->{'kanji'}\n";
}

$sth->finish();

# Disconnect from the database.
$dbh->disconnect();

The output is:
id = 1, eng = Ah!  kanji = NA
id = 2, eng = love (n) kanji = 愛
id = 3, eng = ice cream kanji = NA
id = 4, eng = to love kanji = 愛する
id = 5, eng = among, between, an interval (n) kanji = 間

With the kanji being printed to the terminal correctly.

use Encode;
$string = decode("iso-8859-1", $octets); # NOTE replace iso-8859-1 by your
database's actual encoding


Again I initially had issues trying to get the data into the database.
 I found it was necessary to have the server and the client speaking
utf8 to each other. So I have in my /etc/my.conf file:

[client]
default-character-set=utf8

[mysqld]
default-character-set=utf8

When looking at settings from the mysql client I have:
mysql> show variables like "character%";
+--------------------------+-----------------------------------------------------------------------+
| Variable_name            | Value
                            |
+--------------------------+-----------------------------------------------------------------------+
| character_set_client     | utf8
                            |
| character_set_connection | utf8
                            |
| character_set_database   | utf8
                            |
| character_set_filesystem | binary
                            |
| character_set_results    | utf8
                            |
| character_set_server     | utf8
                            |
| character_set_system     | utf8
                            |
| character_sets_dir       |
/usr/local/mysql-5.0.51a-linux-i686-icc-glibc23/share/mysql/charsets/
|
+--------------------------+-----------------------------------------------------------------------+
8 rows in set (0.00 sec)



You can rule out if it's a font issue by copy pasting a valid japanese
character into a gtk application (gedit). If you can see the character then
it's probably not a font issue.


I can type Japanese characters and copy and paste them into a number
of different apps including gedit.

Thanks for any other suggestions.

Peter.



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]