Upgrade to Pro — share decks privately, control downloads, hide ads and more …

i18n and L10n in TYPO3 Flow

i18n and L10n in TYPO3 Flow

Karsten Dambekalns

October 06, 2012
Tweet

More Decks by Karsten Dambekalns

Other Decks in Programming

Transcript

  1. Inspiring people to share co-lead of TYPO3 Neos and Flow

    35 years old lives in Lübeck, Germany 1 wife, 3 sons, 1 espresso machine likes canoeing and climbing Karsten Dambekalns
  2. Inspiring people to share Basics i18n is the process of

    making software localizable L10n means actually • formatting dates, numbers, … • translating text • adjusting other things (images, …)
  3. Inspiring people to share Locale Locale instances encapsulate a locale

    identifier Locales define the language and formats to use Locales form a hierarchy • en is a parent of en_US which is a parent of en_US_POSIX • Thus items for the en_US locale that do not exist, will be used from the en locale if available
  4. Inspiring people to share What you need A way to

    translate in templates A way to store translations of labels Formatters for dates, numbers, … An API for use in PHP
  5. Inspiring people to share f:translate the way of translating you

    will use most <f:translate id="my.label.1"/> {f:translate(id: 'my.label.1')} translation by label is possible, but discouraged • less reliable than id-based translation • prohibits changes to the original label
  6. Inspiring people to share f:form.select has translation support built in

    <f:form.select options="{paymentOptions}" translate="{by: 'id'}" /> <f:form.select options="{paymentOptions}" translate="{by: 'id', prefix: 'payment.options.', package: 'Acme.Demo.Translations'}" />
  7. Inspiring people to share Message catalogs XML Localisation Interchange File

    Format is the standard we use Looked up in well-known location Resources/ Private/ Translations/ <locale>/ <source>.xlf
  8. Inspiring people to share XLIFF basics An XLIFF file can

    hold translations for one locale It contains one or more <file> elements corresponding to a source Localizable data is stored in <trans-unit> elements which contain a <source> element to store the source text and a (non-mandatory) <target> element to store the translated text
  9. Inspiring people to share Text Text <?xml version="1.0" encoding="UTF-8"?> <xliff

    version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2"> <file original="" product-name="Acme.Demo" source- language="en" datatype="plaintext"> <body> <trans-unit id="my.label.1" xml:space="preserve"> <source>My Label 1</source> </trans-unit> </body> </file> </xliff>
  10. Inspiring people to share <?xml version="1.0" encoding="UTF-8"?> <xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">

    <file original="" product-name="Acme.Demo" source- language="en" datatype="plaintext"> <body> <trans-unit id="my.label.1" xml:space="preserve"> <source>My Label 1</source> <target>Mein Aufkleber 1</target> </trans-unit> </body> </file> </xliff>
  11. Inspiring people to share i18n configuration Default locale can be

    set in Settings.yaml Locale fallback rules are configurable TYPO3: Flow: i18n: defaultLocale: de fallbackRule: strict: FALSE order: [lv, en]
  12. Inspiring people to share i18n configuration Default locale can be

    set in Settings.yaml Locale fallback rules are configurable TYPO3: Flow: i18n: defaultLocale: de fallbackRule: strict: FALSE order: [lv, en]
  13. Inspiring people to share Placeholder use $this->view->assign('foo', 'QUUX'); $this->view->assign('bar', 'BAZ');

    <trans-unit id="my.label.3" xml:space="preserve"> <source>I have {0} and {1}</source> <target>{1} habe ich und {0} habe ich auch</target> </trans-unit> <f:translate id="my.label.3" arguments="{0: foo, 1: bar}"/>
  14. Inspiring people to share String placeholders $this->view->assign('foo', 'QUUX'); $this->view->assign('bar', 'BAZ');

    <trans-unit id="my.label.3" xml:space="preserve"> <source>I have {some} and {thing}</source> <target>{thing} habe ich und {some} habe ich auch</target> </trans-unit> <f:translate id="my.label.3" arguments="{'some': foo, 'thing': bar}"/> * will be available with Flow 2.0
  15. Inspiring people to share Using formatters $this->view->assign('currentDateAndTime', new \DateTime()); $this->view->assign('currentCost',

    1.25); <source> At {0,datetime} it costs {1,number} monetary units </source> <f:translate id="my.label.4" arguments="{ 0: currentDateAndTime, 1: currentCost }"/>
  16. Inspiring people to share Plural forms The CLDR defines six

    plural forms • zero, one, two, few, many, other Different languages use more or less forms • singular and plurals for English • one, few and other for Polish • only other for Japanese
  17. Inspiring people to share <group id="my.label.6" restype="x-gettext-plurals"> <trans-unit id="my.label.6[0]" xml:space="preserve">

    <source>There is this quark</source> </trans-unit> <trans-unit id="my.label.6[1]" xml:space="preserve"> <source>There are these quarks</source> </trans-unit> </group> Plural forms <f:translate id="my.label.6" quantity="{quarks->f:count()}"/>
  18. Inspiring people to share L10n of resources Resources can be

    localized as well The locale is part of the name • Image.png • Image.de.png • Image.lv.png Usable for all resources • Images, templates, …
  19. Inspiring people to share f:resource * see Known issues <img

    src="{f:uri.resource(path: 'Images/Image.png')}"/> <img src="../../../../Resources/Public/Images/Image.png"/> uses localized resources by default*
  20. Inspiring people to share i18n in PHP code TYPO3\Flow\I18n\Translator •

    translateById() • translateByOriginalLabel() TYPO3\Flow\I18n\Service • getConfiguration() • getLocalizedFilename() TYPO3\Flow\I18n\Formatter\*
  21. Inspiring people to share /** * @param string $locale *

    @return void */ public function selectAction($locale = NULL) { if ($locale !== NULL) { $this->i18nService->getConfiguration()->setCurrentLocale( new \TYPO3\Flow\I18n\Locale($locale) ); } $this->forward('index'); } Setting current locale WARNING: Stupid example
  22. Inspiring people to share Tips & Tricks Split catalogs at

    logical points Try to use.a.clever.id.system Avoid hardcoded labels from the start To change "original" labels simply translate to "original" language
  23. Inspiring people to share Known issues L10n support in f:resource

    VH pending in review Fallback per label is missing, currently only done per catalog Overriding parts of catalogs is missing Model translation still missing
  24. Inspiring people to share Thank You! These slides can be

    found at: http://speakerdeck.com/kdambekalns http://slideshare.net/kfish Give me feedback: [email protected] | [email protected] Download Flow: http://flow.typo3.org Follow me on twitter: @kdambekalns Support me using