43
© All rights reserved. Zend Technologies, Inc. Zend Framework I18n WEBINAR By Thomas Weidner

Plugin Zend Framework I18nWebinar Translation

Embed Size (px)

Citation preview

Page 1: Plugin Zend Framework I18nWebinar Translation

© All rights reserved. Zend Technologies, Inc.

Zend FrameworkI18n WEBINARBy Thomas Weidner

Page 2: Plugin Zend Framework I18nWebinar Translation

© All rights reserved. Zend Technologies, Inc.

Introductionor who the hell is that guy?

Page 3: Plugin Zend Framework I18nWebinar Translation

© All rights reserved. Zend Technologies, Inc.

Introduction

35 years old, austrian (not australian)

Developer since more than 17 years

ZF since 2006

I18n Team Leader

Thomas Weidner

3 Introduction

Page 4: Plugin Zend Framework I18nWebinar Translation

© All rights reserved. Zend Technologies, Inc.

Overviewor what will we talk about today?

Page 5: Plugin Zend Framework I18nWebinar Translation

© All rights reserved. Zend Technologies, Inc.

Overview

• Internationalization (I18n)

Generic overview

Zend Framework and I18n

• Translation

What is translation about?

Creating and working with translation

Let’s view it

Future improvements

5 Overview

Page 6: Plugin Zend Framework I18nWebinar Translation

© All rights reserved. Zend Technologies, Inc.

Internationalization (I18n)or one to rule them all ?

Page 7: Plugin Zend Framework I18nWebinar Translation

© All rights reserved. Zend Technologies, Inc.

Generic overview – I18n vs L10n

• Internationalization versus Localization:

I18n => Support other regions without changing code

L10n => Adopt your project for a specific region

• Zend Framework supports them all

Not all people are native English!

7 Internationalization

Page 8: Plugin Zend Framework I18nWebinar Translation

© All rights reserved. Zend Technologies, Inc.

Generic overview – What’s a locale

• Locale

Locale, the working horse for L10n

Language + Region

Region vs Country

Additional informations (charset, encoding)

8 Internationalization

en_US = english language + region USAde_AT = german language + region Austria

Page 9: Plugin Zend Framework I18nWebinar Translation

© All rights reserved. Zend Technologies, Inc.

Zend Framework and I18n – Classes

• Zend_Locale

Locale handling (independend from PHP and OS)

Pre-translated informations

Formatting

9 Internationalization

Zend_Locale

Zend_Currency Zend_Date Zend_Translate

Zend FrameworkZF I18n core

Page 10: Plugin Zend Framework I18nWebinar Translation

© All rights reserved. Zend Technologies, Inc.

Zend Framework and I18n – Classes

• Zend_Currency

Localized currency handling

• Zend_Date

Localized date and time handling

• Zend_Translate

Generic translation handling

10 Internationalization

Page 11: Plugin Zend Framework I18nWebinar Translation

© All rights reserved. Zend Technologies, Inc.

Translation within ZFor what he says?

Page 12: Plugin Zend Framework I18nWebinar Translation

© All rights reserved. Zend Technologies, Inc.

Overview

• How does translation work

Concept and workflow

• Source files

Creation and handling

• Translate me

Usage

• Specials

Caching, Performance

12 Translation within Zend Framework

Page 13: Plugin Zend Framework I18nWebinar Translation

© All rights reserved. Zend Technologies, Inc.

How does translation work

• Languages are different

Provide your user with a language he understands

Most browsers send their wished languages

Translation is just one part to a multilingual site

13 Translation within Zend Framework

User accessingSite

Detectlanguage

Selecttranslation

Settranslation

Page 14: Plugin Zend Framework I18nWebinar Translation

© All rights reserved. Zend Technologies, Inc.

Source files – Adapters Concept

• Adapter concept

Multiple source types supported

Easy to implement own adapters

• Select your adapter based on your usage

Usage may grow… switching to other adapter is just 1 codeline

14 Translation within Zend Framework

Page 15: Plugin Zend Framework I18nWebinar Translation

© All rights reserved. Zend Technologies, Inc.

Source files – Adapters

Array

Csv

Gettext

Ini

Qt

Tbx

Tmx

Xliff

XmlTm

your own…

15 Translation within Zend Framework

Page 16: Plugin Zend Framework I18nWebinar Translation

© All rights reserved. Zend Technologies, Inc.

Source files – Array adapter

• Pro

Fast, Faster, Fastest

Human readable

return array(‘Original content’ => ‘Translated content’‘Another content’ => ‘Also translated’

);

16 Translation within Zend Framework

• Con

No editor tools

Not customer friendly

• Usage

During development

Small to medium sites

Page 17: Plugin Zend Framework I18nWebinar Translation

© All rights reserved. Zend Technologies, Inc.

Source files – CSV adapter

• ProFast

Human readable

Customer editable

“Original content”;”Translated content”“Another content”;“Also translated”

17 Translation within Zend Framework

• Con

No creation tools

• Usage

Translations by customer

Small to medium sites

Page 18: Plugin Zend Framework I18nWebinar Translation

© All rights reserved. Zend Technologies, Inc.

Source files – INI adapter

• Pro

Fast

Human readable

Original_content = “Translated content”Another_content = “Also translated”

18 Translation within Zend Framework

• Con

No creation tools

Not customer friendly

Key limitations• Usage

Small to medium sites

Mixing translation with configuration

Page 19: Plugin Zend Framework I18nWebinar Translation

© All rights reserved. Zend Technologies, Inc.

Source files – Gettext adapter

• ProEncrypted for customer

Free Tools

Professional translation

BINARY FORMAT (*.mo files)… *.po is NOT Gettext

19 Translation within Zend Framework

• Con

Fixed for customers

Not human readable

• Usage

Small to enterprise sites

Many sources available

Page 20: Plugin Zend Framework I18nWebinar Translation

© All rights reserved. Zend Technologies, Inc.

Source files – TMX adapter

• Pro

Professional translation tools

Supports HTML tags

** XML HEADERS **<tu tuid='Message 1'>

<tuv xml:lang="en"><seg>Message 1 (en)</seg></tuv><tuv xml:lang="fr"><seg>Message 1 (fr)</seg></tuv>

</tu>

20 Translation within Zend Framework

• Con

No free good tools

Difficult for customer

Must use caching• Usage

With professional translation services

Medium to Enterprise sites

HINT: TMX is said to be “Industry standard” butBEWARE it’s very huge & bloated format

Page 21: Plugin Zend Framework I18nWebinar Translation

© All rights reserved. Zend Technologies, Inc.

Working examplesor are you sure it really works?

Page 22: Plugin Zend Framework I18nWebinar Translation

© All rights reserved. Zend Technologies, Inc.

Translate me – Manual usage

$trans = new Zend_Translate(‘array’,‘/www/lang/’,‘de_AT’,array(‘scan’ => Zend_Translate::LOCALE_DIRECTORY)

);$trans->translate(‘I said hello’);

22 Working examples

Page 23: Plugin Zend Framework I18nWebinar Translation

© All rights reserved. Zend Technologies, Inc.

Translate me – Manual usage

$trans = new Zend_Translate(‘array’,‘/www/lang/’,‘de_AT’,array(‘scan’ => Zend_Translate::LOCALE_DIRECTORY)

);$trans->translate(‘I said hello’);

23 Working examples

Select your Adapter

Switch your working adapter within a single line

Page 24: Plugin Zend Framework I18nWebinar Translation

© All rights reserved. Zend Technologies, Inc.

Translate me – Manual usage

$trans = new Zend_Translate(‘array’,‘/www/lang/’,‘de_AT’,array(‘scan’ => Zend_Translate::LOCALE_DIRECTORY)

);$trans->translate(‘I said hello’);

24 Working examples

Where are your translations?

Directory or

Single file

Page 25: Plugin Zend Framework I18nWebinar Translation

© All rights reserved. Zend Technologies, Inc.

Translate me – Manual usage

$trans = new Zend_Translate(‘array’,‘/www/lang/’,‘de_AT’,array(‘scan’ => Zend_Translate::LOCALE_DIRECTORY)

);$trans->translate(‘I said hello’);

25 Working examples

Which language ?

Automatic detection (‘auto’, ‘browser’)

Manual declaration (‘de_AT’, ‘en_US’)

Use application wide settings (Zend_Application)

Page 26: Plugin Zend Framework I18nWebinar Translation

© All rights reserved. Zend Technologies, Inc.

Translate me – Manual usage

$trans = new Zend_Translate(‘array’,‘/www/lang/’,‘de_AT’,array(‘scan’ => Zend_Translate::LOCALE_DIRECTORY)

);$trans->translate(‘I said hello’);

26 Working examples

Several options

Scan: search for translations

• LOCALE-DIRECTORY : /www/lang/en/file.php

• LOCALE-FILE: /www/lang/file-en.php

Page 27: Plugin Zend Framework I18nWebinar Translation

© All rights reserved. Zend Technologies, Inc.

Translate me – Manual usage

$trans = new Zend_Translate(‘array’,‘/www/lang/’,‘de_AT’,array(‘scan’ => Zend_Translate::LOCALE_DIRECTORY)

);$trans->translate(‘I said hello’);

27 Working examples

It’s alive!

Returns: “Ich habe Hallo gesagt”

Page 28: Plugin Zend Framework I18nWebinar Translation

© All rights reserved. Zend Technologies, Inc.

Translate me – Manual usage

/language

/language/en_GB/language/en_GB/file1.xxx

/language/de_CH/language/de_CH/file1.xxx

28 Working examples

Zend_Translate::LOCALE_DIRECTORY:

Locale within the directory

Also partitial recognised like:

• /language/App1-en_US/xxx

• /language/Something_en_GB_different/xxx

Page 29: Plugin Zend Framework I18nWebinar Translation

© All rights reserved. Zend Technologies, Inc.

Translate me – Manual usage

/language

/language/dir//language/dir/file1-en_GB.xxx

/language/dir2/language/dir2/file2-de_CH.xxx

29 Working examples

Zend_Translate::LOCALE_FILE:

Locale within the filename

Also partitial recognised like:

• file1.en.tmx

• file_en-GB.php

• file_de-something.mo

Page 30: Plugin Zend Framework I18nWebinar Translation

© All rights reserved. Zend Technologies, Inc.

Translate me – View helper

30 Working examples

Translation view helper (in your view script)

Simpler

Smaller

$this->translate(‘I said hello’);

$this->translate(‘Company Name’, ‘en’);

Page 31: Plugin Zend Framework I18nWebinar Translation

© All rights reserved. Zend Technologies, Inc.

Translate me – Dynamic translations

31 Working examples

Add parameters to your translation

sprintf Syntax

Multiple parameters possible

Other languages may change order

$this->translate(“Pay %1\$s euro”, $money);

$this->translate(“Pay %1\$s euro until %2\$s”, $money, $date);

Page 32: Plugin Zend Framework I18nWebinar Translation

© All rights reserved. Zend Technologies, Inc.

Other components – Registry

32 Working examples

Registry

Application wide usage

• Zend_Form

• Zend_Validate

• Zend_View

• others

$translate = new Zend_Translate(……);

Zend_Registry::set(‘Zend_Translate’, $translate);

Page 33: Plugin Zend Framework I18nWebinar Translation

© All rights reserved. Zend Technologies, Inc.

Other components – Application

33 Working examples

Application

Application wide recognised

Configurable

resources.translate.adapter = “array“;resources.translate.locale = “de_AT”;resources.translate.data = “/www/lang/”;

Page 34: Plugin Zend Framework I18nWebinar Translation

© All rights reserved. Zend Technologies, Inc.

Special – Plural translation

34 Working examples

$translate->plural(‘Car’, ‘Cars’, 2);$translate->translate(array(‘Car’, ‘Cars’, 2));

$this->translate(array(‘Car’, ‘Cars’, 2));

Singular / Plural

Car (one item) / Singular

Cars (multiple items) / Plural

Same method for singular & plural

Page 35: Plugin Zend Framework I18nWebinar Translation

© All rights reserved. Zend Technologies, Inc.

Special – Plural translation 2

35 Working examples

$this->translate(array(‘%1\$s Car’, ‘%1\$s Cars’, $nr), $nr);

Don’t forget your parameters

1 Car

3 Cars

Plural rules for all languages implemented

Page 36: Plugin Zend Framework I18nWebinar Translation

© All rights reserved. Zend Technologies, Inc.

Special – Plural translation 3

36 Working examples

$this->translate(array(‘Singular’, ‘Plural1’, ‘Plural2’, 2));

Prepared for PHP 6

Several languages define more than 1 plural

Allows to write the source code in YOUR language

Page 37: Plugin Zend Framework I18nWebinar Translation

© All rights reserved. Zend Technologies, Inc.

Special – Plural translation 4

Plural rules supported by:

Array

CSV

Gettext

Not supported by:

Ini

Qt

Tmx

Tbx

Xliff

XmlTm

37 Working examples

Page 38: Plugin Zend Framework I18nWebinar Translation

© All rights reserved. Zend Technologies, Inc.

Special – Performance

38 Working examples

Caching… caching… caching

• Rule1: Use a cache !!!

Page 39: Plugin Zend Framework I18nWebinar Translation

© All rights reserved. Zend Technologies, Inc.

Special – Performance

39 Working examples

Caching… caching… caching

• Rule1: Use a cache !!!

• Rule2: Always use a cache!

Page 40: Plugin Zend Framework I18nWebinar Translation

© All rights reserved. Zend Technologies, Inc.

Special – Performance

40 Working examples

Caching… caching… caching

• Rule1: Use a cache !!!

• Rule2: Always use a cache!!

• Rule3: You must really use a cache!

Page 41: Plugin Zend Framework I18nWebinar Translation

© All rights reserved. Zend Technologies, Inc.

Special – Performance

41 Working examples

Caching… caching… caching

• Rule1: Use a cache !!!

• Rule2: Always use a cache!!

• Rule3: You must really use a cache!

All adapters have SAME SPEED

• Only initial reading differs

Page 42: Plugin Zend Framework I18nWebinar Translation

© All rights reserved. Zend Technologies, Inc.

Summary

Translation with Zend_Translate is

Simple to use

Quickly implemented

Integrated in many other ZF components

For more information

Read the manual

Subscribe by blog: http://www.thomasweidner.com

42 Working examples

Page 43: Plugin Zend Framework I18nWebinar Translation

© All rights reserved. Zend Technologies, Inc.

I hope you enjoyed this Webinaror hey man, get back to work ☺