You are here

protected function SvgEmbedProcess::embedTranslate in SVG Embed 2.0.x

Helper function called recursively to translate all strings in an SVG file.

Parameters

SimpleXMLElement $xml: the SVG graphic code

string $langcode: the language code to which we need to translate

1 call to SvgEmbedProcess::embedTranslate()
SvgEmbedProcess::translate in src/SvgEmbedProcess.php

File

src/SvgEmbedProcess.php, line 96

Class

SvgEmbedProcess
Class SvgEmbedProcess.

Namespace

Drupal\svg_embed

Code

protected function embedTranslate($xml, $langcode) : void {
  foreach ($xml as $child) {
    $this
      ->embedTranslate($child, $langcode);
    if (isset($child->text) || isset($child->tspan)) {
      if (isset($child->text->tspan)) {
        $text = $child->text->tspan;
      }
      elseif (isset($child->tspan)) {
        $text = $child->tspan;
      }
      else {
        $text = $child->text;
      }
      $i = 0;
      while (TRUE) {
        $string = (string) $text[$i];
        if (empty($string)) {
          break;
        }
        $string = trim($string);
        if (!empty($string)) {
          $query = $this->connection
            ->select('locales_source', 's');
          $query
            ->leftJoin('locales_target', 't', 's.lid = t.lid');

          /** @noinspection NullPointerExceptionInspection */
          $translation = $query
            ->fields('t', [
            'translation',
          ])
            ->condition('s.source', $string)
            ->condition('s.textgroup', 'svg_embed')
            ->condition('t.language', $langcode)
            ->execute()
            ->fetchField();
          $text[$i][0] = empty($translation) ? $string : $translation;
        }
        $i++;
      }
    }
  }
}