drupal 8's multilingual apis: building for the entire world

71
DRUPALCAMP GHENT 2016 GROW SOME IDEAS DRUPAL 8'S MULTILINGUAL APIS: BUILDING FOR THE ENTIRE WORLD Christian López Espínola - Lingotek

Upload: christian-lopez-espinola

Post on 12-Apr-2017

70 views

Category:

Technology


1 download

TRANSCRIPT

Page 1: Drupal 8's Multilingual APIs: Building for the Entire World

DRUPALCAMP GHENT 2016

GROW SOME IDEAS

DRUPAL 8'S MULTILINGUAL APIS: BUILDING FOR THE ENTIRE WORLD

Christian López Espínola - Lingotek

Page 2: Drupal 8's Multilingual APIs: Building for the Entire World

DRUPAL 8'S MULTILINGUAL APIS: BUILDING FOR THE ENTIRE

WORLD

Page 3: Drupal 8's Multilingual APIs: Building for the Entire World

DRUPALCAMP GHENT 2016

GROW SOME IDEAS

DRUPAL 8 MULTILINGUAL (setup, tips and tricks)

for a good user experience Tom Robert

Room Calibrate 17:10 - 17:40

Page 4: Drupal 8's Multilingual APIs: Building for the Entire World

penyaskitoChristian López Espínola

Drupal 8 Multilingual Initiative contributor

Page 5: Drupal 8's Multilingual APIs: Building for the Entire World
Page 6: Drupal 8's Multilingual APIs: Building for the Entire World
Page 7: Drupal 8's Multilingual APIs: Building for the Entire World

Drupal 7 MULTILINGUAL

Page 8: Drupal 8's Multilingual APIs: Building for the Entire World

Drupal CORE

Drupal 7 MULTILINGUAL

Page 9: Drupal 8's Multilingual APIs: Building for the Entire World

Drupal CORE

LOCALE

!

Drupal 7 MULTILINGUAL

Page 10: Drupal 8's Multilingual APIs: Building for the Entire World

Drupal CORE

L10n UP

LOCALE

!

m

Drupal 7 MULTILINGUAL

Page 11: Drupal 8's Multilingual APIs: Building for the Entire World

Drupal CORE

L10n UP

CONTENT TRANSLATION

LOCALE

!

Ü

m

Drupal 7 MULTILINGUAL

Page 12: Drupal 8's Multilingual APIs: Building for the Entire World

Drupal CORE

L10n UP

CONTENT TRANSLATION

LOCALE

I18N !

!

Ü

m

Drupal 7 MULTILINGUAL

Page 13: Drupal 8's Multilingual APIs: Building for the Entire World

Drupal CORE

L10n UP

CONTENT TRANSLATION

LOCALE

I18N

VARIABLE

"

!

!

Ü

m

Drupal 7 MULTILINGUAL

Page 14: Drupal 8's Multilingual APIs: Building for the Entire World

Drupal CORE

L10n UP

CONTENT TRANSLATION

LOCALE

I18N

VARIABLE

Entity translation

#

"

!

!

Ü

m

Drupal 7 MULTILINGUAL

Page 15: Drupal 8's Multilingual APIs: Building for the Entire World

Four pillars in Drupal 8

Page 16: Drupal 8's Multilingual APIs: Building for the Entire World

LANGUAGE

Base services for all modules dealing

with data. Not just multilingual.

!

Four pillars in Drupal 8

Page 17: Drupal 8's Multilingual APIs: Building for the Entire World

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

Page 18: Drupal 8's Multilingual APIs: Building for the Entire World

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

Page 19: Drupal 8's Multilingual APIs: Building for the Entire World

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

Page 20: Drupal 8's Multilingual APIs: Building for the Entire World

LANGUAGE INTERFACE CONTENT CONFIG

(! 8 é

Four pillars in Drupal 8

Page 21: Drupal 8's Multilingual APIs: Building for the Entire World

DEALING WITH LANGUAGE

\Drupal:languageManager()

LanguageManager

ConfigurableLanguageManager

Page 22: Drupal 8's Multilingual APIs: Building for the Entire World

DEALING WITH LANGUAGE

->getLanguages()UND: NOT SPECIFIED

ZXX: NOT APPLICABLE

EN: ENGLISH (DEFAULT)

Page 23: Drupal 8's Multilingual APIs: Building for the Entire World

UND: NOT SPECIFIED

ZXX: NOT APPLICABLE

HU: HUNGARIAN

IT: ITALIAN

DEALING WITH LANGUAGE

->getLanguages()

Page 24: Drupal 8's Multilingual APIs: Building for the Entire World

UND: NOT SPECIFIED

ZXX: NOT APPLICABLE

HU: HUNGARIAN

IT: ITALIAN

language.entity.$langcode.yml

DEALING WITH LANGUAGE

->getLanguages()

Page 25: Drupal 8's Multilingual APIs: Building for the Entire World

UND: NOT SPECIFIED

ZXX: NOT APPLICABLE

HU: HUNGARIAN

IT: ITALIAN

language.entity.$langcode.ymlLocked

DEALING WITH LANGUAGE

->getLanguages()

Page 26: Drupal 8's Multilingual APIs: Building for the Entire World

UND: NOT SPECIFIED

ZXX: NOT APPLICABLE

HU: HUNGARIAN

IT: ITALIAN

language.entity.$langcode.ymlLocked Not locked

DEALING WITH LANGUAGE

->getLanguages()

Page 27: Drupal 8's Multilingual APIs: Building for the Entire World

DEALING WITH LANGUAGE

ConfigurableLanguage::createFromLangcode(‘fr’)->save()

Page 28: Drupal 8's Multilingual APIs: Building for the Entire World

DEALING WITH LANGUAGE

ConfigurableLanguage::load(‘fr’)->delete()

Page 29: Drupal 8's Multilingual APIs: Building for the Entire World

DEALING WITH LANGUAGE

$languageManager->getCurrentLanguage()

Page 30: Drupal 8's Multilingual APIs: Building for the Entire World

LANGUAGE INTERFACE CONTENT CONFIG

(! 8 é

Four pillars in Drupal 8

Page 31: Drupal 8's Multilingual APIs: Building for the Entire World

INTERFACE LANGUAGE

Page 32: Drupal 8's Multilingual APIs: Building for the Entire World

INTERFACE LANGUAGE

t(‘English text’)

Page 33: Drupal 8's Multilingual APIs: Building for the Entire World

INTERFACE LANGUAGE

Page 34: Drupal 8's Multilingual APIs: Building for the Entire World

DEPENDENCY INJECTION

LOGIC

TRANSLATION

CONFIGURATIONUSER

Page 35: Drupal 8's Multilingual APIs: Building for the Entire World

DEPENDENCY INJECTION

LOGIC

TRANSLATION

CONFIGURATIONUSER

Page 36: Drupal 8's Multilingual APIs: Building for the Entire World

INTERFACE LANGUAGE

t(‘English text’)

Page 37: Drupal 8's Multilingual APIs: Building for the Entire World

INTERFACE LANGUAGE

t(‘English text’)$this->t(‘English text’)

Page 38: Drupal 8's Multilingual APIs: Building for the Entire World

INTERFACE LANGUAGE

class Foo { use StringTranslationTrait; …

}

Page 39: Drupal 8's Multilingual APIs: Building for the Entire World

INTERFACE LANGUAGE

Page 40: Drupal 8's Multilingual APIs: Building for the Entire World

INTERFACE LANGUAGE

$this->t(‘English text’)->getOption(‘langcode’);

Page 41: Drupal 8's Multilingual APIs: Building for the Entire World

INTERFACE LANGUAGE

format_plural(…)$this->formatPlural(…)

Page 42: Drupal 8's Multilingual APIs: Building for the Entire World

Javascript api

Drupal.t(…)Drupal.formatPlural()

Page 43: Drupal 8's Multilingual APIs: Building for the Entire World

TWIG TEMPLATES

<input {{ attributes }} placeholder="{{ 'Search'|trans }}" />

INPUT--SEARCH.HTML.TWIG

<div{{ author_attributes }}> {% trans %} Submitted by {{ author_name }} on {{ date }} {% endtrans %} </div>

NODE.HTML.TWIG

Page 44: Drupal 8's Multilingual APIs: Building for the Entire World

LOCALE.links.menu.ymllocale.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

Page 45: Drupal 8's Multilingual APIs: Building for the Entire World

LOCALE.links.menu.ymllocale.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

Page 46: Drupal 8's Multilingual APIs: Building for the Entire World

LANGUAGE INTERFACE CONTENT CONFIG

(! 8 é

Four pillars in Drupal 8

English to X

Page 47: Drupal 8's Multilingual APIs: Building for the Entire World

Node.php (Snippet)/** * Defines the node entity class. * * @ContentEntityType( * id = "node", * label = @Translation(“Content"), * translatable = TRUE, * entity_keys = { * "id" = "nid", * "label" = "title", * "langcode" = "langcode", * } * )

Page 48: Drupal 8's Multilingual APIs: Building for the Entire World

Node.php (Snippet)function baseFieldDefinitions($entity_type) { // … $fields['title'] = BaseFieldDefinition::create('string') ->setLabel(t('Title')) ->setRequired(TRUE) ->setTranslatable(TRUE);

}

Page 49: Drupal 8's Multilingual APIs: Building for the Entire World

FIELDS THEMSELVES

Page 50: Drupal 8's Multilingual APIs: Building for the Entire World

FIELDS THEMSELVES

AUTOMATED!

Page 51: Drupal 8's Multilingual APIs: Building for the Entire World

MULTICOLUMN FIELD SNIPPET * @FieldType( * id = "image", * column_groups = { * "file" = { * "label" = @Translation("File"), * "columns" = { * "target_id", "width", "height" * }, * }, * "alt" = { * "label" = @Translation("Alt"), * "translatable" = TRUE * }, * }

Page 52: Drupal 8's Multilingual APIs: Building for the Entire World

Entity language API$node = Node::load(42);

$node = $node ->getTranslation(‘hu’);

$node = $entityRepository ->getTranslationFromContext($node);

Page 53: Drupal 8's Multilingual APIs: Building for the Entire World

Entity language API$node->getUntranslated()

$node->language()

$node->getTranslationLanguages()

$node->hasTranslation(‘hu’)

$node->addTranslation(‘hu’)

$node->removeTranslation(‘hu’)

Page 54: Drupal 8's Multilingual APIs: Building for the Entire World

LANGUAGE INTERFACE CONTENT CONFIG

(! 8 é

Four pillars in Drupal 8

English to X X to Y

Intelligent objects

Page 55: Drupal 8's Multilingual APIs: Building for the Entire World

config/install/system.maintenance.yml

message: '@site is currently under maintenance. We should be back shortly. Thank you for your patience.' langcode: en

Page 56: Drupal 8's Multilingual APIs: Building for the Entire World

Core.data_types.schema.yml

config_object: type: mapping mapping: langcode: type: string label: 'Language code' …

text: type: string label: 'Text' translatable: true

Page 57: Drupal 8's Multilingual APIs: Building for the Entire World

config/schema/system.schema.yml

system.maintenance: type: config_object label: 'Maintenance mode' mapping: message: type: text label: 'Message to display…’

LANGCODE

TRANSLATABLE STRING

Page 58: Drupal 8's Multilingual APIs: Building for the Entire World

system.maintenance.yml

message: '@site is currently under maintenance. We should be back shortly. Thank you for your patience.' langcode: en

Page 59: Drupal 8's Multilingual APIs: Building for the Entire World

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

Page 60: Drupal 8's Multilingual APIs: Building for the Entire World

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…’

Page 61: Drupal 8's Multilingual APIs: Building for the Entire World

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

Page 62: Drupal 8's Multilingual APIs: Building for the Entire World

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 …’

Page 63: Drupal 8's Multilingual APIs: Building for the Entire World

configuration API

$config = \Drupal::config(‘system.maintenance’);

$config->get(‘message’);

Page 64: Drupal 8's Multilingual APIs: Building for the Entire World

configuration API

$config = \Drupal::config(‘system.maintenance’);

$config->get(‘message’);

OVERRIDES APPLY AS

APPROPRIATE

Page 65: Drupal 8's Multilingual APIs: Building for the Entire World

configuration API

$manager = \Drupal::languageManager(); $hu = $manager->getLanguage('hu');

$original = $manager->getConfigOverrideLanguage(); $manager->setConfigOverrideLanguage($hu);

$config = \Drupal::config(‘system.maintenance'); // …

$manager->setConfigOverrideLanguage($original);

Page 66: Drupal 8's Multilingual APIs: Building for the Entire World

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

Page 67: Drupal 8's Multilingual APIs: Building for the Entire World

LANGUAGE INTERFACE CONTENT CONFIG

(! 8 é

Four pillars in Drupal 8

English to X X to Y

Intelligent objects

X to Y

Dumb arrays

Page 68: Drupal 8's Multilingual APIs: Building for the Entire World
Page 69: Drupal 8's Multilingual APIs: Building for the Entire World
Page 70: Drupal 8's Multilingual APIs: Building for the Entire World

DRUPAL 8'S MULTILINGUAL APIS: BUILDING FOR THE ENTIRE

WORLD

Page 71: Drupal 8's Multilingual APIs: Building for the Entire World

DRUPALCAMP GHENT 2016

GROW SOME IDEAS