Jump to content

Method for outputting multiple string translations simultaneously


ZGD
 Share

Recommended Posts

I'm using PW to build an API which returns data in multiple languages at once. For example I might have the words "Hello" and "Goodbye" that need to be translated into several languages. The API (which returns data as JSON) needs to return something like the following...
 

{
  "hello": {
    "de": "Hallo",
    "en": "Hello",
    "fr": "Bonjour",
    "it": "Ciao",
    "tc": "你好"
  },
  "goodbye": {
    "de": "Auf Wiedersehen",
    "en": "Goodbye",
    "fr": "Au Revoir",
    "it": "Addio",
    "tc": "再見"
  }
}


Ideally I'd like to be able to use PW's template string translating feature, as there will be potentially hundreds of different translations and creating many fields seems the wrong way to go. Each translation will also need to have a note or description alongside it. I was originally thinking something like the example below may be possible, but it seems unlikely.
 

<?php
function _t($translation) {
  // logic to return all translations here...
  return $translation;
}

$translations = [
  'hello' => _t(__('Hello')), // A casual greeting message
  'goodbye' => _t(__('Goodbye')) // A casual departing message
];

echo json_encode($translations);


Can anyone suggest a good method for returning all translations of a translatable string? Is it even possible to access multiple translations of static strings within a template?

Alternatively, if anyone has solved a similar problem before, I'm open to suggestions.

Thanks!

Link to comment
Share on other sites

Managed to resolve this, code below for any that's interested.

My translations class:

<?php
namespace Translations;

class Translation {

    private function __construct() {}

    protected static function translations() {
      return [];
    }

    private static function initialize() {
        $languages = \ProcessWire\wire('languages');
        $user = \ProcessWire\wire('user');

        $translated = [];
        foreach($languages as $lang) {
          $user->language = $lang;

          foreach(static::translations() as $k => $t) {
            $translated[$k][$lang->language_code] = $t;
          }
        }

        return $translated;
    }

    public static function translate() {
      return static::initialize();
    }
}

include_once __DIR__ . '/greetings.php';
include_once __DIR__ . '/questions.php';


Now I can create as many different translation files as I like, for example greetings.php

<?php 
namespace Translations;

use function ProcessWire\__ as __;

class Greetings extends Translation {

    protected static function translations() {
      return [
        'hello' => __('Hello'), // A casual greeting message
        'goodbye' => __('GoodBye'), // A casual departing message
      ];
    }
}

and questions.php

<?php 
namespace Translations;

use function ProcessWire\__ as __;

class Questions extends Translation {

    protected static function translations() {
      return [
        'howareyou' => __('How are You?'),
        'howmuch' => __('How Much?'),
      ];
    }
}


So in my template I can now write:

<?php
use Translations\Greetings;
use Translations\Questions;

$greetings = Greetings::translate();
$questions = Questions::translate();

// ... output here

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...