translate icon indicating copy to clipboard operation
translate copied to clipboard

Add custom translations

Open fernandoigual opened this issue 9 years ago • 11 comments

Hi.

I need add custom translations through a hook. These text are coming from a webservice and I need translate some keys.

How can I add this keys for translate?

Thanks in advance

fernandoigual avatar May 05 '15 09:05 fernandoigual

You can add custom sources that will parse templates and scripts, but that is not what you want. I think you'll want to inject an array with strings right?

boboldehampsink avatar May 06 '15 07:05 boboldehampsink

I need auto generate source messages ready for translating. I get it creating a file /craft/plugins/myplugin/translations/es.php with content:

'España', 'Morocco' => 'Marruecos', 'Italy' => 'Italia', ); Your plugin search for this script, add Morocco, Italy and Spain and has default values for Spanish translations. Thanks

fernandoigual avatar May 06 '15 15:05 fernandoigual

I would like something like this in my plugin:

public function registerCustomTranslations() { $dataService = craft()->myplugin_data; $offices = $dataService->getOfficesGroupedByCountryList("es"); foreach($offices as $office) { $country = $dataService->getCountryById($office->CountryId); \Craft\Craft::t($country); // OR craft()->translate->set('es', $country); } }

fernandoigual avatar May 06 '15 15:05 fernandoigual

Inserting a variable like that won't work. I'll work on something so you can inject the country list into the plugin for translatin.

boboldehampsink avatar May 06 '15 15:05 boboldehampsink

I get it with this in my plugin!

public function registerTranslateSources() { $dataService = craft()->myplugin_data; $offices = $dataService->getOfficeGroupedByCountryList("es"); $translations = array(); foreach(array_keys($offices) as $country) {
$countryName = $dataService->getCountryById($country); $translations[$countryName] = $countryName; } craft()->translate->set('es', $translations); }

fernandoigual avatar May 06 '15 15:05 fernandoigual

Ah cool!

boboldehampsink avatar May 06 '15 15:05 boboldehampsink

Sorry but, my code replace translations updated before... And occurrences aren't showed because they aren't into code...

I think the solution is create a new hook which merge file occurrences with custom in TranslateService::get:

registerCustomOcurrences

fernandoigual avatar May 06 '15 16:05 fernandoigual

Ok, i've added this to the develop branch:

// Get custom occurences
$custom = craft()->plugins->call('registerCustomOccurences', array($criteria));

// Loop through sources, see if we can get any data
foreach ($custom as $plugin) {
    if (is_array($plugin)) {
        foreach ($plugin as $occurence) {
            $occurences[] = $occurence;
        }
    }
}

It takes one parameter $criteria so you can return these occurences to the right source.

Could you test the develop branch?

boboldehampsink avatar May 06 '15 17:05 boboldehampsink

You are great! Thank you!

fernandoigual avatar May 06 '15 17:05 fernandoigual

Only a comment.

If I add an occurrence and after this occurrence is also into a file, this occurrence appears twice.

fernandoigual avatar May 06 '15 17:05 fernandoigual

Mmm I found some more bugs too, will work some more on it.

boboldehampsink avatar May 06 '15 18:05 boboldehampsink