Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue: #237 eTrans - Language Concept 2023 #238

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# OpenEuropa Webtools eTrans - Language Concept 2023

This Drupal 10 custom module provides a block that adds machine translation
capabilities for the preferred EU language of a user. It is based on the
eTrans - Language Concept 2023 from OpenEuropa Webtools.

More information about this concept can be found
[here](https://webtools.europa.eu/showcase-demo/resources/etrans/demo/links/demo/lc2023/lc2023_live.html).

## Features

When a user attempts to change the language of the page and the selected
language is one of the 24 languages supported by eTranslation, the user
stays on the same page. A banner for the selected language appears where
the user can use machine translation for this page.
The module also supports live translation.

## Dependencies

This module depends on the `oe_multilingual` module.

## How to use

1. Install the module as you would any other Drupal module.
2. Navigate to the block layout page (`/admin/structure/block`)
3. Locate the region where you want to place the block and click the
"Place block" button.
4. From the list of available blocks, find "OpenEuropa Webtools eTrans -
Language Concept 2023" and click the "Place block" button next to it.
5. Configure the block settings as needed and click "Save block".

The OpenEuropa Webtools eTrans - Language Concept 2023 block should now
appear on your site in the chosen region and provide machine translation
capabilities for the preferred EU language of a user.

## Configuration

The block has a number of settings that control its behavior:

- **Receiver**: The ID of a HTML element in which the eTrans component
will be rendered.
- **Domain**: The domain for translation, can be 'General text' or 'EU
formal language'.
- **Delay**: The time in milliseconds to delay rendering the translation.
- **Live translation**: When enabled, all pages visited by a user will be
automatically translated to the selected language until the user cancels
the translation process.
- **Include**: A list of CSS selectors indicating the page elements to be
translated.
- **Exclude**: A list of CSS selectors indicating page elements to be
excluded from the translation.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
block.block.oe_webtools_etrans_lc2023:
type: block_settings
label: 'OpenEuropa Webtools eTrans - Language Concept 2023 Block settings'
mapping:
receiver:
type: string
label: 'Receiver'
domain:
type: string
label: 'Domain'
delay:
type: integer
label: 'Delay'
source:
type: string
label: 'Source'
include:
type: string
label: 'Include'
exclude:
type: string
label: 'Exclude'
live:
type: boolean
label: 'Live'
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
(function ($, Drupal) {
"use strict";

Drupal.behaviors.oeWTlc2023Behavior = {
attach: function (context, settings) {
// Initialize jQuery objects
const $etransMessage = $(once('wt-ecl-etrans-translate-message', '.wt-ecl-etrans-message', context));
const $translateLink = $(once('wt-ecl-translate-link', '.wt-ecl-etrans-message a#_translate', context));
const $closeLink = $(once('wt-ecl-translate-close', 'button.wt-ecl-message__close', context));
const $defaultLanguage = $('a.oe-webtools-lc2023-default-language', context);

// Attach event handlers
if ($translateLink.length) handleTranslateLink($translateLink, $etransMessage, settings);
if ($etransMessage.length) handleEtransMessage($etransMessage, $closeLink, context);
if ($defaultLanguage.length) handleDefaultLanguage($defaultLanguage);

// Update language elements and add prefLang query parameter.
$(window, context).on('wtTranslationEnd', function () {
// Make sure we do not have a live cookie.
if ($wt && $wt.etrans && !$wt.etrans.getLiveCookie()) {
addPrefLanguageQueryParam(context);
}
});
}
};

// Translate link click event handler
function handleTranslateLink($translateLink, $etransMessage, settings) {
$translateLink.on('click', function (e) {
e.preventDefault();
$wt.etrans.translate("body", settings.oe_wt_etrans.preferred_language);
$etransMessage.hide();
});
}

// Etrans message event handlers
function handleEtransMessage($etransMessage, $closeLink, context) {
$(window, context).on('wtTranslationAbort wtTranslationStart', function (event) {
$etransMessage.toggle(event.type === 'wtTranslationAbort');
});

$closeLink.on('click', function () {
$etransMessage.hide();
removePrefLangFromUrl();
});
}

// Default language click event handler
function handleDefaultLanguage($defaultLanguage) {
$defaultLanguage.on('click', function () {
if ($wt && $wt.etrans.isTranslated()) {
$wt.etrans.removeLiveCookie();
}
});
}

// Function to remove 'prefLang' parameter from URL
function removePrefLangFromUrl() {
const url = new URL(window.location.href);
url.searchParams.delete('prefLang');
window.history.pushState({}, '', url);
}

// Add prefLang query parameter to links.
function addPrefLanguageQueryParam(context) {
$(context).find('a:not(.ecl-language-list__container a)').each(function () {
const href = $(this).attr('href');
if (href && href.startsWith('/') && !href.includes('prefLang=')) {
const newHref = appendPrefLang(href, $wt.etrans.toLanguage);
$(this).attr('href', newHref);
}
});
}

// Append 'prefLang' parameter to the URL.
function appendPrefLang(href, language) {
if (language) {
return href + (href.includes('?') ? '&prefLang=' : '?prefLang=') + language;
}
return href;
}

})(jQuery, Drupal);
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
name: OpenEuropa Webtools eTrans - Language Concept 2023
description: Provides a block that adds machine translation possibility for preferred EU language using Language Concept 2023
package: OpenEuropa Webtools
type: module
core_version_requirement: ^10
dependencies:
- oe_webtools:oe_webtools
- oe_multilingual:oe_multilingual
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
oe_wt_etrans_lc2023:
js:
js/oe-webtools-lc2023.js: {}
dependencies:
- core/jquery
- core/once
- core/drupal
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

/**
* @file
* Primary module hooks for oe_webtools_etrans module.
*/

declare(strict_types=1);

use Drupal\Core\Url;

/**
* Implements hook_theme().
*/
function oe_webtools_etrans_lc2023_theme() {
return [
'oe_webtools_etrans_lc2023' => [
'variables' => [
'oe_wt_etrans_script' => NULL,
'oe_wt_etrans_lc2023' => NULL,
],
],
];
}

/**
* Implements hook_language_switch_links_alter().
*/
function oe_webtools_etrans_lc2023_language_switch_links_alter(array &$links, $type, Url $url) {
$default_language_code = \Drupal::languageManager()->getDefaultLanguage()->getId();
foreach ($links as $lang_code => $link) {
if ($lang_code !== $default_language_code) {
continue;
}
// Set default language class.
$links[$default_language_code]['attributes']['class'][] = 'oe-webtools-lc2023-default-language';
if (isset($links[$default_language_code]['query']['prefLang'])) {
unset($links[$default_language_code]['query']['prefLang']);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
services:
oe_webtools_etrans_lc2023.etrans_service:
class: Drupal\oe_webtools_etrans_lc2023\Service\ETransService
arguments: [ '@entity_type.manager', '@language_manager' , '@module_handler']

oe_webtools_etrans_lc2023.etrans_eu_languages_event_subscriber:
class: Drupal\oe_webtools_etrans_lc2023\EventSubscriber\ETransLanguagesEventSubscriber
arguments : ['@stream_wrapper_manager', '@language_manager', '@entity_type.manager', '@oe_webtools_etrans_lc2023.etrans_service', '@block.repository']
tags:
- { name: event_subscriber }
Loading