Hi everybody.
One of the programming problems that you will inevitably encounter is the localization of applications in different countries of linguistic data.
Today I will introduce you 10 tips to solve the language problem.
1. Set timezone for the application
The application should set a time zone for itself. Otherwise, the server / server time zone will be lost, which is probably not what the user wants.
1 2 3 | date_default_timezone_set( 'Asia/Ho_Chi_Minh' ); |
You should run this function as soon as possible in your project.
Alternatively, you can use the timezone_identifiers_list()
function to create a list of time zones from which the user can choose.
For example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | /** Lấy 1 list timezones khả dụng cho ứng dụng */ function get_timezones() { $o = array(); $t = timezone_identifiers_list(); foreach($t as $a) { $t = ''; //Get the time difference $zone = new DateTimeZone($a); $seconds = $zone->getOffset( new DateTime('now', $zone) ); $hours = sprintf( '%+02d' , intval($seconds/3600)); $minutes = sprintf( '%02d' , ($seconds%3600)/60 ); $t = $a . "[ $hours:$minutes ]"; $o[$a] = $t; } ksort($o); return $o; } |
The above function will return a list of timezone like this:
- [Africa / Abidjan] => Africa / Abidjan [+0: 00]
- [Africa / Accra] => Africa / Accra [+0: 00]
- [Africa / Addis_Ababa] => Africa / Addis_Ababa [+3: 00]
- [Africa / Algiers] => Africa / Algiers [+1: 00]
- [Africa / Asmara] => Africa / Asmara [+3: 00]
- [Africa / Bamako] => Africa / Bamako [+0: 00]
- [Africa / Bangui] => Africa / Bangui [+1: 00]
- [Africa / Banjul] => Africa / Banjul [+0: 00]
- [Africa / Bissau] => Africa / Bissau [+0: 00]
- ….
You can then use select to let users choose their timezone.
2. Set the timezone for mysql from the project
Projects are usually hosted on servers in different time zones, compared to developers and users’ local machines. Therefore, mysql’s time zone needs to be set to the user’s time.
You can execute the following SQL statement to set timezone for MySql:
1 2 3 | $c = mysqli_connect($host, $username, $password); mysqli_query($c , "SET `time_zone` = '" . date('P') . "'"); |
The date
function of php with the parameter P
used to get the time difference between the current and the UTC time.
TIMESTAMP
For the TIMESTAMP
fields in mysql, every time-date combination will be converted to the correct timestamp
by mysql
, since mysql
itself is in the correct time zone.
DATETIME
When using datetime
columns, the application needs to convert date and time values into UTC, then save to mysql.
When retrieving data from a database, we must convert that value into the application’s time zone.
Save to database:
1 2 3 4 5 6 7 8 9 | $time_to_save = date('Y-m-d h:i:s'); //Thời gian hiện tại của người dùng sẽ được lưu trong cột datetime $d = new DateTime($time_to_save , new DateTimeZone(date_default_timezone_get()) ); $d->setTimezone( new DateTimeZone('UTC') ); //Chuyển đổi sang giờ UTC $mysql_datetime = $d->format('Y-m-d h:i:s'); //Giá trị cuối cùng được lưu trong cơ sở dữ liệu |
3. Set collation for database at creation time
collation
database collation
needs to be set right from the start, to avoid errors later and always be consistent when backing up and dumping the database.
1 2 3 | ALTER DATABASE db_name CHARACTER SET utf8 COLLATE utf8_unicode_ci; |
Or you can edit the collation
after creating the database. Setting collation
, character
set from the beginning for the database ensures that all subsequent tables and columns will be the same by default.
4. Set the character for mysql connection object
When the application is displaying non-English characters, it must set the character encoding of mysql connection.
For example:
1 2 | mysqli_set_charset ( $connect , 'UTF8' ); |
It’s easy, isn’t it?
5. Set the correct character for htmlentities
The htmlentities
function is used to print out the content, previously provided by the user. Prior to php 5.4, the default character encoding used as ISO-8859-1 could not display languages like hindi, bengali, russian etc.
1 2 3 | $value = htmlentities($value , ENT_QUOTES , 'UTF-8'); echo $value; |
Php 5.4 onwards, the default encoding will be UTF-8 which will solve most problems, but still be more aware of it if your application is multilingual.
You can also use the mb_list_encodings
function that can be used to create lists for selected users or you will depend on the user’s language and processing.
1 2 3 4 5 6 7 8 9 10 11 12 13 | //Lấy danh sách các mã được hỗ trợ bằng hàm mb_list_encodings function get_supported_encodings() { $e = mb_list_encodings(); $list = array(); foreach($e as $c => $val) { $list[$val] = $val; } return $list; } |
After running the above function, you will get the following result:
- [UTF-32BE] => UTF-32BE
- [UTF-32LE] => UTF-32LE
- [UTF-16] => UTF-16
- [UTF-16BE] => UTF-16BE
- [UTF-16LE] => UTF-16LE
- [UTF-8] => UTF-8
- [UTF-7] => UTF-7
- [UTF7-IMAP] => UTF7-IMAP
- [ASCII] => ASCII
- [EUC-JP] => EUC-JP
- [SJIS] => SJIS
- …
6. Set encoding for non-html content by sending the correct headers
When you want to use the file: css, javascript, xml, etc., set the correct encoding via the header
function like this:
XML
1 2 3 4 | header('Content-Type: text/xml; charset=utf-8'); // Echo phần còn lại của nội dung xml |
Javascript
1 2 3 | header('Content-Type: text/javascript; charset=utf-8'); // Echo phần còn lại của nội dung javascript |
7. Set character Sets b correctly for html and xml output
HTML: This is in the head tag of the html document
1 2 3 4 5 | [html] <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> [/ html] |
Support for html5:
1 2 3 4 5 | [html] <meta charset="utf-8"> [/ html] |
XML: The first line of the xml document has the specified encoding
1 2 3 4 5 | [xml] <?xml version="1.0" encoding="utf-8"?> [/ xml] |
8. Set the correct locale
Setting the correct locale
will be useful for language-related settings such as numeric formatting, currency symbols, etc. Setting the correct locale
for the application should be set up as soon as possible in the project.
1 2 3 | setlocale(LC_ALL, 'en_US.UTF-8'); |
You should also provide the user with a list of locations to choose from, or from your language choose a locale to use appropriately.
9. Format numbers according to locale
When displaying numbers as regular numbers or amounts, you should format them with commas, decimals, etc. using the money_format
function.
For example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | $amount = '100000'; //Định dạng số tại Indian - Lakh, Crore setlocale(LC_MONETARY, 'en_IN'); $am = money_format('%!i', $amount); echo $am; //1,00,000.00 //Định dạng triệu, tỷ echo "<br />"; setlocale(LC_MONETARY, 'en_US.utf8'); $am = money_format('%!i', $amount); echo $am; //100,000.00 |
10. Date display format
The date format displays according to the user format.
There are several types of different commonly used date formats:
1 2 3 4 5 | Định dạng ISO: 2020-05-26 (YYYY-MM-DD) Các nước Asian: 26-05-2020 (DD-MM-YYYY) Mỹ: 05-26-2020 (MM-DD-YYYY) |
Dates must be formatted both when displayed and when being entered into an input form.
1 2 3 | Giá trị trên form: 26/05/2020 or 05/26/2020 Hiển thị: 26 May 2020 or May 26, 2020 |
Format dates with strftime
function:
1 2 3 4 5 6 | $format = '%d-%b-%Y'; $stamp = time(); $display_format = strftime( $format , $stamp); echo $display_format; //Output 26-05-2020 |
Format with DateTime
class:
1 2 3 4 5 | $t = date('Y-m-d h:i:s'); $d = new DateTime($t , new DateTimeZone(date_default_timezone_get()) ); $display_format = $d->format('d-m-Y'); echo $display_format; //26-05-2020 |
Depending on the project, users should have the option to select the format to display the date and enter the date in the form or the programmer can choose the format according to the user’s locale to handle.
Conclude
So I’ve finished 10 tips to localize the project, hoping to help you with any part of the programming process.
See you in the following article.
Thanks you!