Skip to content

Commit

Permalink
Merge branch 'SLB-486-translation-improvements' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
chindris committed Nov 18, 2024
2 parents df95bff + c0030f3 commit 3dbc860
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
8 changes: 8 additions & 0 deletions packages/drupal/custom/directives.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,11 @@ Provided by the "custom" module.
Implemented in "Drupal\custom\Plugin\GraphQL\Directive\PEntityEditLink".
"""
directive @resolveEntityEditLink repeatable on FIELD_DEFINITION | SCALAR | UNION | ENUM | INTERFACE | OBJECT

"""
Resolves the translated strings.
Provided by the "custom" module.
Implemented in "Drupal\custom\Plugin\GraphQL\Directive\DrupalTranslatedStrings".
"""
directive @drupalTranslatedStrings repeatable on FIELD_DEFINITION | SCALAR | UNION | ENUM | INTERFACE | OBJECT
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Drupal\custom\Plugin\GraphQL\Directive;

use Drupal\Core\Plugin\PluginBase;
use Drupal\graphql\GraphQL\Resolver\ResolverInterface;
use Drupal\graphql\GraphQL\ResolverBuilder;
use Drupal\graphql_directives\DirectiveInterface;

/**
* @Directive(
* id = "drupalTranslatedStrings"
* )
*/
class DrupalTranslatedStrings extends PluginBase implements DirectiveInterface {

public function buildResolver(ResolverBuilder $builder, array $arguments): ResolverInterface {
/* @var \Drupal\locale\StringDatabaseStorage $stringStorage */
$stringStorage = \Drupal::service('locale.storage');
// We are only interested in the translated strings that belong to the
// website context.
$translations = $stringStorage->getTranslations(['context' => 'website', 'translated' => TRUE]);
return $builder->fromValue(array_map(function($item) {
return [
'__typename' => 'DrupalTranslatableString',
'source' => $item->source,
'language' => $item->language,
'translation' => $item->translation,
];
}, $translations));
}

}
1 change: 1 addition & 0 deletions packages/schema/src/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,7 @@ type Query {

stringTranslations: [TranslatableString!]
@gatsbyNodes(type: "TranslatableString")
@drupalTranslatedStrings
}

type Mutation {
Expand Down

0 comments on commit 3dbc860

Please sign in to comment.