Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Drupal 8 multilingual APIs
@gaborhojtsyGábor Hojtsy
DRUPAL 7 MULTILINGUAL
Drupal
CORE
DRUPAL 7 MULTILINGUAL
Drupal
CORE
LOCALE
!
DRUPAL 7 MULTILINGUAL
Drupal
CORE
L10n UP
LOCALE
!
m
DRUPAL 7 MULTILINGUAL
Drupal
CORE
L10n UP
CONTENT
TRANSLATION
LOCALE
!
Ü
m
DRUPAL 7 MULTILINGUAL
Drupal
CORE
L10n UP
CONTENT
TRANSLATION
LOCALE
I18N !
!
Ü
m
DRUPAL 7 MULTILINGUAL
Drupal
CORE
L10n UP
CONTENT
TRANSLATION
LOCALE
I18N
VARIABLE
"
!
!
Ü
m
DRUPAL 7 MULTILINGUAL
Drupal
CORE
L10n UP
CONTENT
TRANSLATION
LOCALE
I18N
VARIABLE
Entity
translation
#
"
!
!
Ü
m
DRUPAL 7 MULTILINGUAL
FOUR PILLARS IN DRUPAL 8
LANGUAGE
Base services for all
modules dealing
with data. Not just
multilingual.
!
FOUR PILLARS IN DRUPAL 8
LANGUAGE
Base services for all
modules dealing
with data. Not just
multilingual.
INTERFACE
Interface
translation has
built...
LANGUAGE
Base services for all
modules dealing
with data. Not just
multilingual.
INTERFACE
Interface
translation has
built...
LANGUAGE
Base services for all
modules dealing
with data. Not just
multilingual.
INTERFACE
Interface
translation has
built...
LANGUAGE INTERFACE CONTENT CONFIG
(! 8 é
FOUR PILLARS IN DRUPAL 8
DEALING WITH LANGUAGE
Drupal:languageManager()
LanguageManager
ConfigurableLanguageManager
DEALING WITH LANGUAGE
->getLanguages()
UND: NOT SPECIFIED
ZXX: NOT APPLICABLE
EN: ENGLISH (DEFAULT)
DEALING WITH LANGUAGE
->getLanguages()
UND: NOT SPECIFIED
ZXX: NOT APPLICABLE
HU: HUNGARIAN
IT: ITALIAN
DEALING WITH LANGUAGE
->getLanguages()
UND: NOT SPECIFIED
ZXX: NOT APPLICABLE
HU: HUNGARIAN
IT: ITALIAN
language.entity.$l...
DEALING WITH LANGUAGE
->getLanguages()
UND: NOT SPECIFIED
ZXX: NOT APPLICABLE
HU: HUNGARIAN
IT: ITALIAN
language.entity.$l...
DEALING WITH LANGUAGE
->getLanguages()
UND: NOT SPECIFIED
ZXX: NOT APPLICABLE
HU: HUNGARIAN
IT: ITALIAN
language.entity.$l...
DEALING WITH LANGUAGE
ConfigurableLanguage::

createFromLangcode(‘fr’)

->save()
DEALING WITH LANGUAGE
ConfigurableLanguage::

load(‘fr’)->delete()
DEALING WITH LANGUAGE
$languageManager

->getCurrentLanguage()
LANGUAGE INTERFACE CONTENT CONFIG
(! 8 é
FOUR PILLARS IN DRUPAL 8
INTERFACE LANGUAGE
INTERFACE LANGUAGE
t(‘English text’)
INTERFACE LANGUAGE
DEPENDENCY INJECTION
LOGIC
TRANSLATION
CONFIGURATION
USER
DEPENDENCY INJECTION
LOGIC
TRANSLATION
CONFIGURATION
USER
INTERFACE LANGUAGE
t(‘English text’)
INTERFACE LANGUAGE
t(‘English text’)
$this->t(‘English text’)
INTERFACE LANGUAGE
class Foo {
use StringTranslationTrait;
…
}
INTERFACE LANGUAGE
INTERFACE LANGUAGE
$this->t(‘English text’)

->getOption(‘langcode’);
INTERFACE LANGUAGE
format_plural(…)
$this->formatPlural(…)
JAVASCRIPT API
Drupal.t(…)
Drupal.formatPlural()
LOCALE.LINKS.MENU.YML
locale.translate_page:

title: 'User interface translation'

description: 'Configure the import …’

...
LOCALE.LINKS.MENU.YML
locale.translate_page:

title: 'User interface translation'

description: 'Configure the import …’

...
LANGUAGE INTERFACE CONTENT CONFIG
(! 8 é
FOUR PILLARS IN DRUPAL 8
English to X
NODE.PHP (SNIPPET)
/**
* Defines the node entity class.
*
* @ContentEntityType(
* id = "node",
* label = @Translation(“Con...
NODE.PHP (SNIPPET)
function baseFieldDefinitions($entity_type) {
// …
$fields['title'] =
BaseFieldDefinition::create('stri...
FIELDS THEMSELVES
FIELDS THEMSELVES
AUTOMATED!
MULTICOLUMN FIELD SNIPPET
* @FieldType(
* id = "image",
* column_groups = {
* "file" = {
* "label" = @Translation("File"),...
ENTITY LANGUAGE API
$node = Node::load(42);
$node = $node
->getTranslation(‘hu’);
$node = $entityRepository
->getTranslati...
ENTITY LANGUAGE API
$node->getUntranslated()
$node->language()
$node->getTranslationLanguages()
$node->hasTranslation(‘hu’...
VIEWS ALL THE THINGS
LANGUAGE INTERFACE CONTENT CONFIG
(! 8 é
FOUR PILLARS IN DRUPAL 8
English to X X to Y
Intelligent
objects
CONFIG/INSTALL/SYSTEM.MAINTENANCE.YML
message: '@site is currently
under maintenance. We should be
back shortly. Thank you...
CORE.DATA_TYPES.SCHEMA.YML
config_object:
type: mapping
mapping:
langcode:
type: string
label: 'Language code'
…
text:
typ...
CONFIG/SCHEMA/SYSTEM.SCHEMA.YML
system.maintenance:
type: config_object
label: 'Maintenance mode'
mapping:
message:
type: ...
SYSTEM.MAINTENANCE.YML
message: '@site is currently
under maintenance. We should be
back shortly. Thank you for your
patie...
SYSTEM.MAINTENANCE.YML
message: '@site is currently
under maintenance. We should be
back shortly. Thank you for your
patie...
SYSTEM.MAINTENANCE.YML
message: '@site is currently
under maintenance. We should be
back shortly. Thank you for your
patie...
SYSTEM.MAINTENANCE.YML
message: '@site is currently
under maintenance. We should be
back shortly. Thank you for your
patie...
SYSTEM.MAINTENANCE.YML
message: '@site is currently
under maintenance. We should be
back shortly. Thank you for your
patie...
CONFIGURATION API
$config =
Drupal::config(‘system.maintenance’);
$config->get(‘message’);
CONFIGURATION API
$config =
Drupal::config(‘system.maintenance’);
$config->get(‘message’);
OVERRIDES
APPLY AS
APPROPRIATE
CONFIGURATION API
$manager = Drupal::languageManager();
$hu = $manager->getLanguage('hu');
$original = $manager->getConfig...
CONFIGURATION API
Drupal::config(‘system.maintenance’);
Drupal::configFactory()-
>getEditable(‘system.maintenance’);
Drupa...
LANGUAGE INTERFACE CONTENT CONFIG
(! 8 é
FOUR PILLARS IN DRUPAL 8
English to X X to Y
Intelligent
objects
X to Y
Dumb
arra...
TEXT
www.drupalaton.hu @drupalaton
@gaborhojtsy
Drupal 8 multilingual APIs
Drupal 8 multilingual APIs
Drupal 8 multilingual APIs
Drupal 8 multilingual APIs
Drupal 8 multilingual APIs
Drupal 8 multilingual APIs
Drupal 8 multilingual APIs
Drupal 8 multilingual APIs
Drupal 8 multilingual APIs
Drupal 8 multilingual APIs
Drupal 8 multilingual APIs
Upcoming SlideShare
Loading in …5
×

Drupal 8 multilingual APIs

256 views

Published on

As presented at Drupal Developer Days Milan

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Drupal 8 multilingual APIs

  1. 1. Drupal 8 multilingual APIs @gaborhojtsyGábor Hojtsy
  2. 2. DRUPAL 7 MULTILINGUAL
  3. 3. Drupal CORE DRUPAL 7 MULTILINGUAL
  4. 4. Drupal CORE LOCALE ! DRUPAL 7 MULTILINGUAL
  5. 5. Drupal CORE L10n UP LOCALE ! m DRUPAL 7 MULTILINGUAL
  6. 6. Drupal CORE L10n UP CONTENT TRANSLATION LOCALE ! Ü m DRUPAL 7 MULTILINGUAL
  7. 7. Drupal CORE L10n UP CONTENT TRANSLATION LOCALE I18N ! ! Ü m DRUPAL 7 MULTILINGUAL
  8. 8. Drupal CORE L10n UP CONTENT TRANSLATION LOCALE I18N VARIABLE " ! ! Ü m DRUPAL 7 MULTILINGUAL
  9. 9. Drupal CORE L10n UP CONTENT TRANSLATION LOCALE I18N VARIABLE Entity translation # " ! ! Ü m DRUPAL 7 MULTILINGUAL
  10. 10. FOUR PILLARS IN DRUPAL 8
  11. 11. LANGUAGE Base services for all modules dealing with data. Not just multilingual. ! FOUR PILLARS IN DRUPAL 8
  12. 12. LANGUAGE Base services for all modules dealing with data. Not just multilingual. INTERFACE Interface translation has built-in update feature, improved usability. ! 8 FOUR PILLARS IN DRUPAL 8
  13. 13. LANGUAGE Base services for all modules dealing with data. Not just multilingual. INTERFACE Interface translation has built-in update feature, improved usability. CONTENT Field translation in built-in API for all entities. Content translation module provides user interface. ! 8 é FOUR PILLARS IN DRUPAL 8
  14. 14. LANGUAGE Base services for all modules dealing with data. Not just multilingual. INTERFACE Interface translation has built-in update feature, improved usability. CONTENT Field translation in built-in API for all entities. Content translation module provides user interface. CONFIG Common configuration system handles blocks, views, field settings. Unified translation. (! 8 é FOUR PILLARS IN DRUPAL 8
  15. 15. LANGUAGE INTERFACE CONTENT CONFIG (! 8 é FOUR PILLARS IN DRUPAL 8
  16. 16. DEALING WITH LANGUAGE Drupal:languageManager() LanguageManager ConfigurableLanguageManager
  17. 17. DEALING WITH LANGUAGE ->getLanguages() UND: NOT SPECIFIED ZXX: NOT APPLICABLE EN: ENGLISH (DEFAULT)
  18. 18. DEALING WITH LANGUAGE ->getLanguages() UND: NOT SPECIFIED ZXX: NOT APPLICABLE HU: HUNGARIAN IT: ITALIAN
  19. 19. DEALING WITH LANGUAGE ->getLanguages() UND: NOT SPECIFIED ZXX: NOT APPLICABLE HU: HUNGARIAN IT: ITALIAN language.entity.$langcode.yml
  20. 20. DEALING WITH LANGUAGE ->getLanguages() UND: NOT SPECIFIED ZXX: NOT APPLICABLE HU: HUNGARIAN IT: ITALIAN language.entity.$langcode.yml Locked
  21. 21. DEALING WITH LANGUAGE ->getLanguages() UND: NOT SPECIFIED ZXX: NOT APPLICABLE HU: HUNGARIAN IT: ITALIAN language.entity.$langcode.yml Locked Not locked
  22. 22. DEALING WITH LANGUAGE ConfigurableLanguage::
 createFromLangcode(‘fr’)
 ->save()
  23. 23. DEALING WITH LANGUAGE ConfigurableLanguage::
 load(‘fr’)->delete()
  24. 24. DEALING WITH LANGUAGE $languageManager
 ->getCurrentLanguage()
  25. 25. LANGUAGE INTERFACE CONTENT CONFIG (! 8 é FOUR PILLARS IN DRUPAL 8
  26. 26. INTERFACE LANGUAGE
  27. 27. INTERFACE LANGUAGE t(‘English text’)
  28. 28. INTERFACE LANGUAGE
  29. 29. DEPENDENCY INJECTION LOGIC TRANSLATION CONFIGURATION USER
  30. 30. DEPENDENCY INJECTION LOGIC TRANSLATION CONFIGURATION USER
  31. 31. INTERFACE LANGUAGE t(‘English text’)
  32. 32. INTERFACE LANGUAGE t(‘English text’) $this->t(‘English text’)
  33. 33. INTERFACE LANGUAGE class Foo { use StringTranslationTrait; … }
  34. 34. INTERFACE LANGUAGE
  35. 35. INTERFACE LANGUAGE $this->t(‘English text’)
 ->getOption(‘langcode’);
  36. 36. INTERFACE LANGUAGE format_plural(…) $this->formatPlural(…)
  37. 37. JAVASCRIPT API Drupal.t(…) Drupal.formatPlural()
  38. 38. LOCALE.LINKS.MENU.YML locale.translate_page:
 title: 'User interface translation'
 description: 'Configure the import …’
 route_name: locale.translate_page
 parent: system.admin_config_regional
 weight: 15 
 locale.translate_status:
 title: 'Available translation updates'
 route_name: locale.translate_status
 description: 'Get a status report …’
 parent: system.admin_reports
  39. 39. LOCALE.LINKS.MENU.YML locale.translate_page:
 title: 'User interface translation'
 description: 'Configure the import …’
 route_name: locale.translate_page
 parent: system.admin_config_regional
 weight: 15 
 locale.translate_status:
 title: 'Available translation updates'
 route_name: locale.translate_status
 description: 'Get a status report …’
 parent: system.admin_reports
  40. 40. LANGUAGE INTERFACE CONTENT CONFIG (! 8 é FOUR PILLARS IN DRUPAL 8 English to X
  41. 41. NODE.PHP (SNIPPET) /** * Defines the node entity class. * * @ContentEntityType( * id = "node", * label = @Translation(“Content"), * translatable = TRUE, * entity_keys = { * "id" = "nid", * "label" = "title", * "langcode" = "langcode", * } * )
  42. 42. NODE.PHP (SNIPPET) function baseFieldDefinitions($entity_type) { // … $fields['title'] = BaseFieldDefinition::create('string') ->setLabel(t('Title')) ->setRequired(TRUE) ->setTranslatable(TRUE); }
  43. 43. FIELDS THEMSELVES
  44. 44. FIELDS THEMSELVES AUTOMATED!
  45. 45. MULTICOLUMN FIELD SNIPPET * @FieldType( * id = "image", * column_groups = { * "file" = { * "label" = @Translation("File"), * "columns" = { * "target_id", "width", "height" * }, * }, * "alt" = { * "label" = @Translation("Alt"), * "translatable" = TRUE * }, * }
  46. 46. ENTITY LANGUAGE API $node = Node::load(42); $node = $node ->getTranslation(‘hu’); $node = $entityRepository ->getTranslationFromContext($node);
  47. 47. ENTITY LANGUAGE API $node->getUntranslated() $node->language() $node->getTranslationLanguages() $node->hasTranslation(‘hu’) $node->addTranslation(‘hu’) $node->removeTranslation(‘hu’)
  48. 48. VIEWS ALL THE THINGS
  49. 49. LANGUAGE INTERFACE CONTENT CONFIG (! 8 é FOUR PILLARS IN DRUPAL 8 English to X X to Y Intelligent objects
  50. 50. CONFIG/INSTALL/SYSTEM.MAINTENANCE.YML message: '@site is currently under maintenance. We should be back shortly. Thank you for your patience.' langcode: en
  51. 51. CORE.DATA_TYPES.SCHEMA.YML config_object: type: mapping mapping: langcode: type: string label: 'Language code' … text: type: string label: 'Text' translatable: true
  52. 52. CONFIG/SCHEMA/SYSTEM.SCHEMA.YML system.maintenance: type: config_object label: 'Maintenance mode' mapping: message: type: text label: 'Message to display…’ LANGCODE TRANSLATABLE STRING
  53. 53. SYSTEM.MAINTENANCE.YML message: '@site is currently under maintenance. We should be back shortly. Thank you for your patience.' langcode: en
  54. 54. SYSTEM.MAINTENANCE.YML message: '@site is currently under maintenance. We should be back shortly. Thank you for your patience.' langcode: en LANGUAGES/HU/SYSTEM.…YML
  55. 55. SYSTEM.MAINTENANCE.YML message: '@site is currently under maintenance. We should be back shortly. Thank you for your patience.' langcode: en LANGUAGES/HU/SYSTEM.…YML message: '@site karbantartás alatt áll…’
  56. 56. SYSTEM.MAINTENANCE.YML message: '@site is currently under maintenance. We should be back shortly. Thank you for your patience.' langcode: en LANGUAGES/HU/SYSTEM.…YML message: '@site karbantartás alatt áll…’ LANGUAGES/IT/SYSTEM.…YML
  57. 57. SYSTEM.MAINTENANCE.YML message: '@site is currently under maintenance. We should be back shortly. Thank you for your patience.' langcode: en LANGUAGES/HU/SYSTEM.…YML message: '@site karbantartás alatt áll…’ LANGUAGES/IT/SYSTEM.…YML message: '@site …’
  58. 58. CONFIGURATION API $config = Drupal::config(‘system.maintenance’); $config->get(‘message’);
  59. 59. CONFIGURATION API $config = Drupal::config(‘system.maintenance’); $config->get(‘message’); OVERRIDES APPLY AS APPROPRIATE
  60. 60. CONFIGURATION API $manager = Drupal::languageManager(); $hu = $manager->getLanguage('hu'); $original = $manager->getConfigOverrideLanguage(); $manager->setConfigOverrideLanguage($hu); $config = Drupal::config(‘system.maintenance'); // … $manager->setConfigOverrideLanguage($original);
  61. 61. CONFIGURATION API Drupal::config(‘system.maintenance’); Drupal::configFactory()- >getEditable(‘system.maintenance’); Drupal::languageManager() ->getLanguageConfigOverride ('hu', 'system.maintenance') ->set('message', 'Karbantartás...') ->save(); OVERRIDES APPLY AS APPROPRIATE NO OVERRIDES APPLY THE OVERRIDE ITSELF
  62. 62. LANGUAGE INTERFACE CONTENT CONFIG (! 8 é FOUR PILLARS IN DRUPAL 8 English to X X to Y Intelligent objects X to Y Dumb arrays
  63. 63. TEXT www.drupalaton.hu @drupalaton
  64. 64. @gaborhojtsy

×