What is internationalization? Internationalization (i18n): Preparing your application to be localized (Pro-tip: How? Watch this talk!) Localization (L10n): Translating, adding icons and other things of certain zone 3
L10n: Definition "A set of parameters that defines the user's language, country and any special variant preferences that the user wants to see in their user interface" Wikipedia What it means: rules for a specific region ‣ Current code standard: RFC 4646 ‣ Language in lowercase (ISO 639-1), hyphen, region in uppercase (ISO 3166-1 alpha-2) 6
L10n: Determining which to use Introducing the \Locale object! 13 Best practice tips: ‣ Get it from your $_GET or $_POST ‣ Get it from the headers ‣ Get it based on ip
\Locale: cool functions! 16 Use To… ‣ \Locale::getDisplayLanguage() Create a list of languages ‣ \Locale::getDisplayRegion() Display the country where the locale is used ‣ \Locale::acceptFromHttp() Parse browser Accept header ‣ \Locale::setDefault() Set the default localization to use
L10n: what to look out for? ‣ Warning! Locale is not just a translation ‣ What's normal for you can be strange to others "Diversity is amazing, both in appearance and thoughts, please respect different opinions, agree to disagree and live in harmony." @michellesanver 17
Images and gestures ‣ Pointing fingers can be offensive in Arabic countries 18 ✌ ‣ The "peace" sign is offensive in Australia, Ireland, New Zealand, South Africa and the United Kingdom
Translations - Semantics (L10n) 19 echo $numberResults.' results found within a '.$range.'km range'; If translated directly into Spanish, it will sound like this: ➡ "Found 123 results within range 5km"
Translations - Semantics (i18n) 20 ‣Solvable by implementing printf() printf( '%1$d results found within a %2$d km range', $numberResults, $range ); ‣Translator decides where to print out variables
Translations - Semantics (L10n) 23 British American Holiday Vacation Football Soccer American Football Football Flat Apartment Garden Yard Rubbish Garbage / Trash
Translations - Semantics 24 Smart Phones Mobile phones Barcode Scanners Control Remotes See Printers Interactive question! What's wrong with the following list?
Translations - Plural forms (L10n) 27 English Polish 0 Apples Jabłek 1 Apple Jabłko 2 .. 4 Apples Jabłka 5 .. 21 Apples Jabłek 22 .. 24 Apples Jabłka 25 .. 31 Apples Jabłek More complex cases do exist! ‣ Slovenian: 4 plural forms There are also cases with 1 plural form ‣ Japanese ‣ Vietnamese
Translations - Plural forms (i18n) 28 Gettext! ‣ Supports plural forms ‣ Is cached in RAM (pros and cons) ‣ Very easy to edit (poEdit) ‣ Can be separated into modules ‣ Produces compiled language files
Translations - Plural forms (i18n) ‣ \MessageFormatter can also help ‣ Can do pretty amazing stuff ‣ I personally don't have experience with it 29 $fmt = new MessageFormatter( 'en_GB', 'Peter has {0, plural, =0{no cat} =1{a cat} other{# cats}}' ); echo $fmt->format(array(0)); $fmt = new MessageFormatter( 'nl_NL', 'Peter heeft {0, plural, =0{geen kat} =1{een kat} other{# katten}}' ); echo $fmt->format(array(0));
// Outputs: // Peter has no cat // Peter heeft geen kat
Number formatting - L10n ‣ Numbers have a lot of different types of annotations ‣ Corollary: nobody really knows well how a number should be formatted 30 Interactive question! How do you format the following negative number, in Euros, here in the Netherlands? 1234,57
foreach ($locales as $myLocale) { $numberFormatter = new \NumberFormatter($myLocale, \NumberFormatter::DECIMAL); $percentFormatter = new \NumberFormatter($myLocale, \NumberFormatter::PERCENT); $currencyFormatter = new \NumberFormatter($myLocale, \NumberFormatter::CURRENCY); printf('Locale: %s'.PHP_EOL, $myLocale); printf('[DEC]-1.234,57: "%s" :: ', $numberFormatter->format(-1234.57)); printf('[PER]25%%: "%s" :: ', $percentFormatter->format(0.25)); printf('[CUR]1.234,57-: "%s"'.PHP_EOL, $currencyFormatter->formatCurrency(-1234.57, 'EUR')); // Last argument can also be \NumberFormatter::CURRENCY to print in CURRENCY of loaded locale }
foreach ($locales as $myLocale) { $numberFormatter = new \NumberFormatter($myLocale, \NumberFormatter::DECIMAL); $percentFormatter = new \NumberFormatter($myLocale, \NumberFormatter::PERCENT); $currencyFormatter = new \NumberFormatter($myLocale, \NumberFormatter::CURRENCY); printf('Locale: %s'.PHP_EOL, $myLocale); printf('[DEC]-1.234,57: "%s" :: ', $numberFormatter->format(-1234.57)); printf('[PER]25%%: "%s" :: ', $percentFormatter->format(0.25)); printf('[CUR]1.234,57-: "%s"'.PHP_EOL, $currencyFormatter->formatCurrency(-1234.57, 'EUR')); // Last argument can also be \NumberFormatter::CURRENCY to print in CURRENCY of loaded locale }
Some \NumberFormatter problems ‣ \NumberFormatter::DURATION isn't implemented in much locales ‣ echo $fmt->format(12345) -> 3 hours, 25 minutes, 45 seconds ‣ "Easy" to implement using getPattern() and setPattern() ‣ Documentation exists, but is not optimal 35
Date and time formatting - L10n ‣ Dates have 3 different annotations ‣ YYYY-MM-DD (1.660M) ‣ DD-MM-YYYY (4.810M) ‣ MM-DD-YYYY (320M) ‣ "It's complicated" (457M) ‣ Contrary to numbers, everybody knows how a date is formatted 36 Interactive question! What is the value of the following date? 05-03-13
Date and time formatting - L10n ➡ YMD (1660) ➡ YMD and DMY (287) ➡ DMY (3295) ➡ DMY and MDY (130) ➡ MDY (320) ➡ YMD and DMY and MDY (40) 37 https://en.wikipedia.org/wiki/Date_format_by_country
Date and time formatting: i18n ‣ Use PHP's \*Date* related classes, like ALWAYS! ‣ Incredibly versatile yet powerful functions ‣ Specially in combination with locales ‣ Always work in UTC, let the \*Date* classes do the rest 38
Date and time formatting: i18n 39 nl-NL fr-FR hi-IN ps-AR Short "23-05-15" "23/05/15" "२३-५-१५" "۲۰۱۵/۵/۲۳" Medium "23 mei 2015" "23 mai 2015" "२३-०५-२०१५" "۲۳ ۲۰۱۵ یم" With time "23 mei 2015 01:34:09" "23 mai 2015 01:34:09" "२३-०५-२०१५ १:३४:०९ पूवार्ह्न" "۲۳ ۱:۳۴:۰۹ ۲۰۱۵ یم"
i18n/L10n and OS ‣ Variety in L10n is almost infinite ‣ Automatic in i18n and L10n is better ‣ Operating system plays an important role ‣ Why reinvent a very very very complicated wheel if it already exists? 41
Timezones - L10n ‣ PHP has full support for timezones ‣ 39 (40?) official timezones ‣ Multiple timezones in one locale ‣ nl-NL: Europe/Amsterdam ‣ es-CL: America/Santiago and Easter/ Pacific ‣ en-US: Has 4 timezones ‣ ru-RU: Has 8 timezones 43 Interactive question! What time is it now in Seoul (ko-KR)?
Timezones: i18n Caution! Calls to ICU library can get pretty expensive! ‣ With a known locale, get all timezones ‣ If there's only one, instantiate \DateTimeZone ‣ More than 1? Get precise timezoneId and DST settings (cache them!) ‣ Now calculate the offset of a timezone for the view 44
Timezones: i18n - Set timezone 47 public function setTimezone($timeZoneName='UTC') { if (!$this->isValidTimeZone($timeZoneName)) { $timeZoneName = 'UTC'; }
Encoding and charsets - L10n ‣ Difficult, often misunderstood subject ‣ Difficult to debug ‣ First step of debugging is knowing what encoding you are working with ‣ Convert to an appropriate charset with iconv() 50
Encoding in PHP ‣ Internal work always in UTF-8, EVERYWHERE ‣ Include some basic stuff so that PHP also knows that it has to work in UTF-8 ‣ Don't forget to send the browser information as well 51 mb_internal_encoding('UTF-8'); header('Content-type: %s; charset=UTF-8'); ‣ Lots of small things to consider, but can vary on each case
Encoding in PHP: mails Caution with the imap extension! Has some problems with UTF-7 Always encode "To" (BC, BCC) and "Subject" fields 52 Code Output "=?utf-8?B?5L2p5ae/?= " 佩姿 "=?iso-8859-1?Q?B=F8lla?=, med =?iso-8859-1?Q?=F8l?= i baggen " Bølla , med øl i baggen "=?utf-7?Q?Petra_M+APw-ller?=" Petra Müller
Practical use of charset Rule of thumb: adjust to the best possible way according to input md5/SHA1-like strings should be ASCII-encoded (Why? It helps the db engine to predict better its memory assignment) 58 Interactive question! What charset should be used to save the following string? f5d39e997c5d7e4e2a3ef49973f61fb2
Differences between TEXT and [VAR]CHAR ‣ [VAR]CHAR(255) holds up to 255 characters ‣ TINYTEXT can hold up to 255 bytes ‣ UTF-8 characters can take up to 5 (or more) bytes 60
Indexes and charsets When working with Unicode characters, performance can be indirectly and negatively impacted ‣ Too big (and complex) of a topic for now ‣ Use EXPLAIN to understand underlying decisions of MySQL (in some cases) ‣ Don't bother in micro-optimization either 61
COLLATION ‣ Used to order data in a "natural" way ‣ Different languages have different rules 62 CREATE TABLE `spanishCollation` ( `name01` VARCHAR(15) COLLATE utf8_spanish_ci, `name02` VARCHAR(15) COLLATE utf8_spanish2_ci ) DEFAULT CHARSET utf8;
Some notes on Collation "*_ci" stands for case-insensitive Watch out with utf8_general_ci and utf8_unicode_ci! ➡ utf8_general_ci has some problems with Hebrew and some cyrillic characters ➡ It's generally faster (7~12%) ➡ But utf8_unicode_ci is more compatible 63
Collation and performance ‣ Performance penalty: order in another collation ‣ It will have to do a filesort ‣ Which is MySQL's way of saying "quicksort" ‣ [Partial] keys can help avoid this quicksort operation 64
General database localization ‣ Not recommended: translation on database level ‣ If absolutely needed, investigate EAV model ‣ PRO: Quick, simple and cheap ‣ CON: Queries may become complex 65
Your own L10n database ‣ Does the locale use the metric or imperial system (either British or American)? ‣ What type of rounding is used in that locale? ‣ Optional: custom number and currency pattern to overwrite any default rules ‣ The preferred timezone (user based, not L10n based) ‣ Direction of text 66
Fonts ‣ Easily overseen, yet very important ‣ Web-safe fonts are generally safe to use ‣ Don't forget to test multibyte characters ‣ 2 bytes: ñÖÑú - ӬģĽ ‣ 3 bytes: 漢字 - ♥၍₶ ‣ 4+bytes: - ‣ Example: Mamá vive en Föllinge en el bosque del Ñañdú.¿Enredado? ¡Deberías! (SimSun-ExtB) 67
JavaScript considerations ‣ Always use native Date() object ‣ Has support for timezones ‣ No native support for i18n on Javascript ‣ http://i18next.com is able to save the day! 68
Finally: Who am I? ‣ Blog: http://blog.unreal4u.com/ (Spanish) ‣ Rate and comment: https://joind.in/talk/view/15219 ‣ Please, it's the only way this talk (and others) can be improved ‣ Slides are ready to be downloaded: ‣ http://unreal4u.com/talks/ ‣ https://speakerdeck.com/unreal4u 70