Internationalization with the symfony framework
-
Upload
th0masr -
Category
Technology
-
view
156 -
download
3
description
Transcript of Internationalization with the symfony framework
![Page 1: Internationalization with the symfony framework](https://reader034.fdocuments.net/reader034/viewer/2022050905/54c71cbc4a79595e4e8b4590/html5/thumbnails/1.jpg)
Internationalization| Thomas Rabaix – h-p://www.soleoweb.com
Internationalization Thomas Rabaix – Soleoweb SARL
![Page 2: Internationalization with the symfony framework](https://reader034.fdocuments.net/reader034/viewer/2022050905/54c71cbc4a79595e4e8b4590/html5/thumbnails/2.jpg)
Internationalization| Thomas Rabaix – h-p://www.soleoweb.com
Thomas Rabaix
• freelance
• symfony fan – Conference #sflive09 – Book : more with symfony – Plugins
• swFunctionalTestGenerationPlugin : generate ready-to-be-customized test skeletons in a matter of minutes
• swCrossLinkApplicationPlugin : add cross application link in your project • sfSolrPlugin : integrates Solr into symfony framework. • Many more
![Page 3: Internationalization with the symfony framework](https://reader034.fdocuments.net/reader034/viewer/2022050905/54c71cbc4a79595e4e8b4590/html5/thumbnails/3.jpg)
Internationalization| Thomas Rabaix – h-p://www.soleoweb.com
What is internationalization and localization ?
• Internationalization
• Localization
![Page 4: Internationalization with the symfony framework](https://reader034.fdocuments.net/reader034/viewer/2022050905/54c71cbc4a79595e4e8b4590/html5/thumbnails/4.jpg)
Internationalization| Thomas Rabaix – h-p://www.soleoweb.com
Culture ?
• Culture is the addition of
– Language + Region
• fr : french • fr_FR : french from France • fr_BE : french from Belgium • en_UK : english from United-Kingdom • en_US : english from United-States
![Page 5: Internationalization with the symfony framework](https://reader034.fdocuments.net/reader034/viewer/2022050905/54c71cbc4a79595e4e8b4590/html5/thumbnails/5.jpg)
Internationalization| Thomas Rabaix – h-p://www.soleoweb.com
What elements can be i18n and l10n ?
• Internationalization – url : routing – Model : doctrine + propel – form : sfForm framework
• Localization – number – currency – date / time
![Page 6: Internationalization with the symfony framework](https://reader034.fdocuments.net/reader034/viewer/2022050905/54c71cbc4a79595e4e8b4590/html5/thumbnails/6.jpg)
Internationalization| Thomas Rabaix – h-p://www.soleoweb.com
Internationalization
• most of the i18n code is based on the Prado framework • ICU data has been updated on sf1.3 and sf1.4
• edit your settings.yml and enable i18n options
![Page 7: Internationalization with the symfony framework](https://reader034.fdocuments.net/reader034/viewer/2022050905/54c71cbc4a79595e4e8b4590/html5/thumbnails/7.jpg)
Internationalization| Thomas Rabaix – h-p://www.soleoweb.com
Culture detection
![Page 8: Internationalization with the symfony framework](https://reader034.fdocuments.net/reader034/viewer/2022050905/54c71cbc4a79595e4e8b4590/html5/thumbnails/8.jpg)
Internationalization| Thomas Rabaix – h-p://www.soleoweb.com
Update culture
• sfContext::getInstance()->getUser()->setCulture(‘fr’)
• Notify the user.change_culture (does not change user’s culture)
![Page 9: Internationalization with the symfony framework](https://reader034.fdocuments.net/reader034/viewer/2022050905/54c71cbc4a79595e4e8b4590/html5/thumbnails/9.jpg)
Internationalization| Thomas Rabaix – h-p://www.soleoweb.com
Culture in URL
• This is a feature built in the sfRouting class and symfony uses the value to update the user current culture.
• example : /en/recipes/search => sf_culture = en
![Page 10: Internationalization with the symfony framework](https://reader034.fdocuments.net/reader034/viewer/2022050905/54c71cbc4a79595e4e8b4590/html5/thumbnails/10.jpg)
Internationalization| Thomas Rabaix – h-p://www.soleoweb.com
Creating i18n url
• example – /recettes/recherche – /recipes/search
• This is not a built in feature, however plugins or code can help you to create translated url.
![Page 11: Internationalization with the symfony framework](https://reader034.fdocuments.net/reader034/viewer/2022050905/54c71cbc4a79595e4e8b4590/html5/thumbnails/11.jpg)
Internationalization| Thomas Rabaix – h-p://www.soleoweb.com
Solution 1 : ysfDimensionPlugin
• You can create custom configuration files per dimension, – a culture dimension with 'fr' or 'en'
• then you just have to create : – SF_APP_FOLDER/config/en/routing.yml – SF_APP_FOLDER/config/fr/routing.yml
• con: duplicate routes configurations on each routing.yml files and can be tricky to setup
• pro : no performances overhead
![Page 12: Internationalization with the symfony framework](https://reader034.fdocuments.net/reader034/viewer/2022050905/54c71cbc4a79595e4e8b4590/html5/thumbnails/12.jpg)
Internationalization| Thomas Rabaix – h-p://www.soleoweb.com
Solution 2 : custom code
• Create a custom sfRouting class
• Usage
![Page 13: Internationalization with the symfony framework](https://reader034.fdocuments.net/reader034/viewer/2022050905/54c71cbc4a79595e4e8b4590/html5/thumbnails/13.jpg)
Internationalization| Thomas Rabaix – h-p://www.soleoweb.com
Model definition
• Propel and Doctrine both have built in feature to handle translation version
Doctrine Propel
![Page 14: Internationalization with the symfony framework](https://reader034.fdocuments.net/reader034/viewer/2022050905/54c71cbc4a79595e4e8b4590/html5/thumbnails/14.jpg)
Internationalization| Thomas Rabaix – h-p://www.soleoweb.com
Model and culture
• The default model culture value can be accessed through : – sfDoctrineRecord::getDefaultCulture() – sfPropel::getDefaultCulture()
• The default model culture value can be updated through : – sfDoctrineRecord::setDefaultCulture() – sfPropel::setDefaultCulture()
![Page 15: Internationalization with the symfony framework](https://reader034.fdocuments.net/reader034/viewer/2022050905/54c71cbc4a79595e4e8b4590/html5/thumbnails/15.jpg)
Internationalization| Thomas Rabaix – h-p://www.soleoweb.com
Model i18n properties
• Accessing i18n property : – always uses the default culture
• Doctrine
• Propel
![Page 16: Internationalization with the symfony framework](https://reader034.fdocuments.net/reader034/viewer/2022050905/54c71cbc4a79595e4e8b4590/html5/thumbnails/16.jpg)
Internationalization| Thomas Rabaix – h-p://www.soleoweb.com
Model : query
• ORM never automatically left join the translation table
• Doctrine
• Propel
![Page 17: Internationalization with the symfony framework](https://reader034.fdocuments.net/reader034/viewer/2022050905/54c71cbc4a79595e4e8b4590/html5/thumbnails/17.jpg)
Internationalization| Thomas Rabaix – h-p://www.soleoweb.com
Helpers – localization
• NumberHelper: – format_currency
– format_number
![Page 18: Internationalization with the symfony framework](https://reader034.fdocuments.net/reader034/viewer/2022050905/54c71cbc4a79595e4e8b4590/html5/thumbnails/18.jpg)
Internationalization| Thomas Rabaix – h-p://www.soleoweb.com
Helpers - DateHelper
• format_date helper and format_datetime helper – Output format (culture = en)
the format_date and date.me helpers automa.cally localize and translate date using ICU data
![Page 19: Internationalization with the symfony framework](https://reader034.fdocuments.net/reader034/viewer/2022050905/54c71cbc4a79595e4e8b4590/html5/thumbnails/19.jpg)
Internationalization| Thomas Rabaix – h-p://www.soleoweb.com
Helpers – DateHelper
• format_daterange helper
• distance_of_time_in_words helper
![Page 20: Internationalization with the symfony framework](https://reader034.fdocuments.net/reader034/viewer/2022050905/54c71cbc4a79595e4e8b4590/html5/thumbnails/20.jpg)
Internationalization| Thomas Rabaix – h-p://www.soleoweb.com
Helpers – I18n
• __() helper
• format_number_choice helper
![Page 21: Internationalization with the symfony framework](https://reader034.fdocuments.net/reader034/viewer/2022050905/54c71cbc4a79595e4e8b4590/html5/thumbnails/21.jpg)
Internationalization| Thomas Rabaix – h-p://www.soleoweb.com
Behind helpers
• Helpers are just shortcut functions to internal class – sfNumberFormat and sfDateFormat – sfI18n
• You can use these classes in your actions or custom lib – Check out the symfony API
![Page 22: Internationalization with the symfony framework](https://reader034.fdocuments.net/reader034/viewer/2022050905/54c71cbc4a79595e4e8b4590/html5/thumbnails/22.jpg)
Internationalization| Thomas Rabaix – h-p://www.soleoweb.com
sfForm – i18n friendly widgets
• the sfForm framework provides with many i18n widget :
– sfWidgetFormI18nChoiceCountry – sfWidgetFormI18nChoiceCurrency – sfWidgetFormI18nChoiceLanguage – sfWidgetFormI18nChoiceTimezone – sfWidgetFormI18nDate – sfWidgetFormI18nDateTime – sfWidgetFormI18nTime
• And the related validator :
– sfValidatorI18nChoiceTimezone – sfValidatorI18nChoiceLanguage – sfValidatorI18nChoiceCountry
All these widgets accept a culture op.on
en
zh
![Page 23: Internationalization with the symfony framework](https://reader034.fdocuments.net/reader034/viewer/2022050905/54c71cbc4a79595e4e8b4590/html5/thumbnails/23.jpg)
Internationalization| Thomas Rabaix – h-p://www.soleoweb.com
sfForm - swCultureDemoForm
Culture is set in the controller
DO NOT USE sfContext::getInstance()
![Page 24: Internationalization with the symfony framework](https://reader034.fdocuments.net/reader034/viewer/2022050905/54c71cbc4a79595e4e8b4590/html5/thumbnails/24.jpg)
Internationalization| Thomas Rabaix – h-p://www.soleoweb.com
sfForm – elements translation
• The sfForm has a translation callback function through the sfWidgetFormSchemaFormatter class
• Label, error message, help messages are translated by using the callable method
• warning : with sf1.3 and sf1.4 "choice" widgets which extends the sfWidgetFormChoiceBase class automatically translates choices provided when the widget is rendered.
![Page 25: Internationalization with the symfony framework](https://reader034.fdocuments.net/reader034/viewer/2022050905/54c71cbc4a79595e4e8b4590/html5/thumbnails/25.jpg)
Internationalization| Thomas Rabaix – h-p://www.soleoweb.com
sfForm + ORM
• symfony provides an unified API between both ORM to handle i18n model.
• If you need to build a form to edit translation from a model, you can use the `embedI18n` method – method declared in the sfFromDoctrine and sfFormPropel
![Page 26: Internationalization with the symfony framework](https://reader034.fdocuments.net/reader034/viewer/2022050905/54c71cbc4a79595e4e8b4590/html5/thumbnails/26.jpg)
Internationalization| Thomas Rabaix – h-p://www.soleoweb.com
sfForm – backend form
• Create a custom BaseForm[Doctrine|Propel] class in the backend/lib/form folder.
![Page 27: Internationalization with the symfony framework](https://reader034.fdocuments.net/reader034/viewer/2022050905/54c71cbc4a79595e4e8b4590/html5/thumbnails/27.jpg)
Internationalization| Thomas Rabaix – h-p://www.soleoweb.com
Translation management …
• Sources available – Gettext – MySQL – SQLite – XLIFF
• Not really user friendly
![Page 28: Internationalization with the symfony framework](https://reader034.fdocuments.net/reader034/viewer/2022050905/54c71cbc4a79595e4e8b4590/html5/thumbnails/28.jpg)
Internationalization| Thomas Rabaix – h-p://www.soleoweb.com
… an alternative
• mgI18nPlugin : interactive GUI to translate webpage
• Open sourced by menugourmet.com : online quality recipes
![Page 29: Internationalization with the symfony framework](https://reader034.fdocuments.net/reader034/viewer/2022050905/54c71cbc4a79595e4e8b4590/html5/thumbnails/29.jpg)
Internationalization| Thomas Rabaix – h-p://www.soleoweb.com
Menugourmet.com
![Page 30: Internationalization with the symfony framework](https://reader034.fdocuments.net/reader034/viewer/2022050905/54c71cbc4a79595e4e8b4590/html5/thumbnails/30.jpg)
Internationalization| Thomas Rabaix – h-p://www.soleoweb.com
example
![Page 31: Internationalization with the symfony framework](https://reader034.fdocuments.net/reader034/viewer/2022050905/54c71cbc4a79595e4e8b4590/html5/thumbnails/31.jpg)
Internationalization| Thomas Rabaix – h-p://www.soleoweb.com
mgI18nPlugin - parameters
• The plugin detects parameters in your translated message
![Page 32: Internationalization with the symfony framework](https://reader034.fdocuments.net/reader034/viewer/2022050905/54c71cbc4a79595e4e8b4590/html5/thumbnails/32.jpg)
Internationalization| Thomas Rabaix – h-p://www.soleoweb.com
mgI18nPlugin
• Display a popup to translate all messages used in the current page
• Parse lib and actions to find message (handle catalogue)
• All messages are stored in database
![Page 33: Internationalization with the symfony framework](https://reader034.fdocuments.net/reader034/viewer/2022050905/54c71cbc4a79595e4e8b4590/html5/thumbnails/33.jpg)
Internationalization| Thomas Rabaix – h-p://www.soleoweb.com
mgI18nPlugin - tips
• Cache is a key/value, so do not use a full text message. – Smaller is your cache, quicker it will be loaded – Use catalogue
• Code message : – The awesome recipes list => title_recipes
• Always use a prefix : – btn : btn_submit – label : label_name – link : link_to_homepage
![Page 34: Internationalization with the symfony framework](https://reader034.fdocuments.net/reader034/viewer/2022050905/54c71cbc4a79595e4e8b4590/html5/thumbnails/34.jpg)
Internationalization| Thomas Rabaix – h-p://www.soleoweb.com
mgI18nPlugin – configuration
• read the famous README file
• Tweak security
![Page 35: Internationalization with the symfony framework](https://reader034.fdocuments.net/reader034/viewer/2022050905/54c71cbc4a79595e4e8b4590/html5/thumbnails/35.jpg)
Internationalization| Thomas Rabaix – h-p://www.soleoweb.com
mgI18nPlugin – tools
• Import xliff into the database
• unreachable messages
![Page 36: Internationalization with the symfony framework](https://reader034.fdocuments.net/reader034/viewer/2022050905/54c71cbc4a79595e4e8b4590/html5/thumbnails/36.jpg)
Internationalization| Thomas Rabaix – h-p://www.soleoweb.com
mgI18nPlugin – ROADMAP
• Complete export database to xliff files
• Remove Doctrine dependency, full PDO support
• Simplify GUI
![Page 37: Internationalization with the symfony framework](https://reader034.fdocuments.net/reader034/viewer/2022050905/54c71cbc4a79595e4e8b4590/html5/thumbnails/37.jpg)
Internationalization| Thomas Rabaix – h-p://www.soleoweb.com
QUESTIONS ? Thomas Rabaix
hGp://www.soleoweb.com
Images references -‐ hGp://www.flickr.com/photos/photonoob/2164014945/ -‐ hGp://www.flickr.com/photos/vermininc/2389710332/ -‐ hGp://www.flickr.com/photos/16175430@N02/3490488907/ -‐ hGp://www.flickr.com/photos/urville_djasim/2643449676/