Jump to content

Alternate language translation (respecting native PW translation)


OLSA
 Share

Recommended Posts

Hello for all.

This is not a topic about multilanguage websites, but mostly about one-language web sites and translation methods.

In one-language none-english projects I don't install "Language Support" modules, but still use static English phrases in files ("Read more"...), and later translate that phrases using some of many options (eg. method + arrays).

To simplifie this topic: if you build website in one none-english language (eg. Germany, Russian etc.) and, as example, want to use wonderfull "Pollino" Soma module, than you have to install "Language Support" module, or you will have "Vote" button at the form. And that's ok. But how to translate that module phrases without using "Language Support" modules? <- was my first idea.

Also, all this is in connection with my custom development where I want to standardise some things, like translation of static phrases.
 

Target:

Leave, and respect native PW call for all phrases in English (__("Some text")) and use that standard principle in all projects (one language, or multilanguage).

Idea: use hooks inside __() call and at the beginning, stop all next calls and procedures, and do some compare and replace job

Problem: there is no hookable method for that job ("wire/core/LanguageFunctions.php")

Currently working solution (example):

// _func.php
// prepend

// Call Language Support module
// You can comment next line if you have installed module
// , or this will do the same job
$module = wire('modules')->get('LanguageSupport');

// Here we rewrite response from module
function translation( HookEvent $event ){  
  $event->return = translate( $event->arguments(1) );
}

// These is the example where we do "find and replace" job
// You can use different variant, like include dict. file
function translate( $phrase ){
  $dictionary = array(
    'Read more' => 'Opširnije',
    'Total votings:' => 'Ukupno glasova: ',
    'Vote' => 'Glasaj',
    'Name' => 'Ime i prezime',
    'Message'=> 'Poruka',    
    'Subscribe' => 'Pošalji',
    'Missing required value' => 'Ovo polje je obavezno',
    'Next' => '>>',
    'Prev' => '<<'    
  );
  return isset($dictionary[$phrase]) ? $dictionary[$phrase] : $phrase;
}

// This is the main part where we hook 
wire()->addHookAfter('LanguageTranslator::getTranslation', null, 'translation');

I didn't avoid "Language Support" installation, but with this I can translate all website phrases in one place (good if you need to translate 20 - 30 views with repeatable phrases, eg. "Read more"). Also with this no need to use custom translation methods (use default PW call (__("Some text")).

p.s. also there is idea to hook append same 'textdomain' for all files/views

Regards,

Sasa

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

×
×
  • Create New...