Slide 1

Slide 1 text

Translating Your WordPress Project Cameron Jones

Slide 2

Slide 2 text

@cameronjonesweb Translating Your WordPress Project More than half of all WP sites aren’t English (51.1%) wordpress.org/about/stats

Slide 3

Slide 3 text

@cameronjonesweb Translating Your WordPress Project Terminology Guide ● Internationalisation (i18n): The methodology of developing your project in a way that allows for it to adopt different translations ● Localisation (L10n): The process of adapting your project to different languages and locales ● Translation: A file that contains translated text in a different language to the source text

Slide 4

Slide 4 text

@cameronjonesweb Translating Your WordPress Project How Do You Localise A WordPress Site? ● With a plugin ● With third party services ● Using a Multisite

Slide 5

Slide 5 text

@cameronjonesweb Translating Your WordPress Project Translation Plugins MultilingualPress Polylang WPML TranslatePress

Slide 6

Slide 6 text

@cameronjonesweb Translating Your WordPress Project Third Party Services Google Translate WeGlot

Slide 7

Slide 7 text

@cameronjonesweb Translating Your WordPress Project WordPress Multisite

Slide 8

Slide 8 text

@cameronjonesweb Translating Your WordPress Project Internationalising Themes & Plugins ● Translations are stored in PO, MO and POT files ● Core functionality determines the right language file based on site and user settings ● This is what we will be learning today

Slide 9

Slide 9 text

@cameronjonesweb Translating Your WordPress Project How Do You Select A Language? Site Settings User Settings Network Settings (Multisite)

Slide 10

Slide 10 text

@cameronjonesweb Translating Your WordPress Project How Do You Select A Language? WPLANG Constant

Slide 11

Slide 11 text

@cameronjonesweb Translating Your WordPress Project How Do You Select A Language? locale Filter

Slide 12

Slide 12 text

@cameronjonesweb Translating Your WordPress Project WordPress Default Language ● US English is the default language in WordPress, the same as most programming languages ● You can create a theme or plugin using any language, it does not even have to be English. ● But if you want to be able to translate it properly, your original text must be in US English

Slide 13

Slide 13 text

@cameronjonesweb Translating Your WordPress Project Text Domain ● Put in your theme or plugin file header (style.css for themes or main plugin file) ● Must be unique ● If distributing on WordPress.org, must match the slug of the theme/plugin (more on that later)

Slide 14

Slide 14 text

@cameronjonesweb Translating Your WordPress Project Text Domain Plugins Themes

Slide 15

Slide 15 text

@cameronjonesweb Translating Your WordPress Project Updating Text So It Can Be Translated ● WordPress uses PHPs gettext library ● WordPress adds an abstraction layer on top of the native gettext library, so don’t use the native functions ● Gettext doesn’t execute PHP code, so you can’t use variables with these functions

Slide 16

Slide 16 text

@cameronjonesweb Translating Your WordPress Project Gettext vs WordPress functions

Slide 17

Slide 17 text

@cameronjonesweb Translating Your WordPress Project Updating Text So It Can Be Translated

Slide 18

Slide 18 text

@cameronjonesweb Translating Your WordPress Project Updating Text So It Can Be Translated

Slide 19

Slide 19 text

@cameronjonesweb Translating Your WordPress Project Text that requires context

Slide 20

Slide 20 text

@cameronjonesweb Translating Your WordPress Project Security ● Always escape translated text when you display it ● As translated text can be filtered, a malicious actor could replace it with JavaScript ● There are built in functions for escaping translations

Slide 21

Slide 21 text

@cameronjonesweb Translating Your WordPress Project Escaping Translations

Slide 22

Slide 22 text

@cameronjonesweb Translating Your WordPress Project Translations including variables

Slide 23

Slide 23 text

@cameronjonesweb Translating Your WordPress Project Translating numbers

Slide 24

Slide 24 text

@cameronjonesweb Translating Your WordPress Project International number formats

Slide 25

Slide 25 text

@cameronjonesweb Translating Your WordPress Project Translating numbers including variables

Slide 26

Slide 26 text

@cameronjonesweb Translating Your WordPress Project Translations in JavaScript ● WordPress has included the wp-i18n script in core since 6.2 ● With wp-i18n you can use the same functions as with PHP ● Prior to this script being made available, you could use wp_localize_script to store translations in JavaScript variables

Slide 27

Slide 27 text

@cameronjonesweb Translating Your WordPress Project Best Practices For Translating Strings ● Translate phrases (sentences and paragraphs) rather than individual words ● Use print formatting instead of concatenation to include variables in translated strings ● Don’t rely on existing core translations as context may be different, provide translations for all your strings

Slide 28

Slide 28 text

@cameronjonesweb Translating Your WordPress Project How do we actually localise? ● WordPress uses .MO, .PO and .POT files to store translations

Slide 29

Slide 29 text

@cameronjonesweb Translating Your WordPress Project .MO Files ● Machine Object file ● Binary ● What WordPress loads the translations from ● Please don’t try to edit these files

Slide 30

Slide 30 text

@cameronjonesweb Translating Your WordPress Project .MO Files

Slide 31

Slide 31 text

@cameronjonesweb Translating Your WordPress Project .PO Files ● Portable Object file ● Human readable ● Used to generate the .MO files ● These are the files you put the translated text in

Slide 32

Slide 32 text

@cameronjonesweb Translating Your WordPress Project .PO Files

Slide 33

Slide 33 text

@cameronjonesweb Translating Your WordPress Project .POT Files ● Portable Object Template file ● Human readable ● Used to generate the .PO files ● Effectively a blank .PO file, just contains the original strings

Slide 34

Slide 34 text

@cameronjonesweb Translating Your WordPress Project Generating the translation files ● Extract strings from source code and add them to a .POT file ● Localise the .POT file into another language and save that as a .PO file ● Use translation software to generate the .MO file

Slide 35

Slide 35 text

@cameronjonesweb Translating Your WordPress Project How do we generate a .POT file? ● WP CLI ● Grunt script ● qSandbox

Slide 36

Slide 36 text

@cameronjonesweb Translating Your WordPress Project Use WP CLI to make a .POT file

Slide 37

Slide 37 text

@cameronjonesweb Translating Your WordPress Project Use Grunt to make a .POT file

Slide 38

Slide 38 text

@cameronjonesweb Translating Your WordPress Project Use qSandbox to make a .POT file

Slide 39

Slide 39 text

@cameronjonesweb Translating Your WordPress Project How do we generate .PO and .MO files? PoEdit

Slide 40

Slide 40 text

@cameronjonesweb Translating Your WordPress Project Naming convention for .PO and .MO files ● Plugins: ${textdomain}-${language}.po/mo Eg: wordpress-seo-en_UK.po ● Themes: ${language}.po/mo Eg: en_UK.po

Slide 41

Slide 41 text

@cameronjonesweb Translating Your WordPress Project Naming convention for .PO and .MO files Theme Plugin

Slide 42

Slide 42 text

@cameronjonesweb Translating Your WordPress Project Use Loco Translate for end to end localisation

Slide 43

Slide 43 text

@cameronjonesweb Translating Your WordPress Project Loading translation files ● Themes and plugins must inform WordPress where the translation files are ● Provide the directory where the files are, not individual files ● The functions for themes and plugins take very different arguments

Slide 44

Slide 44 text

@cameronjonesweb Translating Your WordPress Project Loading translation files - Themes

Slide 45

Slide 45 text

@cameronjonesweb Translating Your WordPress Project Loading translation files - Plugins

Slide 46

Slide 46 text

@cameronjonesweb Translating Your WordPress Project Translating on WP.org ● You don’t need to provide translation files for themes and plugins on WP.org ● Translations are made on translate.wordpress.org ● Translations are downloaded to wp-content/languages

Slide 47

Slide 47 text

About Me Cameron Jones WordPress developer & DevOps Engineer Founder of Mongoose Marketplace @cameronjonesweb on all the things cameronjonesweb.com.au

Slide 48

Slide 48 text

@cameronjonesweb Translating Your WordPress Project Links To view the slides visit: cameronjonesweb.com.au/wpleeds Source Code: github.com/cameronjonesweb/cameronjonesweb-i18n