From 2032a643468ad37ccc0a019e29c44c776728b263 Mon Sep 17 00:00:00 2001 From: Igor Santos Date: Mon, 15 Feb 2016 03:36:09 -0200 Subject: [PATCH 01/33] Adding basic explanation on i18n and section TODOs - what's i18n, l10n and pluralization forms for - common methods of implementation - added the rest of the sub-sections, with TODOs --- ...1-Internationalization-and-Localization.md | 75 +++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 _posts/05-06-01-Internationalization-and-Localization.md diff --git a/_posts/05-06-01-Internationalization-and-Localization.md b/_posts/05-06-01-Internationalization-and-Localization.md new file mode 100644 index 0000000..eadf568 --- /dev/null +++ b/_posts/05-06-01-Internationalization-and-Localization.md @@ -0,0 +1,75 @@ +--- +title: Internationalization and Localization +isChild: true +anchor: i18n_l10n +--- + +## Internationalization (i18n) and Localization (l10n) {#i18n_l10n_title} + +_Disclaimer for newcomers: i18n and l10n are numeronyms, a kind of abbreviation where numbers are used to shorten +words - in our case, internationalization becomes i18n and localization, l10n._ + +First of all, we need to define those two similar concepts and other related things: + +- **Internationalization** is when you organize your code so it can be adapted to different languages or regions +without refactors. This is usually done once - preferably, in the beginning of the project, or else you'll probably +need some huge changes in the source! +- **Localization** happens when you adapt the interface (mainly) by translating contents, based on the i18n work done +before. It usually us done every time a new language or region needs support, and is updated when new interface pieces +are added, as they need to be available in all supported languages. +- **Pluralization** defines the rules needed between different languages to interoperate strings containing numbers and +counters. For instance, in English when you have only one item, it's singular, and anything different from that is +called plural; plural is this language is indicated by adding an S after some words, and sometimes changes parts of it. +In other languages such as Russian or Serbian there are two plural forms plus the singular one - you may even find +languages with a total of four, five or six forms, such as Slovenian, Irish or Arabic. + +## Common ways to implement + +The easiest way to internationalize PHP software is by using array files and using those strings in templates, such as +`

`. This is, however, hardly a recommended way for serious projects, as it poses +some maintenance issues along the road - some might appear in the very beginning, such as pluralization. So, please, +don't try this if your project will contain more than a couple of pages. + +Some frameworks will sport their own i18n packages. Those usually are a more powerful version of the above approach, +but including features needed for real localization, such as plural forms and string replacement. You're free to use +those if you feel like, but you might find bothering to edit array source files, having to deal with pure code issues +(such as string scaping and so on). The main pro here is integration with the environment you're using - the framework +is called _full-stack_ for a reason, right? + +However, the most classic way and often taken as reference for i18n and l10n is a [UNIX tool called `gettext`][gettext]. +It dates back to 1995 and is still the most complete implementation for translating software. It is pretty easy to get +running, while it still sports powerful supporting tools. It's about Gettext we will be talking here. Also, to help you +not get messy over the command-line, we will be presenting a great GUI application that can be used to easily update +your l10n source files. + +### Discussion on l10n keys +TODO: talk about static keys versus text keys, as in https://lingohub.com/blog/2013/07/php-internationalization-with-gettext-tutorial/#What_form_of_msgids_should_be_used + +## Gettext + +### Installation +TODO: You might need to install Gettext and the related PHP library by using your package manager, like `apt-get` or `yum`. + +### Structure +TODO: Talk about POT/PO/MO files and Poedit. Explain details about plural forms, directory structures and domains. + +### Sample implementation +TODO: Add sample code implementing i18n using gettext. + +### Everyday usage +TODO: Explain what's the l10n routine for a project with existing i18n in place, using Poedit (and maybe command line as seen +in the LingoHub file). + +#### Tips & Tricks +TODO: Talk about possible issue with caching. +TODO: Suggest creation of helper functions. + +### References + +* [Wikipedia: i18n and l10n](https://en.wikipedia.org/wiki/Internationalization_and_localization) +* [Wikipedia: Gettext](https://en.wikipedia.org/wiki/Gettext) +* [LingoHub: PHP internationalization with gettext tutorial](https://lingohub.com/blog/2013/07/php-internationalization-with-gettext-tutorial/) +* [PHP Manual: Gettext](http://br2.php.net/manual/en/book.gettext.php) + + +[gettext]: https://en.wikipedia.org/wiki/Gettext From 26b56073286852fa4541b9b4601632283f5a48e2 Mon Sep 17 00:00:00 2001 From: Igor Santos Date: Tue, 16 Feb 2016 04:24:16 -0200 Subject: [PATCH 02/33] i18n: Adding info on extensions, files and folders --- ...1-Internationalization-and-Localization.md | 78 ++++++++++++++++--- 1 file changed, 68 insertions(+), 10 deletions(-) diff --git a/_posts/05-06-01-Internationalization-and-Localization.md b/_posts/05-06-01-Internationalization-and-Localization.md index eadf568..3a297cb 100644 --- a/_posts/05-06-01-Internationalization-and-Localization.md +++ b/_posts/05-06-01-Internationalization-and-Localization.md @@ -24,7 +24,6 @@ In other languages such as Russian or Serbian there are two plural forms plus th languages with a total of four, five or six forms, such as Slovenian, Irish or Arabic. ## Common ways to implement - The easiest way to internationalize PHP software is by using array files and using those strings in templates, such as `

`. This is, however, hardly a recommended way for serious projects, as it poses some maintenance issues along the road - some might appear in the very beginning, such as pluralization. So, please, @@ -36,33 +35,89 @@ those if you feel like, but you might find bothering to edit array source files, (such as string scaping and so on). The main pro here is integration with the environment you're using - the framework is called _full-stack_ for a reason, right? -However, the most classic way and often taken as reference for i18n and l10n is a [UNIX tool called `gettext`][gettext]. +However, the most classic way and often taken as reference for i18n and l10n is a [Unix tool called `gettext`][gettext]. It dates back to 1995 and is still the most complete implementation for translating software. It is pretty easy to get running, while it still sports powerful supporting tools. It's about Gettext we will be talking here. Also, to help you not get messy over the command-line, we will be presenting a great GUI application that can be used to easily update your l10n source files. ### Discussion on l10n keys -TODO: talk about static keys versus text keys, as in https://lingohub.com/blog/2013/07/php-internationalization-with-gettext-tutorial/#What_form_of_msgids_should_be_used +> TODO: talk about static keys versus text keys, as in https://lingohub.com/blog/2013/07/php-internationalization-with-gettext-tutorial/#What_form_of_msgids_should_be_used ## Gettext ### Installation -TODO: You might need to install Gettext and the related PHP library by using your package manager, like `apt-get` or `yum`. +You might need to install Gettext and the related PHP library by using your package manager, like `apt-get` or `yum`. +After installed, enable it by adding `extension=gettext.so` (Linux/Unix) or `extension=php_gettext.dll` (Windows) to +your `php.ini`. ### Structure -TODO: Talk about POT/PO/MO files and Poedit. Explain details about plural forms, directory structures and domains. + +#### Types of files +There are three files you usually deal with while working with gettext. The main ones are PO (Portable Object) and +MO (Machine Object) files, the first being a list of readable "translated objects" and the second, the corresponding +binary to be interpreted by gettext when doing localization. There's also a POT (Template) file, that simply contains +all existing keys from your source files, and can be used as a guide to generate and update all PO files. Those template +files are not mandatory: depending on the tool you're using to do l10n, you can go just fine with only PO/MO files. +You'll always have one pair of PO/MO files per language and region, but only one POT per domain. + +### Domains +There are some cases, in big projects, where you might need to separate translations when the same words convey +different meaning given a context. In those cases you split them into different _domains_. They're basically named +groups of POT/PO/MO files, where the filename is the said _translation domain_. Small and medium-sized projects usually, +for simplicity, use only one domain; it's name is arbitrary, but we will be using "main" for our code samples. + +#### Locale code +A locale is simple code that identifies a version of a language. It's defined following [ISO 639-1][639-1] and +[ISO 3166-1 alpha-2][3166-1] specs: two lower-case letters for the language, optionally followed by an underline and two +upper-case letters identifying the country or regional code. For [rare languages][rare], three letters are used. + +For some speakers, the country part may seem redundant; but in fact, some languages have dialects in different +countries, such as Austrian German (`de_AT`) or Brazilian Portuguese (`pt_BR`). The second part is used to distinguish +between those dialects - when it's not present, it's taken as a "generic" or "hybrid" version of the language. + +### Directory structure +To use Gettext, we will need to adhere to a specific structure of folders. First, you'll need to select an arbitrary +root for your l10n files in your source repository. Inside it you'll have a folder for each needed locale, and a fixed +`LC_MESSAGES` folder that will contain all your PO/MO pairs. Example: + +{% highlight console %} + + ├─ src/ + ├─ templates/ + └─ locales/ + ├─ forum.pot + ├─ site.pot + ├─ de/ + │ └─ LC_MESSAGES/ + │ ├─ forum.mo + │ ├─ forum.po + │ ├─ site.mo + │ └─ site.po + ├─ es_ES/ + │ └─ LC_MESSAGES/ + │ └─ ... + ├─ fr/ + │ └─ ... + ├─ pt_BR/ + │ └─ ... + └─ pt_PT/ + └─ ... +{% endhighlight %} + +### Plural forms +> TODO ### Sample implementation -TODO: Add sample code implementing i18n using gettext. +> TODO: Add sample code implementing i18n using gettext. ### Everyday usage -TODO: Explain what's the l10n routine for a project with existing i18n in place, using Poedit (and maybe command line as seen +> TODO: Explain what's the l10n routine for a project with existing i18n in place, using Poedit (and maybe command line as seen in the LingoHub file). #### Tips & Tricks -TODO: Talk about possible issue with caching. -TODO: Suggest creation of helper functions. +> TODO: Talk about possible issue with caching. +> TODO: Suggest creation of helper functions. ### References @@ -70,6 +125,9 @@ TODO: Suggest creation of helper functions. * [Wikipedia: Gettext](https://en.wikipedia.org/wiki/Gettext) * [LingoHub: PHP internationalization with gettext tutorial](https://lingohub.com/blog/2013/07/php-internationalization-with-gettext-tutorial/) * [PHP Manual: Gettext](http://br2.php.net/manual/en/book.gettext.php) - +* [Gettext Manual](http://www.gnu.org/software/gettext/manual/gettext.html) [gettext]: https://en.wikipedia.org/wiki/Gettext +[639-1]: https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes +[3166-1]: http://en.wikipedia.org/wiki/ISO_3166-1_alpha-2 +[rare]: http://www.gnu.org/software/gettext/manual/gettext.html#Rare-Language-Codes From 844594e8cc89b637d439679cda91e997c98e1be3 Mon Sep 17 00:00:00 2001 From: Igor Santos Date: Tue, 23 Feb 2016 03:19:45 -0300 Subject: [PATCH 03/33] i18n: typos, keys, plurals and samples --- ...1-Internationalization-and-Localization.md | 205 ++++++++++++++++-- 1 file changed, 191 insertions(+), 14 deletions(-) diff --git a/_posts/05-06-01-Internationalization-and-Localization.md b/_posts/05-06-01-Internationalization-and-Localization.md index 3a297cb..cf09ca1 100644 --- a/_posts/05-06-01-Internationalization-and-Localization.md +++ b/_posts/05-06-01-Internationalization-and-Localization.md @@ -15,11 +15,11 @@ First of all, we need to define those two similar concepts and other related thi without refactors. This is usually done once - preferably, in the beginning of the project, or else you'll probably need some huge changes in the source! - **Localization** happens when you adapt the interface (mainly) by translating contents, based on the i18n work done -before. It usually us done every time a new language or region needs support, and is updated when new interface pieces +before. It usually is done every time a new language or region needs support, and is updated when new interface pieces are added, as they need to be available in all supported languages. - **Pluralization** defines the rules needed between different languages to interoperate strings containing numbers and counters. For instance, in English when you have only one item, it's singular, and anything different from that is -called plural; plural is this language is indicated by adding an S after some words, and sometimes changes parts of it. +called plural; plural in this language is indicated by adding an S after some words, and sometimes changes parts of it. In other languages such as Russian or Serbian there are two plural forms plus the singular one - you may even find languages with a total of four, five or six forms, such as Slovenian, Irish or Arabic. @@ -41,9 +41,6 @@ running, while it still sports powerful supporting tools. It's about Gettext we not get messy over the command-line, we will be presenting a great GUI application that can be used to easily update your l10n source files. -### Discussion on l10n keys -> TODO: talk about static keys versus text keys, as in https://lingohub.com/blog/2013/07/php-internationalization-with-gettext-tutorial/#What_form_of_msgids_should_be_used - ## Gettext ### Installation @@ -51,6 +48,10 @@ You might need to install Gettext and the related PHP library by using your pack After installed, enable it by adding `extension=gettext.so` (Linux/Unix) or `extension=php_gettext.dll` (Windows) to your `php.ini`. +Here we will also be using [Poedit] to create translation files. You will probably find it in your system's package +manager; it's available for Unix, Mac and Windows, and can be [downloaded for free in their website][poedit_download] +as well. + ### Structure #### Types of files @@ -65,7 +66,7 @@ You'll always have one pair of PO/MO files per language and region, but only one There are some cases, in big projects, where you might need to separate translations when the same words convey different meaning given a context. In those cases you split them into different _domains_. They're basically named groups of POT/PO/MO files, where the filename is the said _translation domain_. Small and medium-sized projects usually, -for simplicity, use only one domain; it's name is arbitrary, but we will be using "main" for our code samples. +for simplicity, use only one domain; its name is arbitrary, but we will be using "main" for our code samples. #### Locale code A locale is simple code that identifies a version of a language. It's defined following [ISO 639-1][639-1] and @@ -106,16 +107,181 @@ root for your l10n files in your source repository. Inside it you'll have a fold {% endhighlight %} ### Plural forms -> TODO +As we said in the introduction, different languages might sport different plural rules. However, gettext saves us from +this trouble once again. When creating a new .po file, you'll have to declare the [plural rules][plural] for that +language, and translated pieces that are plural-sensitive will have a different form for each of those rules. When +calling Gettext in code, you'll have to specify the number related to the sentence, and it will work out the correct +form to use - even using string substitution if needed. + +Plural rules include the number of plurals available and a boolean test with `n` that would define in which rule the +given number falls (starting the count with 0). For example: + +- Japanese: `nplurals=1; plural=0` - only one rule +- English: `nplurals=2; plural=(n != 1);` - two rules, first if N is one, second rule otherwise +- Brazilian Portuguese: `nplurals=2; plural=(n > 1);` - two rules, second if N is bigger than one, first otherwise + +Now that you understood the basis of how plural rules works - and if you didn't, please look at a deeper explanation +on the [LingoHub tutorial](lingohub) -, you might want to copy the ones you need from a [list][plural] instead of +writing them by hand. + +When calling out Gettext to do the localization of sentences that include counters, you'll have to pass to it the +related number as well. Gettext will work out what rule should be in effect and use the correct localized version. +You will need to include in the .po file a different sentence for each plural rule present in the language file. ### Sample implementation -> TODO: Add sample code implementing i18n using gettext. +After all that theory, let's get a little practical. Here's an excerpt of a .po file - don't mind with its format, +but instead the overall content, you'll learn how to edit it easily later: + +{% highlight po %} +msgid "" +msgstr "" +"Language: pt_BR\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Plural-Forms: nplurals=2; plural=(n > 1);\n" + +msgid "We're now translating some strings" +msgstr "Nós estamos traduzindo algumas strings agora" + +msgid "Hello %1$s! Your last visit was on %2$s" +msgstr "Olá %1$s! Sua última visita foi em %2$s" + +msgid "Only one unread message" +msgid_plural "%d unread messages" +msgstr[0] "Só uma mensagem não lida" +msgstr[1] "%d mensagens não lidas" +{% endhighlight %} + +The first section works like a header, having the `msgid` and `msgstr` specially empty. It describes the file encoding, +plural forms and other things that are less relevant. The second section translates a simple string from English to +Brazilian Portuguese, and the third does the same, but leveraging string replacement from [`sprintf`](sprintf) so the +translation may contain the user name and visit date. The last section is a sample of pluralization forms, displaying +the singular and plural version as `msgid` in English and their corresponding translations as `msgstr` 0 and 1 +(following the number given by the plural rule). There, string replacement is used as well so the number can be seen +directly in the sentence, by using `%d`. The plural forms always have two `msgid` (singular and plural), so it's +advised to not use a complex language as source of translation. + +### Discussion on l10n keys +As you might have noticed, we're using as source ID the actual sentence in English. That `msgid` is the same used +throughout all your `.po` files, meaning other languages will have the same format and the same `msgid` fields, but +translated `msgstr` lines. + +Talking about translation keys, there are two main "schools" here: + +1. `msgid` as a real sentence. The main advantage here is that, if there's pieces of the software untranslated in any +given language, it will be displaying in a meaningful-ish way. If you happen to translate by heart from English to +Spanish but needs help to translate to French, you might publish the new page with missing French sentences, and parts +of the website would be displayed in English instead. Another point is that it's much easier for the translator to +understand what's going on and make a proper translation based on the `msgid`. It also gives you "free" l10n for a +language - the source one. However, if you need to change the actual text, you would need to replace the same `msgid` +across several language files. +2. `msgid` as a unique, structured key. It would describe the sentence role in the application in a structured way, +including the template or part where the string is located instead of its content. It's a great way to have the code +organized, but would bring problems to the translator that would miss the context. A source translation file would be +needed as a basis for other translations - so the developer would ideally have an `en.po` file, that translators would +then read to understand what to write in `fr.po` for instance. This is also both good and bad, as missing translations +would display meaningless keys on screen (`TOP_MENU_WELCOME` instead of `Hello there, User!` on the given French +untranslated page), forcing translation to be complete before publishing - while translation errors would be really +awful in the interface. + +The [Gettext manual][manual] favors the first approach, as in general it's easier for translators and users in +case of trouble. That's how we will be working here as well. ### Everyday usage -> TODO: Explain what's the l10n routine for a project with existing i18n in place, using Poedit (and maybe command line as seen -in the LingoHub file). +In a common application, you would use some Gettext functions while writing static text in your pages. Those sentences +would then appear in `.po` files, get translated, compiled into `.mo` files and then, used by Gettext when rendering +the actual interface. Given that, let's tie together what we have discussed so far in a a step-by-step example: -#### Tips & Tricks +#### 1. A sample template file, including some different gettext calls +{% highlight php %} + + + +

+

+{% endhighlight %} + +- [`gettext()`][func] simply translates a `msgid` into it's corresponding `msgstr` for a given language. There's also +the shorthand function `_()` that works the same way; +- [`ngettext()`][n_func] does the same but with plural rules; +- there's also [`dgettext()`][d_func] and [`dngettext()`][dn_func], that allows you to override the domain for a single +call. More on domain configuration in the next example. + +#### 2. A sample setup file (`i18n_setup.php` as used above), selecting the correct locale and configuring Gettext +{% highlight php %} + '_']); }); + foreach ($langs as $browser_lang) { + if (valid($browser_lang)) { + $lang = $browser_lang; + break; + } + } +} + +// here we define the global system locale given the found language +putenv("LANG=$lang"); + +// this might be useful for date functions (LC_TIME) or money formatting (LC_MONETARY), for instance +setlocale(LC_ALL, $lang); + +// this will make Gettext look for ../locales//LC_MESSAGES/main.mo +bindtextdomain('main', '../locales'); + +// indicates in what encoding the file should be read +bind_textdomain_codeset('main', 'UTF-8'); + +// if your application has additional domains, as cited before, you should bind them here as well +bindtextdomain('forum', '../locales'); +bind_textdomain_codeset('forum', 'UTF-8'); + +// here we indicate the default domain the gettext() calls will respond to +textdomain('main'); + +// this would look for the string in forum.mo instead of main.mo +// echo dgettext('forum', 'Welcome back!'); +?> +{% endhighlight %} + +#### 3. Preparing translation for the first run +> TODO: explain how to install Poedit and how to setup it + +#### 4. Translating strings +> TODO: overall view on how to use Poedit for translation + +### Tips & Tricks > TODO: Talk about possible issue with caching. > TODO: Suggest creation of helper functions. @@ -123,11 +289,22 @@ in the LingoHub file). * [Wikipedia: i18n and l10n](https://en.wikipedia.org/wiki/Internationalization_and_localization) * [Wikipedia: Gettext](https://en.wikipedia.org/wiki/Gettext) -* [LingoHub: PHP internationalization with gettext tutorial](https://lingohub.com/blog/2013/07/php-internationalization-with-gettext-tutorial/) -* [PHP Manual: Gettext](http://br2.php.net/manual/en/book.gettext.php) -* [Gettext Manual](http://www.gnu.org/software/gettext/manual/gettext.html) +* [LingoHub: PHP internationalization with gettext tutorial](lingohub) +* [PHP Manual: Gettext](http://php.net/manual/en/book.gettext.php) +* [Gettext Manual][manual] +[Poedit]: https://poedit.net/ +[poedit_download]: https://poedit.net/download +[lingohub]: https://lingohub.com/blog/2013/07/php-internationalization-with-gettext-tutorial/#Plurals +[plural]: http://docs.translatehouse.org/projects/localization-guide/en/latest/l10n/pluralforms.html [gettext]: https://en.wikipedia.org/wiki/Gettext +[manual]: (http://www.gnu.org/software/gettext/manual/gettext.html) [639-1]: https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes [3166-1]: http://en.wikipedia.org/wiki/ISO_3166-1_alpha-2 [rare]: http://www.gnu.org/software/gettext/manual/gettext.html#Rare-Language-Codes + +[sprintf]: http://php.net/manual/en/function.sprintf.php +[func]: http://php.net/manual/en/function.gettext.php +[n_func]: http://php.net/manual/en/function.ngettext.php +[d_func]: http://php.net/manual/en/function.dgettext.php +[dn_func]: http://php.net/manual/en/function.dngettext.php From ca90db9b1a2489813a83a34d4940d2095583a661 Mon Sep 17 00:00:00 2001 From: Igor Santos Date: Sat, 27 Feb 2016 14:10:07 -0300 Subject: [PATCH 04/33] i18n: review and finishing post - part about key types has been reviewed - included the pieces about using Poedit, caching issues and usual helper functions --- ...1-Internationalization-and-Localization.md | 94 +++++++++++++------ 1 file changed, 66 insertions(+), 28 deletions(-) diff --git a/_posts/05-06-01-Internationalization-and-Localization.md b/_posts/05-06-01-Internationalization-and-Localization.md index cf09ca1..95b4ea2 100644 --- a/_posts/05-06-01-Internationalization-and-Localization.md +++ b/_posts/05-06-01-Internationalization-and-Localization.md @@ -108,7 +108,7 @@ root for your l10n files in your source repository. Inside it you'll have a fold ### Plural forms As we said in the introduction, different languages might sport different plural rules. However, gettext saves us from -this trouble once again. When creating a new .po file, you'll have to declare the [plural rules][plural] for that +this trouble once again. When creating a new `.po` file, you'll have to declare the [plural rules][plural] for that language, and translated pieces that are plural-sensitive will have a different form for each of those rules. When calling Gettext in code, you'll have to specify the number related to the sentence, and it will work out the correct form to use - even using string substitution if needed. @@ -121,15 +121,15 @@ given number falls (starting the count with 0). For example: - Brazilian Portuguese: `nplurals=2; plural=(n > 1);` - two rules, second if N is bigger than one, first otherwise Now that you understood the basis of how plural rules works - and if you didn't, please look at a deeper explanation -on the [LingoHub tutorial](lingohub) -, you might want to copy the ones you need from a [list][plural] instead of +on the [LingoHub tutorial](lingohub_plurals) -, you might want to copy the ones you need from a [list][plural] instead of writing them by hand. -When calling out Gettext to do the localization of sentences that include counters, you'll have to pass to it the +When calling out Gettext to do localization on sentences with counters, you'll have to give him the related number as well. Gettext will work out what rule should be in effect and use the correct localized version. -You will need to include in the .po file a different sentence for each plural rule present in the language file. +You will need to include in the `.po` file a different sentence for each plural rule defined. ### Sample implementation -After all that theory, let's get a little practical. Here's an excerpt of a .po file - don't mind with its format, +After all that theory, let's get a little practical. Here's an excerpt of a `.po` file - don't mind with its format, but instead the overall content, you'll learn how to edit it easily later: {% highlight po %} @@ -152,9 +152,11 @@ msgstr[1] "%d mensagens não lidas" {% endhighlight %} The first section works like a header, having the `msgid` and `msgstr` specially empty. It describes the file encoding, -plural forms and other things that are less relevant. The second section translates a simple string from English to +plural forms and other things that are less relevant. +The second section translates a simple string from English to Brazilian Portuguese, and the third does the same, but leveraging string replacement from [`sprintf`](sprintf) so the -translation may contain the user name and visit date. The last section is a sample of pluralization forms, displaying +translation may contain the user name and visit date. +The last section is a sample of pluralization forms, displaying the singular and plural version as `msgid` in English and their corresponding translations as `msgstr` 0 and 1 (following the number given by the plural rule). There, string replacement is used as well so the number can be seen directly in the sentence, by using `%d`. The plural forms always have two `msgid` (singular and plural), so it's @@ -167,21 +169,27 @@ translated `msgstr` lines. Talking about translation keys, there are two main "schools" here: -1. `msgid` as a real sentence. The main advantage here is that, if there's pieces of the software untranslated in any -given language, it will be displaying in a meaningful-ish way. If you happen to translate by heart from English to -Spanish but needs help to translate to French, you might publish the new page with missing French sentences, and parts -of the website would be displayed in English instead. Another point is that it's much easier for the translator to -understand what's going on and make a proper translation based on the `msgid`. It also gives you "free" l10n for a -language - the source one. However, if you need to change the actual text, you would need to replace the same `msgid` -across several language files. -2. `msgid` as a unique, structured key. It would describe the sentence role in the application in a structured way, -including the template or part where the string is located instead of its content. It's a great way to have the code -organized, but would bring problems to the translator that would miss the context. A source translation file would be -needed as a basis for other translations - so the developer would ideally have an `en.po` file, that translators would -then read to understand what to write in `fr.po` for instance. This is also both good and bad, as missing translations -would display meaningless keys on screen (`TOP_MENU_WELCOME` instead of `Hello there, User!` on the given French -untranslated page), forcing translation to be complete before publishing - while translation errors would be really -awful in the interface. +1. _`msgid` as a real sentence_. + The main advantage are: + - if there's pieces of the software untranslated in any given language, the key displayed will still maintain some + meaning. Example: if you happen to translate by heart from English to Spanish but needs help to translate to French, + you might publish the new page with missing French sentences, and parts of the website would be displayed in English + instead; + - it's much easier for the translator to understand what's going on and make a proper translation based on the + `msgid`; + - it gives you "free" l10n for one language - the source one; + - The only disadvantage: if you need to change the actual text, you would need to replace the same `msgid` + across several language files. + +2. _`msgid` as a unique, structured key_. It would describe the sentence role in the application in a structured way, +including the template or part where the string is located instead of its content. + - it's a great way to have the code organized, separating the text content from the template logic. + - however, that could bring problems to the translator that would miss the context. A source language file would be + needed as a basis for other translations. Example: the developer would ideally have an `en.po` file, that + translators would read to understand what to write in `fr.po` for instance. + - missing translations would display meaningless keys on screen (`TOP_MENU_WELCOME` instead of `Hello there, User!` + on the said untranslated French page). That's good as would force translation to be complete before publishing - + but bad as translation issues would be really awful in the interface. The [Gettext manual][manual] favors the first approach, as in general it's easier for translators and users in case of trouble. That's how we will be working here as well. @@ -276,14 +284,42 @@ textdomain('main'); {% endhighlight %} #### 3. Preparing translation for the first run -> TODO: explain how to install Poedit and how to setup it +To make matters easier - and one of the powerful advantages Gettext has over custom framework i18n packages - is it's custom file type. "Oh man, that's quite hard to understand and edit by hand, a simple array would be easier!" Make no mistake, applications like [Poedit] are here to help - _a lot_. You can get the program from [their website], it's free and available for all platforms. It's a pretty easy tool to get used to, and a very powerful one at the same time - using with responsability the powers Gettext gave it. + +In the first run, you should select "File > New Catalog" from the menu. There you'll have a small screen where we will set the terrain so everything else runs smoothly. You'll be able to find those settings later through "Catalog > Properties": + +- Project name and version, Translation Team and email address: useful information that goes in the `.po` file header; +- Language: here you should use that format we mentioned before, such as `en_US` or `pt_BR`; +- Charsets: UTF-8, preferably; +- Source charset: set here the charset used by your PHP files - probably UTF-8 AS well, right? +- plural forms: here goes those rules we mentioned before - there's a link in there with samples as well; +- Source paths: here you must include all folders from the project where `gettext()` (and siblings) will happen - this is usually your templates folder(s) +- Source keywords: this last part is filled by default, but you might need to alter it later - and is one of the powerful points of Gettext. The underlying software knows how the `gettext()` calls look like in several programming languages, but you might as well create your own translation forms. This will be discussed later in the "Tips" section. + +After setting those points you'll be prompted to save the file - using that directory structure we mentioned as well, and then it will run a scan through your source files to find the localization calls. They'll be fed empty into the translation table, and you'll start typing in the localized versions of those strings. Save it and a `.mo` file will be (re)compiled into the same folder and ta-dah: your project is internationalized. #### 4. Translating strings -> TODO: overall view on how to use Poedit for translation +As you may have noticed before, there are two main types of localized strings: simple ones and the ones with plural forms. The first ones have simply two boxes: source and localized string. The source string can't be modified as Gettext/Poedit do not include the powers to alter your source files - you should change the source itself and rescan the files. Tip: you may right-click a translation line and it will hint you with the source files and lines where that string is being. +On the other hand, plural form strings include two boxes to show the two source strings, and tabs so you can configure the different final forms. + +Whenever you change your sources and need to update the translations, just hit Refresh and Poedit will rescan the code, removing non-existent entries, merging the ones that changed and adding new ones. It may also try to guess some translations, based on other ones you did. Those guesses and the changed entries will receive a "Fuzzy" marker, indicating it needs review, being highlighted in the list. It's also useful if you have a translation team and someone tries to write something they're not sure about: just mark Fuzzy and someone else will review later. + +Finally, it's advised to leave "View > Untranslated entries first" marked, as it will help you _a lot_ to not forget any entry. From that menu you can also open parts of the UI that allow you to leave contextual information for translators, if needed. ### Tips & Tricks -> TODO: Talk about possible issue with caching. -> TODO: Suggest creation of helper functions. + +#### Possible caching issues +If you're running PHP as a module on Apache (`mod_php`), you might face issues with the `.mo` file being cached. It happens the first time it's read, and then, to update it, you might need to restart the server. On Nginx and PHP5 it usually takes only a couple of page refreshes to refresh the translation cache, and on PHP7 it is rarely needed. + +#### Additional helper functions +As preferred by many people, it's easier to use `_()` instead of `gettext()`. Many custom i18n libraries from frameworks use something similar to `t()` as well, to make translated code shorter. However, that's the only function that sports a shortcut. You might want to add in your project some others, such as `__()` or `_n()` for `ngettext()`, or maybe a fancy `_r()` that would join `gettext()` and `sprintf()` calls. + +In those cases, you'll need to instruct the Gettext utility on how to extract the strings from those new functions. Don't be afraid, it's something very easy. It's just a field in the `.po` file, or a Settings screen on Poedit - remember when we mentioned it before? In the editor that option is inside "Catalog > Properties > Source keywords". You need to include there the specifications of those new functions, following [a specific format](func_format): + +- if you create something like `t()` that simply returns the translation for a string, you can specify it as `t`. Gettext will know the only function argument is the string to be translated; +- if the function has more than one argument, you can specify in which one the first string is - and if needed, the plural form as well. For instance, if we call our function like this: `__('one user', '%d users', $number)`, the specification would be `__:1,2`, meaning the first form is the first argument, and the second form is the second argument. If your number comes as first argument instead, the spec would be `__:2,3`, indicating the first form is the second argument, and so on. + +After including those new rules in the `.po` file, a new scan will bring your new strings just as easy as before. ### References @@ -295,13 +331,15 @@ textdomain('main'); [Poedit]: https://poedit.net/ [poedit_download]: https://poedit.net/download -[lingohub]: https://lingohub.com/blog/2013/07/php-internationalization-with-gettext-tutorial/#Plurals +[lingohub]: https://lingohub.com/blog/2013/07/php-internationalization-with-gettext-tutorial/ +[lingohub_plurals]: https://lingohub.com/blog/2013/07/php-internationalization-with-gettext-tutorial/#Plurals [plural]: http://docs.translatehouse.org/projects/localization-guide/en/latest/l10n/pluralforms.html [gettext]: https://en.wikipedia.org/wiki/Gettext -[manual]: (http://www.gnu.org/software/gettext/manual/gettext.html) +[manual]: http://www.gnu.org/software/gettext/manual/gettext.html [639-1]: https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes [3166-1]: http://en.wikipedia.org/wiki/ISO_3166-1_alpha-2 [rare]: http://www.gnu.org/software/gettext/manual/gettext.html#Rare-Language-Codes +[func_format]: https://www.gnu.org/software/gettext/manual/gettext.html#Language-specific-options [sprintf]: http://php.net/manual/en/function.sprintf.php [func]: http://php.net/manual/en/function.gettext.php From 56b01c71d6b616f671b21f2e3175503079e7198a Mon Sep 17 00:00:00 2001 From: Igor Santos Date: Sat, 27 Feb 2016 22:52:59 -0300 Subject: [PATCH 05/33] i18n: fixing typos and right column margin --- ...1-Internationalization-and-Localization.md | 88 +++++++++++++------ 1 file changed, 62 insertions(+), 26 deletions(-) diff --git a/_posts/05-06-01-Internationalization-and-Localization.md b/_posts/05-06-01-Internationalization-and-Localization.md index 95b4ea2..0307942 100644 --- a/_posts/05-06-01-Internationalization-and-Localization.md +++ b/_posts/05-06-01-Internationalization-and-Localization.md @@ -121,8 +121,8 @@ given number falls (starting the count with 0). For example: - Brazilian Portuguese: `nplurals=2; plural=(n > 1);` - two rules, second if N is bigger than one, first otherwise Now that you understood the basis of how plural rules works - and if you didn't, please look at a deeper explanation -on the [LingoHub tutorial](lingohub_plurals) -, you might want to copy the ones you need from a [list][plural] instead of -writing them by hand. +on the [LingoHub tutorial](lingohub_plurals) -, you might want to copy the ones you need from a [list][plural] instead +of writing them by hand. When calling out Gettext to do localization on sentences with counters, you'll have to give him the related number as well. Gettext will work out what rule should be in effect and use the correct localized version. @@ -170,9 +170,9 @@ translated `msgstr` lines. Talking about translation keys, there are two main "schools" here: 1. _`msgid` as a real sentence_. - The main advantage are: - - if there's pieces of the software untranslated in any given language, the key displayed will still maintain some - meaning. Example: if you happen to translate by heart from English to Spanish but needs help to translate to French, + The main advantages are: + - if there are pieces of the software untranslated in any given language, the key displayed will still maintain some + meaning. Example: if you happen to translate by heart from English to Spanish but need help to translate to French, you might publish the new page with missing French sentences, and parts of the website would be displayed in English instead; - it's much easier for the translator to understand what's going on and make a proper translation based on the @@ -188,7 +188,7 @@ including the template or part where the string is located instead of its conten needed as a basis for other translations. Example: the developer would ideally have an `en.po` file, that translators would read to understand what to write in `fr.po` for instance. - missing translations would display meaningless keys on screen (`TOP_MENU_WELCOME` instead of `Hello there, User!` - on the said untranslated French page). That's good as would force translation to be complete before publishing - + on the said untranslated French page). That's good it as would force translation to be complete before publishing - but bad as translation issues would be really awful in the interface. The [Gettext manual][manual] favors the first approach, as in general it's easier for translators and users in @@ -197,7 +197,7 @@ case of trouble. That's how we will be working here as well. ### Everyday usage In a common application, you would use some Gettext functions while writing static text in your pages. Those sentences would then appear in `.po` files, get translated, compiled into `.mo` files and then, used by Gettext when rendering -the actual interface. Given that, let's tie together what we have discussed so far in a a step-by-step example: +the actual interface. Given that, let's tie together what we have discussed so far in a step-by-step example: #### 1. A sample template file, including some different gettext calls {% highlight php %} @@ -212,14 +212,14 @@ the actual interface. Given that, let's tie together what we have discussed so f $unread), $unread)?> - +

{% endhighlight %} -- [`gettext()`][func] simply translates a `msgid` into it's corresponding `msgstr` for a given language. There's also +- [`gettext()`][func] simply translates a `msgid` into its corresponding `msgstr` for a given language. There's also the shorthand function `_()` that works the same way; - [`ngettext()`][n_func] does the same but with plural rules; - there's also [`dgettext()`][d_func] and [`dngettext()`][dn_func], that allows you to override the domain for a single @@ -248,7 +248,7 @@ if (isset($_GET['lang']) && valid($_GET['lang'])) { // if the cookie is present instead, let's just keep it $lang = $_COOKIE['lang']; //you should sanitize this! } elseif (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) { - // default resort: look for the languages the browser says the user accepts + // default: look for the languages the browser says the user accepts $langs = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']); array_walk($langs, function (&$lang) { $lang = strtr(strtok($lang, ';'), ['-' => '_']); }); foreach ($langs as $browser_lang) { @@ -284,42 +284,78 @@ textdomain('main'); {% endhighlight %} #### 3. Preparing translation for the first run -To make matters easier - and one of the powerful advantages Gettext has over custom framework i18n packages - is it's custom file type. "Oh man, that's quite hard to understand and edit by hand, a simple array would be easier!" Make no mistake, applications like [Poedit] are here to help - _a lot_. You can get the program from [their website], it's free and available for all platforms. It's a pretty easy tool to get used to, and a very powerful one at the same time - using with responsability the powers Gettext gave it. +To make matters easier - and one of the powerful advantages Gettext has over custom framework i18n packages - is its +custom file type. "Oh man, that's quite hard to understand and edit by hand, a simple array would be easier!" Make no +mistake, applications like [Poedit] are here to help - _a lot_. You can get the program from +[their website](poedit_download), it's free and available for all platforms. It's a pretty easy tool to get used to, +and a very powerful one at the same time - using all powerful features Gettext has available. -In the first run, you should select "File > New Catalog" from the menu. There you'll have a small screen where we will set the terrain so everything else runs smoothly. You'll be able to find those settings later through "Catalog > Properties": +In the first run, you should select "File > New Catalog" from the menu. There you'll have a small screen where we will +set the terrain so everything else runs smoothly. You'll be able to find those settings later through +"Catalog > Properties": - Project name and version, Translation Team and email address: useful information that goes in the `.po` file header; - Language: here you should use that format we mentioned before, such as `en_US` or `pt_BR`; - Charsets: UTF-8, preferably; -- Source charset: set here the charset used by your PHP files - probably UTF-8 AS well, right? +- Source charset: set here the charset used by your PHP files - probably UTF-8 as well, right? - plural forms: here goes those rules we mentioned before - there's a link in there with samples as well; -- Source paths: here you must include all folders from the project where `gettext()` (and siblings) will happen - this is usually your templates folder(s) -- Source keywords: this last part is filled by default, but you might need to alter it later - and is one of the powerful points of Gettext. The underlying software knows how the `gettext()` calls look like in several programming languages, but you might as well create your own translation forms. This will be discussed later in the "Tips" section. +- Source paths: here you must include all folders from the project where `gettext()` (and siblings) will happen - this +is usually your templates folder(s) +- Source keywords: this last part is filled by default, but you might need to alter it later - and is one of the +powerful points of Gettext. The underlying software knows how the `gettext()` calls look like in several programming +languages, but you might as well create your own translation forms. This will be discussed later in the "Tips" section. -After setting those points you'll be prompted to save the file - using that directory structure we mentioned as well, and then it will run a scan through your source files to find the localization calls. They'll be fed empty into the translation table, and you'll start typing in the localized versions of those strings. Save it and a `.mo` file will be (re)compiled into the same folder and ta-dah: your project is internationalized. +After setting those points you'll be prompted to save the file - using that directory structure we mentioned as well, +and then it will run a scan through your source files to find the localization calls. They'll be fed empty into the +translation table, and you'll start typing in the localized versions of those strings. Save it and a `.mo` file will be +(re)compiled into the same folder and ta-dah: your project is internationalized. #### 4. Translating strings -As you may have noticed before, there are two main types of localized strings: simple ones and the ones with plural forms. The first ones have simply two boxes: source and localized string. The source string can't be modified as Gettext/Poedit do not include the powers to alter your source files - you should change the source itself and rescan the files. Tip: you may right-click a translation line and it will hint you with the source files and lines where that string is being. -On the other hand, plural form strings include two boxes to show the two source strings, and tabs so you can configure the different final forms. +As you may have noticed before, there are two main types of localized strings: simple ones and the ones with plural +forms. The first ones have simply two boxes: source and localized string. The source string can't be modified as +Gettext/Poedit do not include the powers to alter your source files - you should change the source itself and rescan +the files. Tip: you may right-click a translation line and it will hint you with the source files and lines where that +string is being used. +On the other hand, plural form strings include two boxes to show the two source strings, and tabs so you can configure +the different final forms. -Whenever you change your sources and need to update the translations, just hit Refresh and Poedit will rescan the code, removing non-existent entries, merging the ones that changed and adding new ones. It may also try to guess some translations, based on other ones you did. Those guesses and the changed entries will receive a "Fuzzy" marker, indicating it needs review, being highlighted in the list. It's also useful if you have a translation team and someone tries to write something they're not sure about: just mark Fuzzy and someone else will review later. +Whenever you change your sources and need to update the translations, just hit Refresh and Poedit will rescan the code, +removing non-existent entries, merging the ones that changed and adding new ones. It may also try to guess some +translations, based on other ones you did. Those guesses and the changed entries will receive a "Fuzzy" marker, +indicating it needs review, being highlighted in the list. It's also useful if you have a translation team and someone +tries to write something they're not sure about: just mark Fuzzy and someone else will review later. -Finally, it's advised to leave "View > Untranslated entries first" marked, as it will help you _a lot_ to not forget any entry. From that menu you can also open parts of the UI that allow you to leave contextual information for translators, if needed. +Finally, it's advised to leave "View > Untranslated entries first" marked, as it will help you _a lot_ to not forget +any entry. From that menu you can also open parts of the UI that allow you to leave contextual information for +translators, if needed. ### Tips & Tricks #### Possible caching issues -If you're running PHP as a module on Apache (`mod_php`), you might face issues with the `.mo` file being cached. It happens the first time it's read, and then, to update it, you might need to restart the server. On Nginx and PHP5 it usually takes only a couple of page refreshes to refresh the translation cache, and on PHP7 it is rarely needed. +If you're running PHP as a module on Apache (`mod_php`), you might face issues with the `.mo` file being cached. It +happens the first time it's read, and then, to update it, you might need to restart the server. On Nginx and PHP5 it +usually takes only a couple of page refreshes to refresh the translation cache, and on PHP7 it is rarely needed. #### Additional helper functions -As preferred by many people, it's easier to use `_()` instead of `gettext()`. Many custom i18n libraries from frameworks use something similar to `t()` as well, to make translated code shorter. However, that's the only function that sports a shortcut. You might want to add in your project some others, such as `__()` or `_n()` for `ngettext()`, or maybe a fancy `_r()` that would join `gettext()` and `sprintf()` calls. +As preferred by many people, it's easier to use `_()` instead of `gettext()`. Many custom i18n libraries from +frameworks use something similar to `t()` as well, to make translated code shorter. However, that's the only function +that sports a shortcut. You might want to add in your project some others, such as `__()` or `_n()` for `ngettext()`, +or maybe a fancy `_r()` that would join `gettext()` and `sprintf()` calls. -In those cases, you'll need to instruct the Gettext utility on how to extract the strings from those new functions. Don't be afraid, it's something very easy. It's just a field in the `.po` file, or a Settings screen on Poedit - remember when we mentioned it before? In the editor that option is inside "Catalog > Properties > Source keywords". You need to include there the specifications of those new functions, following [a specific format](func_format): +In those cases, you'll need to instruct the Gettext utility on how to extract the strings from those new functions. +Don't be afraid, it's very easy. It's just a field in the `.po` file, or a Settings screen on Poedit. In the editor +that option is inside "Catalog > Properties > Source keywords". You need to include there the specifications of those +new functions, following [a specific format](func_format): -- if you create something like `t()` that simply returns the translation for a string, you can specify it as `t`. Gettext will know the only function argument is the string to be translated; -- if the function has more than one argument, you can specify in which one the first string is - and if needed, the plural form as well. For instance, if we call our function like this: `__('one user', '%d users', $number)`, the specification would be `__:1,2`, meaning the first form is the first argument, and the second form is the second argument. If your number comes as first argument instead, the spec would be `__:2,3`, indicating the first form is the second argument, and so on. +- if you create something like `t()` that simply returns the translation for a string, you can specify it as `t`. +Gettext will know the only function argument is the string to be translated; +- if the function has more than one argument, you can specify in which one the first string is - and if needed, the +plural form as well. For instance, if we call our function like this: `__('one user', '%d users', $number)`, the +specification would be `__:1,2`, meaning the first form is the first argument, and the second form is the second +argument. If your number comes as first argument instead, the spec would be `__:2,3`, indicating the first form is the +second argument, and so on. -After including those new rules in the `.po` file, a new scan will bring your new strings just as easy as before. +After including those new rules in the `.po` file, a new scan will bring in your new strings just as easy as before. ### References From d7f9db00808bf0132839334a38f4184a6471aa8c Mon Sep 17 00:00:00 2001 From: Igor Santos Date: Sat, 27 Feb 2016 14:51:28 -0300 Subject: [PATCH 06/33] Adding weeklies to resources page --- _posts/16-08-01-Sites.md | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/_posts/16-08-01-Sites.md b/_posts/16-08-01-Sites.md index a5189dc..7eaedca 100644 --- a/_posts/16-08-01-Sites.md +++ b/_posts/16-08-01-Sites.md @@ -17,6 +17,17 @@ PHP versions * [PHP Best Practices](https://phpbestpractices.org/) * [Best practices for Modern PHP Development](https://www.airpair.com/php/posts/best-practices-for-modern-php-development) +### News around the PHP and web development communities +You can subscribe to weekly newsletters to keep yourself informed on new libraries, latest news, events and general +announcements, as well as additional resources being published every now and then: + +* [PHP Weekly](http://www.phpweekly.com) +* [JavaScript Weekly](http://javascriptweekly.com) +* [HTML5 Weekly](http://html5weekly.com) +* [Mobile Web Weekly](http://mobilewebweekly.co) +* There are also Weeklies on other platforms you might be interested in; here's +[a list of some](https://github.com/jondot/awesome-weekly). + ### PHP universe * [PHP Developer blog](http://blog.phpdeveloper.org/) From a003a5b92603f1f58f5b54aa18a01e37a02c6184 Mon Sep 17 00:00:00 2001 From: Rodrigo Prado Date: Sat, 26 Mar 2016 09:02:17 -0300 Subject: [PATCH 07/33] added complementary word Brazil to pt translation --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index cae591c..d276a7c 100644 --- a/README.md +++ b/README.md @@ -45,7 +45,7 @@ developers know where to find good information! * [Korean](http://modernpug.github.io/php-the-right-way) * [Persian](http://novid.github.io/php-the-right-way/) * [Polish](http://pl.phptherightway.com) -* [Portuguese](http://br.phptherightway.com) +* [Portuguese (Brazil)](http://br.phptherightway.com) * [Romanian](https://bgui.github.io/php-the-right-way/) * [Russian](http://getjump.github.io/ru-php-the-right-way) * [Serbian](http://phpsrbija.github.io/php-the-right-way/) From 09a16031f4fabc49aba34e9a33ce8d0e6df4c371 Mon Sep 17 00:00:00 2001 From: Eric Poe Date: Fri, 3 Jun 2016 19:32:48 -0500 Subject: [PATCH 08/33] Fix capitalization for "PHP" and "PaaS" --- _posts/08-03-01-Plain-PHP-Templates.md | 1 + _posts/16-05-01-PHP-PaaS-Providers.md | 1 + 2 files changed, 2 insertions(+) diff --git a/_posts/08-03-01-Plain-PHP-Templates.md b/_posts/08-03-01-Plain-PHP-Templates.md index 7d08069..78bcf05 100644 --- a/_posts/08-03-01-Plain-PHP-Templates.md +++ b/_posts/08-03-01-Plain-PHP-Templates.md @@ -1,4 +1,5 @@ --- +title: Plain PHP Templates isChild: true anchor: plain_php_templates --- diff --git a/_posts/16-05-01-PHP-PaaS-Providers.md b/_posts/16-05-01-PHP-PaaS-Providers.md index 27a5e2a..57f3f57 100644 --- a/_posts/16-05-01-PHP-PaaS-Providers.md +++ b/_posts/16-05-01-PHP-PaaS-Providers.md @@ -1,4 +1,5 @@ --- +title: PHP PaaS Providers isChild: true anchor: php_paas_providers --- From c5ecf32775de9402e56f2123ca798fd2f8fbdd69 Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Delhommeau Date: Mon, 4 Jul 2016 13:36:59 +0200 Subject: [PATCH 09/33] style: Improve render of example code --- less/all.less | 5 +++++ styles/syntax.css | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/less/all.less b/less/all.less index 4ecc4c8..744e501 100644 --- a/less/all.less +++ b/less/all.less @@ -139,6 +139,11 @@ pre{ pre{ padding: 5px 10px; + white-space: pre-wrap; + white-space: -moz-pre-wrap; + white-space: -pre-wrap; + white-space: -o-pre-wrap; + word-wrap: break-word; } } diff --git a/styles/syntax.css b/styles/syntax.css index 1e651cf..f65ef4c 100644 --- a/styles/syntax.css +++ b/styles/syntax.css @@ -1,4 +1,4 @@ -.highlight { background: #ffffff; } +.highlight { background: #ffffff; margin: 0 4px; font-size: 0.8em; } .highlight .c { color: #999988; font-style: italic } /* Comment */ .highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */ .highlight .k { font-weight: bold } /* Keyword */ From 14e80864d583bfcc0bcefa7d11ac0c7a60aecc70 Mon Sep 17 00:00:00 2001 From: Myo T Kyaw Date: Fri, 16 Sep 2016 18:40:43 -0700 Subject: [PATCH 10/33] Update with minor grammar fixes --- _posts/01-04-01-Mac-Setup.md | 6 +++--- _posts/01-05-01-Windows-Setup.md | 4 ++-- _posts/03-03-01-Namespaces.md | 2 +- _posts/04-02-01-Composer-and-Packagist.md | 4 ++-- _posts/04-03-01-PEAR.md | 2 +- _posts/05-02-01-The-Basics.md | 2 +- _posts/05-05-01-PHP-and-UTF8.md | 2 +- _posts/06-03-01-Complex-Problem.md | 2 +- _posts/07-04-01-Interacting-via-Code.md | 2 +- _posts/10-03-01-Password-Hashing.md | 4 ++-- _posts/10-05-01-Configuration-Files.md | 2 +- _posts/12-04-01-Shared-Servers.md | 2 +- _posts/16-09-01-Videos.md | 2 +- _posts/17-02-01-User-Groups.md | 2 +- 14 files changed, 19 insertions(+), 19 deletions(-) diff --git a/_posts/01-04-01-Mac-Setup.md b/_posts/01-04-01-Mac-Setup.md index c496f25..16e015d 100644 --- a/_posts/01-04-01-Mac-Setup.md +++ b/_posts/01-04-01-Mac-Setup.md @@ -26,7 +26,7 @@ command-line, X11 or Aqua based open-source software on the OS X operating system. MacPorts supports pre-compiled binaries, so you don't need to recompile every -dependencies from the source tarball files, it saves your life if you don't +dependency from the source tarball files, it saves your life if you don't have any package installed on your system. At this point, you can install `php54`, `php55`, `php56` or `php70` using the `port install` command, for example: @@ -34,7 +34,7 @@ At this point, you can install `php54`, `php55`, `php56` or `php70` using the `p sudo port install php56 sudo port install php70 -And you can run `select` command to switch your active php: +And you can run `select` command to switch your active PHP: sudo port select --set php php70 @@ -46,7 +46,7 @@ applications/projects require different versions of PHP, and you are not using v ### Install PHP via Liip's binary installer Another popular option is [php-osx.liip.ch] which provides one liner installation methods for versions 5.3 through 7.0. -It doesn't overwrite the php binaries installed by Apple, but installs everything in a separate location (/usr/local/php5). +It doesn't overwrite the PHP binaries installed by Apple, but installs everything in a separate location (/usr/local/php5). ### Compile from Source diff --git a/_posts/01-05-01-Windows-Setup.md b/_posts/01-05-01-Windows-Setup.md index e916c91..1435ed7 100644 --- a/_posts/01-05-01-Windows-Setup.md +++ b/_posts/01-05-01-Windows-Setup.md @@ -7,13 +7,13 @@ anchor: windows_setup You can download the binaries from [windows.php.net/download][php-downloads]. After the extraction of PHP, it is recommended to set the [PATH][windows-path] to the root of your PHP folder (where php.exe is located) so you can execute PHP from anywhere. -For learning and local development you can use the built in webserver with PHP 5.4+ so you don't need to worry about +For learning and local development, you can use the built in webserver with PHP 5.4+ so you don't need to worry about configuring it. If you would like an "all-in-one" which includes a full-blown webserver and MySQL too then tools such as the [Web Platform Installer][wpi], [XAMPP][xampp], [EasyPHP][easyphp], [OpenServer][openserver] and [WAMP][wamp] will help get a Windows development environment up and running fast. That said, these tools will be a little different from production so be careful of environment differences if you are working on Windows and deploying to Linux. -If you need to run your production system on Windows then IIS7 will give you the most stable and best performance. You +If you need to run your production system on Windows, then IIS7 will give you the most stable and best performance. You can use [phpmanager][phpmanager] (a GUI plugin for IIS7) to make configuring and managing PHP simple. IIS7 comes with FastCGI built in and ready to go, you just need to configure PHP as a handler. For support and additional resources there is a [dedicated area on iis.net][php-iis] for PHP. diff --git a/_posts/03-03-01-Namespaces.md b/_posts/03-03-01-Namespaces.md index 55030aa..dc6084e 100644 --- a/_posts/03-03-01-Namespaces.md +++ b/_posts/03-03-01-Namespaces.md @@ -24,7 +24,7 @@ In October 2014 the PHP-FIG deprecated the previous autoloading standard: [PSR-0 many PHP 5.2-only projects currently implement PSR-0. Luckily those PHP 5.2-only projects are starting to up their version requirements, meaning PSR-0 is being used less and less. -If you're going to use an autoloader standard for a new application or package then you almost certainly want +If you're going to use an autoloader standard for a new application or package, then you almost certainly want to look into PSR-4. * [Read about Namespaces][namespaces] diff --git a/_posts/04-02-01-Composer-and-Packagist.md b/_posts/04-02-01-Composer-and-Packagist.md index 553e57f..10e1f6f 100644 --- a/_posts/04-02-01-Composer-and-Packagist.md +++ b/_posts/04-02-01-Composer-and-Packagist.md @@ -79,7 +79,7 @@ as a dependency of your project. composer require twig/twig:~1.8 {% endhighlight %} -Alternatively the `composer init` command will guide you through creating a full `composer.json` file +Alternatively, the `composer init` command will guide you through creating a full `composer.json` file for your project. Either way, once you've created your `composer.json` file you can tell Composer to download and install your dependencies into the `vendor/` directory. This also applies to projects you've downloaded that already provide a `composer.json` file: @@ -106,7 +106,7 @@ first ran `composer install`. If you share your project with other coders and th is part of your distribution, when they run `composer install` they'll get the same versions as you. To update your dependencies, run `composer update`. -This is most useful when you define your version requirements flexibly. For instance a version +This is most useful when you define your version requirements flexibly. For instance, a version requirement of `~1.8` means "anything newer than `1.8.0`, but less than `2.0.x-dev`". You can also use the `*` wildcard as in `1.8.*`. Now Composer's `composer update` command will upgrade all your dependencies to the newest version that fits the restrictions you define. diff --git a/_posts/04-03-01-PEAR.md b/_posts/04-03-01-PEAR.md index 3c9af2a..997586d 100644 --- a/_posts/04-03-01-PEAR.md +++ b/_posts/04-03-01-PEAR.md @@ -57,7 +57,7 @@ handle your PEAR dependencies. This example will install code from `pear2.php.ne {% endhighlight %} The first section `"repositories"` will be used to let Composer know it should "initialize" (or "discover" in PEAR -terminology) the pear repo. Then the require section will prefix the package name like this: +terminology) the pear repo. Then the required section will prefix the package name like this: > pear-channel/Package diff --git a/_posts/05-02-01-The-Basics.md b/_posts/05-02-01-The-Basics.md index 458c670..dd5fc28 100644 --- a/_posts/05-02-01-The-Basics.md +++ b/_posts/05-02-01-The-Basics.md @@ -6,7 +6,7 @@ anchor: the_basics ## The Basics {#the_basics_title} PHP is a vast language that allows coders of all levels the ability to produce code not only quickly, but efficiently. -However while advancing through the language, we often forget the basics that we first learnt (or overlooked) in favor +However, while advancing through the language, we often forget the basics that we first learnt (or overlooked) in favor of short cuts and/or bad habits. To help combat this common issue, this section is aimed at reminding coders of the basic coding practices within PHP. diff --git a/_posts/05-05-01-PHP-and-UTF8.md b/_posts/05-05-01-PHP-and-UTF8.md index de0d659..b734d3b 100644 --- a/_posts/05-05-01-PHP-and-UTF8.md +++ b/_posts/05-05-01-PHP-and-UTF8.md @@ -18,7 +18,7 @@ for a brief, practical summary. ### UTF-8 at the PHP level The basic string operations, like concatenating two strings and assigning strings to variables, don't need anything -special for UTF-8. However most string functions, like `strpos()` and `strlen()`, do need special consideration. These +special for UTF-8. However, most string functions, like `strpos()` and `strlen()`, do need special consideration. These functions often have an `mb_*` counterpart: for example, `mb_strpos()` and `mb_strlen()`. These `mb_*` strings are made available to you via the [Multibyte String Extension], and are specifically designed to operate on Unicode strings. diff --git a/_posts/06-03-01-Complex-Problem.md b/_posts/06-03-01-Complex-Problem.md index 0d6f907..c64e723 100644 --- a/_posts/06-03-01-Complex-Problem.md +++ b/_posts/06-03-01-Complex-Problem.md @@ -10,7 +10,7 @@ If you have ever read about Dependency Injection then you have probably seen the ### Inversion of Control -Inversion of Control is as it says, "inverting the control" of a system by keeping organisational control entirely +Inversion of Control is as it says, "inverting the control" of a system by keeping organizational control entirely separate from our objects. In terms of Dependency Injection, this means loosening our dependencies by controlling and instantiating them elsewhere in the system. diff --git a/_posts/07-04-01-Interacting-via-Code.md b/_posts/07-04-01-Interacting-via-Code.md index 78d4f4b..5cae4f0 100644 --- a/_posts/07-04-01-Interacting-via-Code.md +++ b/_posts/07-04-01-Interacting-via-Code.md @@ -19,7 +19,7 @@ foreach ($db->query('SELECT * FROM table') as $row) { {% endhighlight %} -This is bad practice for all sorts of reasons, mainly that its hard to debug, hard to test, hard to read and it is +This is bad practice for all sorts of reasons, mainly that it's hard to debug, hard to test, hard to read and it is going to output a lot of fields if you don't put a limit on there. While there are many other solutions to doing this - depending on if you prefer [OOP](/#object-oriented-programming) or diff --git a/_posts/10-03-01-Password-Hashing.md b/_posts/10-03-01-Password-Hashing.md index ee70a65..d1694e6 100644 --- a/_posts/10-03-01-Password-Hashing.md +++ b/_posts/10-03-01-Password-Hashing.md @@ -8,8 +8,8 @@ anchor: password_hashing Eventually everyone builds a PHP application that relies on user login. Usernames and passwords are stored in a database and later used to authenticate users upon login. -It is important that you properly [_hash_][3] passwords before storing them. Password hashing is an irreversible, one -way function performed against the user's password. This produces a fixed-length string that cannot be feasibly +It is important that you properly [_hash_][3] passwords before storing them. Password hashing is an irreversible, +one-way function performed against the user's password. This produces a fixed-length string that cannot be feasibly reversed. This means you can compare a hash against another to determine if they both came from the same source string, but you cannot determine the original string. If passwords are not hashed and your database is accessed by an unauthorized third-party, all user accounts are now compromised. Some users may (unfortunately) use the same password diff --git a/_posts/10-05-01-Configuration-Files.md b/_posts/10-05-01-Configuration-Files.md index afb7c0b..2e30846 100644 --- a/_posts/10-05-01-Configuration-Files.md +++ b/_posts/10-05-01-Configuration-Files.md @@ -14,4 +14,4 @@ via the file system. that, even if the script is accessed directly, it will not be output as plain text. - Information in configuration files should be protected accordingly, either through encryption or group/user file system permissions. -- It is a good idea to ensure that you do not commit configuration files containing sensitive information eg passwords or API tokens to source control. +- It is a good idea to ensure that you do not commit configuration files containing sensitive information e.g. passwords or API tokens to source control. diff --git a/_posts/12-04-01-Shared-Servers.md b/_posts/12-04-01-Shared-Servers.md index f33127a..828eb4f 100644 --- a/_posts/12-04-01-Shared-Servers.md +++ b/_posts/12-04-01-Shared-Servers.md @@ -9,6 +9,6 @@ PHP has shared servers to thank for its popularity. It is hard to find a host wi the latest version. Shared servers allow you and other developers to deploy websites to a single machine. The upside to this is that it has become a cheap commodity. The downside is that you never know what kind of a ruckus your neighboring tenants are going to create; loading down the server or opening up security holes are the main concerns. If -your project's budget can afford to avoid shared servers you should. +your project's budget can afford to avoid shared servers, you should. To make sure your shared servers are offering the latest versions of PHP, check out [PHP Versions](http://phpversions.info/shared-hosting/). diff --git a/_posts/16-09-01-Videos.md b/_posts/16-09-01-Videos.md index 35e961b..7cdbd6f 100644 --- a/_posts/16-09-01-Videos.md +++ b/_posts/16-09-01-Videos.md @@ -6,7 +6,7 @@ title: Video Tutorials ## Video Tutorials {#videos} -### Youtube Channels +### YouTube Channels * [PHP Academy](https://www.youtube.com/user/phpacademy) * [The New Boston](https://www.youtube.com/user/thenewboston) * [Sherif Ramadan](https://www.youtube.com/user/businessgeek) diff --git a/_posts/17-02-01-User-Groups.md b/_posts/17-02-01-User-Groups.md index 97c062b..7fcd668 100644 --- a/_posts/17-02-01-User-Groups.md +++ b/_posts/17-02-01-User-Groups.md @@ -7,7 +7,7 @@ anchor: user_groups If you live in a larger city, odds are there's a PHP user group nearby. You can easily find your local PUG at the [usergroup-list at php.net][php-uglist] which is based upon [PHP.ug][php-ug]. Alternate sources might be -[Meetup.com][meetup] or a search for ```php user group near me``` using your favourite search engine +[Meetup.com][meetup] or a search for ```php user group near me``` using your favorite search engine (i.e. [Google][google]). If you live in a smaller town, there may not be a local PUG; if that's the case, start one! Special mention should be made of two global user groups: [NomadPHP] and [PHPWomen]. [NomadPHP] offers twice monthly From c672506e4fa395496a2db77e873958ba8e2578ee Mon Sep 17 00:00:00 2001 From: Myo T Kyaw Date: Sat, 17 Sep 2016 10:06:51 -0700 Subject: [PATCH 11/33] Correct wrong fix for _posts/04-03-01-PEAR.md:60 --- _posts/04-03-01-PEAR.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_posts/04-03-01-PEAR.md b/_posts/04-03-01-PEAR.md index 997586d..3c9af2a 100644 --- a/_posts/04-03-01-PEAR.md +++ b/_posts/04-03-01-PEAR.md @@ -57,7 +57,7 @@ handle your PEAR dependencies. This example will install code from `pear2.php.ne {% endhighlight %} The first section `"repositories"` will be used to let Composer know it should "initialize" (or "discover" in PEAR -terminology) the pear repo. Then the required section will prefix the package name like this: +terminology) the pear repo. Then the require section will prefix the package name like this: > pear-channel/Package From 246d08a5389f8f195601d87f87891f307f906a45 Mon Sep 17 00:00:00 2001 From: Myo T Kyaw Date: Sat, 17 Sep 2016 23:20:10 -0700 Subject: [PATCH 12/33] Put backticks for 'require' in _posts/04-03-01-PEAR.md:60 --- _posts/04-03-01-PEAR.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_posts/04-03-01-PEAR.md b/_posts/04-03-01-PEAR.md index 3c9af2a..f61a3e6 100644 --- a/_posts/04-03-01-PEAR.md +++ b/_posts/04-03-01-PEAR.md @@ -57,7 +57,7 @@ handle your PEAR dependencies. This example will install code from `pear2.php.ne {% endhighlight %} The first section `"repositories"` will be used to let Composer know it should "initialize" (or "discover" in PEAR -terminology) the pear repo. Then the require section will prefix the package name like this: +terminology) the pear repo. Then the `require` section will prefix the package name like this: > pear-channel/Package From 89e02f7ce7a12c718334e39092e8af8c09ab55da Mon Sep 17 00:00:00 2001 From: William Turrell Date: Thu, 6 Oct 2016 10:59:36 +0100 Subject: [PATCH 13/33] Password Hashing - explain salts - what they are - why they're needed - how password_hash/verify handle it all for you. --- _posts/10-03-01-Password-Hashing.md | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/_posts/10-03-01-Password-Hashing.md b/_posts/10-03-01-Password-Hashing.md index ee70a65..5711b54 100644 --- a/_posts/10-03-01-Password-Hashing.md +++ b/_posts/10-03-01-Password-Hashing.md @@ -12,8 +12,13 @@ It is important that you properly [_hash_][3] passwords before storing them. Pas way function performed against the user's password. This produces a fixed-length string that cannot be feasibly reversed. This means you can compare a hash against another to determine if they both came from the same source string, but you cannot determine the original string. If passwords are not hashed and your database is accessed by an -unauthorized third-party, all user accounts are now compromised. Some users may (unfortunately) use the same password -for other services. Therefore, it is important to take security seriously. +unauthorized third-party, all user accounts are now compromised. + +Passwords should also be individually [_salted_][5] by adding a random string to each password before hashing. This prevents dictionary attacks and the use of 'rainbow tables' (a reverse list of crytographic hashes for common passwords.) + +Hashing and salting are vital as often users use the same password for multiple services and password quality can be poor. + +Fortunately, nowadays PHP makes this easy. **Hashing passwords with `password_hash`** @@ -37,10 +42,12 @@ if (password_verify('bad-password', $passwordHash)) { } {% endhighlight %} +password_hash() takes care of password salting for you. The salt is stored, along with the algorithm and "cost", as part of the hash. password_verify() extracts this to determine how to check the password, so you don't need a separate database field to store your salts. * [Learn about `password_hash()`] [1] * [`password_compat` for PHP >= 5.3.7 && < 5.5] [2] * [Learn about hashing in regards to cryptography] [3] +* [Learn about salts] [5] * [PHP `password_hash()` RFC] [4] @@ -48,3 +55,4 @@ if (password_verify('bad-password', $passwordHash)) { [2]: https://github.com/ircmaxell/password_compat [3]: http://en.wikipedia.org/wiki/Cryptographic_hash_function [4]: https://wiki.php.net/rfc/password_hash +[5]: https://en.wikipedia.org/wiki/Salt_(cryptography) From 646aae60a9f3b619c12624a573780fcf6f5e1c7d Mon Sep 17 00:00:00 2001 From: William Turrell Date: Sat, 8 Oct 2016 12:18:17 +0100 Subject: [PATCH 14/33] Conform to style guide Backticks for functions, double (") not single (') quotes --- _posts/10-03-01-Password-Hashing.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/_posts/10-03-01-Password-Hashing.md b/_posts/10-03-01-Password-Hashing.md index 5711b54..1e4019d 100644 --- a/_posts/10-03-01-Password-Hashing.md +++ b/_posts/10-03-01-Password-Hashing.md @@ -14,7 +14,7 @@ reversed. This means you can compare a hash against another to determine if they but you cannot determine the original string. If passwords are not hashed and your database is accessed by an unauthorized third-party, all user accounts are now compromised. -Passwords should also be individually [_salted_][5] by adding a random string to each password before hashing. This prevents dictionary attacks and the use of 'rainbow tables' (a reverse list of crytographic hashes for common passwords.) +Passwords should also be individually [_salted_][5] by adding a random string to each password before hashing. This prevents dictionary attacks and the use of "rainbow tables" (a reverse list of crytographic hashes for common passwords.) Hashing and salting are vital as often users use the same password for multiple services and password quality can be poor. @@ -42,7 +42,7 @@ if (password_verify('bad-password', $passwordHash)) { } {% endhighlight %} -password_hash() takes care of password salting for you. The salt is stored, along with the algorithm and "cost", as part of the hash. password_verify() extracts this to determine how to check the password, so you don't need a separate database field to store your salts. +`password_hash()` takes care of password salting for you. The salt is stored, along with the algorithm and "cost", as part of the hash. `password_verify()` extracts this to determine how to check the password, so you don't need a separate database field to store your salts. * [Learn about `password_hash()`] [1] * [`password_compat` for PHP >= 5.3.7 && < 5.5] [2] From 24dfa328ecaac52977e9bf5af419ee8f1063a04c Mon Sep 17 00:00:00 2001 From: William Turrell Date: Sun, 9 Oct 2016 12:02:33 +0100 Subject: [PATCH 15/33] Improve Docker explanation - More of an overview / how it works - Explain in which circumstances it might be useful - Simplify the example container - Mention PHPDocker.io - Two links to Docker Hub, one with the official images - brief security warning --- _posts/13-03-01-Docker.md | 31 +++++++++++++------------------ 1 file changed, 13 insertions(+), 18 deletions(-) diff --git a/_posts/13-03-01-Docker.md b/_posts/13-03-01-Docker.md index b013dc0..3d1a1e7 100644 --- a/_posts/13-03-01-Docker.md +++ b/_posts/13-03-01-Docker.md @@ -5,41 +5,36 @@ anchor: docker ## Docker {#docker_title} -Beside using Vagrant, another easy way to get a virtual development or production environment up and running is [Docker]. -Docker helps you to provide Linux containers for all kind of applications. -There are many helpful docker images which could provide you with other great services without the need to install -these services on your local machine, e.g. MySQL or PostgreSQL and a lot more. Have a look at the [Docker Hub Registry] -[docker-hub] to search a list of available pre-built containers, which you can then run and use in very few steps. +[Docker] is so called because it's all about containers. It's a lightweight alternative to a fully-fledged Vagrant VM. PHP's OOP is perhaps a helpful analogy to understanding it: think of Docker "images" as classes and "containers" as instances of them. For a typical LAMP application, you might have a web server container, another for the PHP-FPM process and a third for the MySQL server. Your project files - PHP, HTML, other assets, database contents – all remain on your host computer. You can create containers from the command line, or build a `docker-compose.yml` file that specifies which to use and how they communicate ("link") with one another. + +Docker may help if you're developing multiple websites and want the separation that comes from installing each on it's on virtual machine, but don't have the necessary disk space or the time to keep everything up to date. It's efficient: the installation and downloads are quicker, you only need to store one copy of each image however often it's used, containers need less RAM and share the same OS kernel, so you can have more servers running simultaneously, and it takes a matter of seconds to stop and start them, no need to wait for a full server boot. ### Example: Running your PHP Applications in Docker -After you [installed docker][docker-install] on your machine, you can start an Apache with PHP support in one step. -The following command will download a fully functional Apache installation with the latest PHP version and provide the -directory `/path/to/your/php/files` at `http://localhost:8080`: +After [installing docker][docker-install] on your machine, you can start a web server with one command. +The following will download a fully functional Apache installation with the latest PHP version, map `/path/to/your/php/files` to the document root, which you can view at `http://localhost:8080`: {% highlight console %} docker run -d --name my-php-webserver -p 8080:80 -v /path/to/your/php/files:/var/www/html/ php:apache {% endhighlight %} -After running `docker run` your container is initialized and running. -If you would like to stop or start your container again, you can use the provided name attribute and simply run -`docker stop my-php-webserver` and `docker start my-php-webserver` without providing the above mentioned parameters -again. +This will initialize and launch your container. `-d` makes it runs in the background. To stop and start it, simply run `docker stop my-php-webserver` and `docker start my-php-webserver` (the other parameters are not needed again). ### Learn more about Docker -The commands mentioned above only show a quick way to run an Apache web server with PHP support but there are a lot -more things that you can do with Docker. One of the most important things for PHP developers will be linking your -web server to a database instance, for example. How this could be done is well described within the [Docker User Guide] -[docker-doc]. +The command above shows a quick way to run a basic server. There's much more you can do (and thousands of pre-built images in the [Docker Hub][docker-hub].) Take time to learn the terminology and read the [Docker User Guide][docker-doc] to get the most from it, and don't run random code you've downloaded without checking it's safe – unofficial images may not have the latest security patches. If in doubt, stick to the [official repositiories][docker-hub-official]. + +The [PHPDocker.io] site will auto-generate all the files you need for a fully-featured LAMP/LEMP stack, including your choice of PHP version and extensions. * [Docker Website][Docker] * [Docker Installation][docker-install] -* [Docker Images at the Docker Hub Registry][docker-hub] * [Docker User Guide][docker-doc] - +* [Docker Hub][docker-hub] +* [Docker Hub - official images][docker-hub-official] [Docker]: http://docker.com/ [docker-hub]: https://hub.docker.com/ +[docker-hub-official]: https://hub.docker.com/explore/ [docker-install]: https://docs.docker.com/installation/ [docker-doc]: https://docs.docker.com/userguide/ +[PHPDocker.io]: https://phpdocker.io/generator From 3ef02dca0d52f4deb5d2dd8780fc6eed6ca3fcac Mon Sep 17 00:00:00 2001 From: William Turrell Date: Mon, 10 Oct 2016 18:53:08 +0100 Subject: [PATCH 16/33] Fix typos --- _posts/13-03-01-Docker.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/_posts/13-03-01-Docker.md b/_posts/13-03-01-Docker.md index 3d1a1e7..3f4f8de 100644 --- a/_posts/13-03-01-Docker.md +++ b/_posts/13-03-01-Docker.md @@ -7,7 +7,7 @@ anchor: docker [Docker] is so called because it's all about containers. It's a lightweight alternative to a fully-fledged Vagrant VM. PHP's OOP is perhaps a helpful analogy to understanding it: think of Docker "images" as classes and "containers" as instances of them. For a typical LAMP application, you might have a web server container, another for the PHP-FPM process and a third for the MySQL server. Your project files - PHP, HTML, other assets, database contents – all remain on your host computer. You can create containers from the command line, or build a `docker-compose.yml` file that specifies which to use and how they communicate ("link") with one another. -Docker may help if you're developing multiple websites and want the separation that comes from installing each on it's on virtual machine, but don't have the necessary disk space or the time to keep everything up to date. It's efficient: the installation and downloads are quicker, you only need to store one copy of each image however often it's used, containers need less RAM and share the same OS kernel, so you can have more servers running simultaneously, and it takes a matter of seconds to stop and start them, no need to wait for a full server boot. +Docker may help if you're developing multiple websites and want the separation that comes from installing each on it's own virtual machine, but don't have the necessary disk space or the time to keep everything up to date. It's efficient: the installation and downloads are quicker, you only need to store one copy of each image however often it's used, containers need less RAM and share the same OS kernel, so you can have more servers running simultaneously, and it takes a matter of seconds to stop and start them, no need to wait for a full server boot. ### Example: Running your PHP Applications in Docker @@ -22,7 +22,7 @@ This will initialize and launch your container. `-d` makes it runs in the backgr ### Learn more about Docker -The command above shows a quick way to run a basic server. There's much more you can do (and thousands of pre-built images in the [Docker Hub][docker-hub].) Take time to learn the terminology and read the [Docker User Guide][docker-doc] to get the most from it, and don't run random code you've downloaded without checking it's safe – unofficial images may not have the latest security patches. If in doubt, stick to the [official repositiories][docker-hub-official]. +The command above shows a quick way to run a basic server. There's much more you can do (and thousands of pre-built images in the [Docker Hub][docker-hub]). Take time to learn the terminology and read the [Docker User Guide][docker-doc] to get the most from it, and don't run random code you've downloaded without checking it's safe – unofficial images may not have the latest security patches. If in doubt, stick to the [official repositiories][docker-hub-official]. The [PHPDocker.io] site will auto-generate all the files you need for a fully-featured LAMP/LEMP stack, including your choice of PHP version and extensions. From f6c3087e36adaea1c592d5b4022f08c84930635e Mon Sep 17 00:00:00 2001 From: William Turrell Date: Mon, 10 Oct 2016 23:26:05 +0100 Subject: [PATCH 17/33] Rewrite introductory paragraph MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit I may have made it more complicated than before… Might be case of deciding what to leave out (e.g. I've tried to explain distinction between images and containers and give a simple example where containers only do one thing – I was thoroughly confused when first searching the Docker Hub…) --- _posts/13-03-01-Docker.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/_posts/13-03-01-Docker.md b/_posts/13-03-01-Docker.md index 3f4f8de..7292556 100644 --- a/_posts/13-03-01-Docker.md +++ b/_posts/13-03-01-Docker.md @@ -5,7 +5,11 @@ anchor: docker ## Docker {#docker_title} -[Docker] is so called because it's all about containers. It's a lightweight alternative to a fully-fledged Vagrant VM. PHP's OOP is perhaps a helpful analogy to understanding it: think of Docker "images" as classes and "containers" as instances of them. For a typical LAMP application, you might have a web server container, another for the PHP-FPM process and a third for the MySQL server. Your project files - PHP, HTML, other assets, database contents – all remain on your host computer. You can create containers from the command line, or build a `docker-compose.yml` file that specifies which to use and how they communicate ("link") with one another. +[Docker] - a lightweight alternative to a full virtual machine - is so called because it's all about "containers". A container is a building block which, in the simplest case, does one specific job, e.g. running a web server. An "image" is the package you use to build the container - Docker has a repository full of them. + +A typical LAMP application might have three containers: a web server, a PHP-FPM process and MySQL. As with shared folders in Vagrant, you can leave your application files where they are and tell Docker where to find them. + +You can generate containers from the command line (see example below) or, for ease of maintenance, build a `docker-compose.yml` file for your project specifying which to create and how they communicate with one another. Docker may help if you're developing multiple websites and want the separation that comes from installing each on it's own virtual machine, but don't have the necessary disk space or the time to keep everything up to date. It's efficient: the installation and downloads are quicker, you only need to store one copy of each image however often it's used, containers need less RAM and share the same OS kernel, so you can have more servers running simultaneously, and it takes a matter of seconds to stop and start them, no need to wait for a full server boot. From f5050d987b14e1ce38329854c295dc2806c71cb9 Mon Sep 17 00:00:00 2001 From: adaroobi Date: Sun, 23 Oct 2016 20:51:11 +0300 Subject: [PATCH 18/33] Updated Readme: updated Portuguese title and added Arabic link. --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index cae591c..33355ce 100644 --- a/README.md +++ b/README.md @@ -45,7 +45,7 @@ developers know where to find good information! * [Korean](http://modernpug.github.io/php-the-right-way) * [Persian](http://novid.github.io/php-the-right-way/) * [Polish](http://pl.phptherightway.com) -* [Portuguese](http://br.phptherightway.com) +* [Portuguese (Brazil)](http://br.phptherightway.com/) * [Romanian](https://bgui.github.io/php-the-right-way/) * [Russian](http://getjump.github.io/ru-php-the-right-way) * [Serbian](http://phpsrbija.github.io/php-the-right-way/) @@ -54,6 +54,7 @@ developers know where to find good information! * [Thai](https://apzentral.github.io/php-the-right-way/) * [Turkish](http://hkulekci.github.io/php-the-right-way/) * [Ukrainian](http://iflista.github.com/php-the-right-way) +* [العربية](https://adaroobi.github.io/php-the-right-way/) ### Translations From 580ba62b76ef06538d0a99f00b971c0bd24e2ac8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Borges?= Date: Mon, 24 Oct 2016 14:59:10 +0100 Subject: [PATCH 19/33] Corrected Portuguese Brazil. --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index cae591c..d276a7c 100644 --- a/README.md +++ b/README.md @@ -45,7 +45,7 @@ developers know where to find good information! * [Korean](http://modernpug.github.io/php-the-right-way) * [Persian](http://novid.github.io/php-the-right-way/) * [Polish](http://pl.phptherightway.com) -* [Portuguese](http://br.phptherightway.com) +* [Portuguese (Brazil)](http://br.phptherightway.com) * [Romanian](https://bgui.github.io/php-the-right-way/) * [Russian](http://getjump.github.io/ru-php-the-right-way) * [Serbian](http://phpsrbija.github.io/php-the-right-way/) From 60c2720becf3e67c6150914015bf97640ea64d4c Mon Sep 17 00:00:00 2001 From: William Turrell Date: Fri, 28 Oct 2016 11:10:36 +0100 Subject: [PATCH 20/33] Warn about composer update on production Also, greater encouragement to check composer.lock is staged in repositories. Rephrased for (imho) improved clarity. --- _posts/04-02-01-Composer-and-Packagist.md | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/_posts/04-02-01-Composer-and-Packagist.md b/_posts/04-02-01-Composer-and-Packagist.md index d580acb..d396772 100644 --- a/_posts/04-02-01-Composer-and-Packagist.md +++ b/_posts/04-02-01-Composer-and-Packagist.md @@ -100,10 +100,11 @@ Now you can use your project dependencies, and they'll be autoloaded on demand. ### Updating your dependencies Composer creates a file called `composer.lock` which stores the exact version of each package it -downloaded when you -first ran `composer install`. If you share your project with other coders and the `composer.lock` file -is part of your distribution, when they run `composer install` they'll get the same versions as you. -To update your dependencies, run `composer update`. +downloaded when you first ran `composer install`. If you share your project with others, +ensure the `composer.lock` file is included, so that when they run `composer install` they'll +get the same versions as you. To update your dependencies, run `composer update`. Don't use +`composer update` when deploying, only `composer install`, otherwise you may end up with different +package versions on production. This is most useful when you define your version requirements flexibly. For instance a version requirement of `~1.8` means "anything newer than `1.8.0`, but less than `2.0.x-dev`". You can also use From 247e1b69c919595f9b093da3e465784c74a72a8a Mon Sep 17 00:00:00 2001 From: Daniel Holmes Date: Wed, 9 Nov 2016 20:23:08 -0600 Subject: [PATCH 21/33] Updated information for running Apache and PHP-FPM Includes an updated link for mod_fastcgi--as it is still useful on older Apache versions--as the project host no longer hosts the documentation. However, in Apache 2.4, mod_proxy_fcgi makes this quick work, so referencing it and a clean tutorial as well. --- _posts/12-03-01-Virtual-or-Dedicated-Servers.md | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/_posts/12-03-01-Virtual-or-Dedicated-Servers.md b/_posts/12-03-01-Virtual-or-Dedicated-Servers.md index 1780ecf..73e47e2 100644 --- a/_posts/12-03-01-Virtual-or-Dedicated-Servers.md +++ b/_posts/12-03-01-Virtual-or-Dedicated-Servers.md @@ -35,10 +35,14 @@ Alternatively, if you want to squeeze more performance and stability out of Apac same FPM system as nginx and run the [worker MPM] or [event MPM] with mod_fastcgi or mod_fcgid. This configuration will be significantly more memory efficient and much faster but it is more work to set up. +If you are running Apache 2.4 or later, you can use [mod_proxy_fcgi] to get great performance that is easy to setup. + * [Read more on Apache][apache] * [Read more on Multi-Processing Modules][apache-MPM] * [Read more on mod_fastcgi][mod_fastcgi] * [Read more on mod_fcgid][mod_fcgid] +* [Read more on mod_proxy_fcgi][mod_proxy_fcgi] +* [Read more on setting up Apache and PHP-FPM with mod_proxy_fcgi][tutorial-mod_proxy_fcgi] [nginx]: http://nginx.org/ @@ -50,5 +54,7 @@ be significantly more memory efficient and much faster but it is more work to se [event MPM]: http://httpd.apache.org/docs/2.4/mod/event.html [apache]: http://httpd.apache.org/ [apache-MPM]: http://httpd.apache.org/docs/2.4/mod/mpm_common.html -[mod_fastcgi]: http://www.fastcgi.com/mod_fastcgi/docs/mod_fastcgi.html +[mod_fastcgi]: https://blogs.oracle.com/opal/entry/php_fpm_fastcgi_process_manager [mod_fcgid]: http://httpd.apache.org/mod_fcgid/ +[mod_proxy_fcgi]: https://httpd.apache.org/docs/current/mod/mod_proxy_fcgi.html +[tutorial-mod_proxy_fcgi]: https://serversforhackers.com/video/apache-and-php-fpm From ade9c6e5714765065a8cbe97734ab9d18fa491bf Mon Sep 17 00:00:00 2001 From: Igor Santos Date: Sun, 13 Nov 2016 18:17:47 -0200 Subject: [PATCH 22/33] Adding information on other i18n libraries Cleaning up framework references, as they're listed below --- ...1-Internationalization-and-Localization.md | 70 ++++++++++++++----- 1 file changed, 51 insertions(+), 19 deletions(-) diff --git a/_posts/05-06-01-Internationalization-and-Localization.md b/_posts/05-06-01-Internationalization-and-Localization.md index 0307942..2cc26ff 100644 --- a/_posts/05-06-01-Internationalization-and-Localization.md +++ b/_posts/05-06-01-Internationalization-and-Localization.md @@ -29,17 +29,36 @@ The easiest way to internationalize PHP software is by using array files and usi some maintenance issues along the road - some might appear in the very beginning, such as pluralization. So, please, don't try this if your project will contain more than a couple of pages. -Some frameworks will sport their own i18n packages. Those usually are a more powerful version of the above approach, -but including features needed for real localization, such as plural forms and string replacement. You're free to use -those if you feel like, but you might find bothering to edit array source files, having to deal with pure code issues -(such as string scaping and so on). The main pro here is integration with the environment you're using - the framework -is called _full-stack_ for a reason, right? +The most classic way and often taken as reference for i18n and l10n is a [Unix tool called `gettext`][gettext]. It dates +back to 1995 and is still a complete implementation for translating software. It is pretty easy to get running, while +it still sports powerful supporting tools. It's about Gettext we will be talking here. Also, to help you not get messy +over the command-line, we will be presenting a great GUI application that can be used to easily update your l10n source +files. -However, the most classic way and often taken as reference for i18n and l10n is a [Unix tool called `gettext`][gettext]. -It dates back to 1995 and is still the most complete implementation for translating software. It is pretty easy to get -running, while it still sports powerful supporting tools. It's about Gettext we will be talking here. Also, to help you -not get messy over the command-line, we will be presenting a great GUI application that can be used to easily update -your l10n source files. +### Other tools + +There are common libraries used that support Gettext, and other implementations of i18n. Some of the may seem easier to +install, or sport additional features or i18n file formats. In this document we focus on the tools provided with the +PHP core, but here we list others for completion: + +- [oscarotero/Gettext][oscarotero]: Gettext support with an OO interface; includes improved helper functions, powerful +extractors for several file formats (some of them not supported natively by the `gettext` command), and can also export +to other formats besides `.mo/.po` files. Can be useful if you need to integrate your translation files into other parts +of the system, like a JavaScript interface. +- [symfony/translation][symfony]: supports a lot of different formats, but recommends using verbose XLIFF's. Doesn't +include helper functions nor a built-in extractor, but supports placeholders using `strtr()` internally. +- [zend/i18n][zend]: supports array and INI files, or Gettext formats. Implements a caching layer to save you from +reading the filesystem every time. Also includes view helpers, and locale-aware input filters and validators. However, +it has no message extractor. + +Other frameworks also include i18n modules, but those are not available outside of their codebases: +- [Laravel] supports basic array files, has no automatic extractor but includes a `@lang` helper for template files. +- [Yii] supports array, Gettext and database-based translation, and includes a messages extractor. It is backed by the +[`Intl`][intl] extension, available since PHP 5.3, and based on the [ICU project]; this enables Yii to run powerful +replacements, like spelling out numbers, formatting dates, times, intervals, currency and ordinals. + +If you decide to go for one of the libraries that provide no extractors, you may want to use the gettext formats, so +you can use the original gettext toolchain (including Poedit) as described in the rest of the chapter. ## Gettext @@ -66,7 +85,8 @@ You'll always have one pair of PO/MO files per language and region, but only one There are some cases, in big projects, where you might need to separate translations when the same words convey different meaning given a context. In those cases you split them into different _domains_. They're basically named groups of POT/PO/MO files, where the filename is the said _translation domain_. Small and medium-sized projects usually, -for simplicity, use only one domain; its name is arbitrary, but we will be using "main" for our code samples. +for simplicity, use only one domain; its name is arbitrary, but we will be using "main" for our code samples. +In [Symfony] projects, for example, domains are used to separate the translation for validation messages. #### Locale code A locale is simple code that identifies a version of a language. It's defined following [ISO 639-1][639-1] and @@ -181,18 +201,21 @@ Talking about translation keys, there are two main "schools" here: - The only disadvantage: if you need to change the actual text, you would need to replace the same `msgid` across several language files. -2. _`msgid` as a unique, structured key_. It would describe the sentence role in the application in a structured way, -including the template or part where the string is located instead of its content. +2. _`msgid` as a unique, structured key_. +It would describe the sentence role in the application in a structured way, including the template or part where the +string is located instead of its content. - it's a great way to have the code organized, separating the text content from the template logic. - however, that could bring problems to the translator that would miss the context. A source language file would be needed as a basis for other translations. Example: the developer would ideally have an `en.po` file, that translators would read to understand what to write in `fr.po` for instance. - - missing translations would display meaningless keys on screen (`TOP_MENU_WELCOME` instead of `Hello there, User!` + - missing translations would display meaningless keys on screen (`top_menu.welcome` instead of `Hello there, User!` on the said untranslated French page). That's good it as would force translation to be complete before publishing - - but bad as translation issues would be really awful in the interface. + but bad as translation issues would be really awful in the interface. Some libraries, though, include an option to + specify a given language as "fallback", having a similar behavior as the other approach. The [Gettext manual][manual] favors the first approach, as in general it's easier for translators and users in -case of trouble. That's how we will be working here as well. +case of trouble. That's how we will be working here as well. However, the [Symfony documentation][symfony-keys] favors +keyword-based translation, to allow for independent changes of all translations without affecting templates as well. ### Everyday usage In a common application, you would use some Gettext functions while writing static text in your pages. Those sentences @@ -204,7 +227,7 @@ the actual interface. Given that, let's tie together what we have discussed so f