Support Chinese Characters End to End
I have an App that I hope to support Chinese (or any other language) Characters. But it took me a while to find a one page references that enable me to support from end-to-end. It’s hard to know where go wrong sometimes. So record down my findings here briefly for my future reference and others.
In my App System, there are 3 major components: the Php web, the MySql Service, and the App itself.
Php web Support
For my Php side, in order to show Chinese, a special header is needed to define it as UTF-8 format. To do that, the code as follow.
header("Content-Type: text/plain; charset=UTF-8");
From there, you could safely assume when you print Chinese characters etc, it should be shown.
print “汉语/漢語 Hanyu; 华语/華語 Huáyǔ; 中文 Zhōngwén”;
More example in the below at code gist here.
For MySql, for the field that required Chinese character support, should be at least of type
utf8_bin type. This however only support the Transitional Chinese Character.
To support Simplified Chinese Character, one would need to have
In case you need an SQL command to alter your field type, refer below
ALTER TABLE `yourtable` CHANGE `yourfield` `yourfield` TEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL;
Php Queried MySql Result.
The above is just within the MySql, which could contain the String. But when you extract the MySql Query from Php, you’ll get to see your Chinese character as
In order to solve that, when connecting MySql from Php, right after connection, you need to set the character set to
$cnx = mysqli_connect($host, $user, $password, $dbname) \
or die (“Can’t open DB!”);
Within the App Support
By now, if you extract the server JSon String from your backend php site, you’ll get the Chinese Character to be displayed accordingly.
Just to add on, if you need translation for your App to another language, you could add another
array.xml) with a Locale set. You could manually put them to the
value-'locale' folder. You could find the value here. Optionally, you could create using the UI-Wizard, by adding in the
Then add all the String that you want translated into the new file.
Note, this is just within my single App, what is done. Let me know if there’s other major consideration missed.
P/S: For Chinese support, there’s another complication. A search of word in Traditional Chinese can’t be mapped to Simplified Chinese easily. If anyone has any idea how to get that handled, let me know.