30
Lokalisierung von Ruby on Rails Till Vollmer Geschäftsführer Codemart GmbH

Till Vollmer Presentation

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Till Vollmer Presentation

Lokalisierung vonRuby on Rails

Till VollmerGeschäftsführer Codemart GmbH

Page 2: Till Vollmer Presentation

Locali[s|z]ing Ruby on Rails

Till VollmerManaging Director Codemart GmbH

“The Confusion of Tongues”by Gustave Doré

Page 3: Till Vollmer Presentation

3

Why?

64% of people onlinedo not speak English!(680 Mio people)

Source: http://global-reach.biz

Page 4: Till Vollmer Presentation

4

Definitions

Internationalisation i18Nto handle international text (input, processand output)

Localising L10NProcess of making an application to fit alocale

Globalisation G11NRefers to both

Page 5: Till Vollmer Presentation

5

Locale

Language Region Time zone Time format Number display Currency

Page 6: Till Vollmer Presentation

6

Locale examples

Date: 28.Oktober 2006 - 10/28 2006 Time: 16:45 – 4:45 pm Numbers: 100.000,34 - 100,000.34 Currency: EUR 1.000 - $1,000 Language: Mädchen – girl

Page 7: Till Vollmer Presentation

7

History of Text

ASCII – American StandardCode for InformationInterchange (1967)

7 Bit with control chars developed from

telegraphic codes

Page 8: Till Vollmer Presentation

8

History of Text

Codepages Use of 8th Bit to

introduce all sort ofcharacters

Charsets ISO-8859-X Problems: No texts

with mixedencodings

Page 9: Till Vollmer Presentation

9

Unicode

Have one! system thatcan handle all possiblecharacters (1991)

encodes the underlyingcharacters — graphemes— rather than thevariant glyphs(renderings) for suchcharacters

http://ian-albert.com/misc/unichart.php

Page 10: Till Vollmer Presentation

10

Unicode

Codepoints♦Notation: U+xxxxxxxx (hex)♦Only theoretical not an encoding!♦First 256 chars -> ISO 8859-1♦Example: U+0052, U+0075, U+0062, U+0079♦Don‘t get mislead by leading 00 (not 2-

Bytes)

Page 11: Till Vollmer Presentation

11

Klingon language - Klingonese

Was rejected in 2001 by UnicodeConsortium, but exists in private AreaU+F8D0 to U+F8FF

Copyright: Paramount Pictures

Page 12: Till Vollmer Presentation

12

Unicode encodings

Mapping of codepoint into Bytes♦UTF-7 7-bit encoding, suited for transmission and storage only; obsolete

♦UTF-8 an 8-bit, variable-width encoding, compatible with ASCII

♦UTF-16 a 16-bit, variable-width encoding

♦UTF-32/UCS4 functionally identical 32-bit fixed-width encodings

♦UCS2 a 16-bit, fixed-width encoding that only supports the BMP

Most common myth: UTF-8 only needs 2 Bytesmaximum! It can use up to 4 (or even 6 in ISO-10646)

Page 13: Till Vollmer Presentation

13

UTF-8

Variable length and ASCII fits in Up to 4 Bytes (or even 6 in ISO)ISO 10646 range covered UTF-8 representation

----------------------- --------------------

Bits Hex Min Hex Max Byte Sequence in Binary

7 00000000 0000007f 0vvvvvvv11 00000080 000007FF 110vvvvv 10vvvvvv16 00000800 0000FFFF 1110vvvv 10vvvvvv 10vvvvvv21 00010000 001FFFFF 11110vvv 10vvvvvv 10vvvvvv 10vvvvvv26 00200000 03FFFFFF 111110vv 10vvvvvv 10vvvvvv 10vvvvvv 10vvvvvv31 04000000 7FFFFFFF 1111110v 10vvvvvv 10vvvvvv 10vvvvvv 10vvvvvv 10vvvvvv

Page 14: Till Vollmer Presentation

14

Unicode and Ruby

The ugly truth is:♦Ruby does not support it.♦But: only Java and .net are truly into

Unicode

Ruby supports different encoding for thecode itself

$KCODE = ' UTF8‚require ' jcode'

Page 15: Till Vollmer Presentation

15

Bad examples

"Über-Ruby".length # -> 10 "Café".length # -> 5 "Café".reverse # -> faC "Café"[0..3] #-> Caf\303 "efficient"[0..1] #=> “ef” ? -> effi Upcase, reverse, downcase, etc will not

work

Page 16: Till Vollmer Presentation

16

Implications

Be aware that for Ruby UTF-8 is just abyte sequence

e.g. String#length: Watch your validators

=> There are tools to handle Unicodecorrectly!

Hear more about Unicode and how tohandle Strings properly tomorrow on theUnicode track from Dominic Mitchell

Page 17: Till Vollmer Presentation

17

Localising Rails apps

Views Models Date/Time Currency Number formating Other: e.g. Name display Built in stuff

=> Depends on Region and language

Page 18: Till Vollmer Presentation

18

Built in stuff

Active Record error messages Active Record model names strftime (month names, weekdays) distance_of_time_in_words number_to_currency date_select, datetime_select Date.*, Time.* … => be prepared

Page 19: Till Vollmer Presentation

19

Basic things in Rails

Database set to utf-8 database.yml

encoding: utf8=> set names = utf8

set header in filter application.rb:@response.headers["Content-Type"] = "text/html;charset=utf-8"

META Tag <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

Convince your editor to use UTF-8Radrails -> global setting

Nasty: YAML has some bugs: fixtures ?!

Page 20: Till Vollmer Presentation

20

Rails Plugins/Gems

Globalize Localize Gloc Localization Simplified Ruby Gettext Ri18N

=> All have different featureshttp://wiki.rubyonrails.com/rails/pages/InternationalizationComparison

Page 21: Till Vollmer Presentation

21

Globalize

Model Translations View Translations Active Record errors Caching Currency, Date/Time Pluralisation All translations are in database Locale handling View files, ActionMailer Comes with prefilled database for ~240 countries

=> Complex, but very powerful

Page 22: Till Vollmer Presentation

22

Globalize Examples

Model:class Model < ActiveRecord::Base

translates :title, :description, :textendLocale.set "en-US"model.title= "English"

View:<%=:mytext.t%><%="My text goes here".t%><%=_"My text"%> # gettext syntax

Page 23: Till Vollmer Presentation

23

Localize

Only View Only strings Simple pluralisation, but Proc support Translations are in .rb files=> Simple but efficient if you only need string

translationLocalization.define('de') do |l| l.store 'Hello World', 'Hallo Welt'

l.store '(time)', lambda { |t| t.strftime('%H:%M') }end_"Hello World"_"(time)"

Page 24: Till Vollmer Presentation

24

Gloc

Only View Date/Time Active Record errors Pluralisation rules Translation is stored in .yml file

_gloc_rule_default: ' |n| n==1 ? "_single" : "_plural" ' man_count_plural: There are %d men. man_count_single: There is 1 man.

lwr(:man_count, 1) # => There is 1 man. lwr(:man_count, 8) # => There are 8 men.

Page 25: Till Vollmer Presentation

25

Localization Simplified

Localisation for a site that uses only ONElanguage other than English

Date/Time Active Record Error Messages Sets connection and HTTP header View is normal

Page 26: Till Vollmer Presentation

26

Ruby Gettext

Based on GNU gettext Translation stored in po/mo files A lot of tools exist to translate Rails support (Active Records Errors,

ActionMailer) Pluralisation

_"My Text"

Page 27: Till Vollmer Presentation

27

Ri18N

Again based on gettext, but not socomplex

Po files NO Rails support Purly based on Ruby for Ruby

=> all built in things must be taken care of

Page 28: Till Vollmer Presentation

28

Recommendations

Choose wisely Patching may occur If you need model translation, the only

choice is Globalize Using _() in general is a good idea if you

plan to go global ant take over the world

Page 29: Till Vollmer Presentation

29

Summary

Unicode is the best choice (UTF-8) forstoring texts

Ruby is not good at UTF-8 (but not alone) Localisation: A lot more than store text A lot of plugins exist to help

Page 30: Till Vollmer Presentation

30

DEMO