Skip to content
This repository was archived by the owner on Apr 5, 2018. It is now read-only.

Add custom translations #11

Open
fernandoigual opened this issue May 5, 2015 · 11 comments
Open

Add custom translations #11

fernandoigual opened this issue May 5, 2015 · 11 comments
Assignees

Comments

@fernandoigual
Copy link

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

@boboldehampsink
Copy link
Owner

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?

@fernandoigual
Copy link
Author

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
Copy link
Author

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);
}
}

@boboldehampsink
Copy link
Owner

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.

@fernandoigual
Copy link
Author

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);
}

@boboldehampsink
Copy link
Owner

Ah cool!

@fernandoigual
Copy link
Author

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

@boboldehampsink
Copy link
Owner

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?

@fernandoigual
Copy link
Author

You are great!
Thank you!

@fernandoigual
Copy link
Author

Only a comment.

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

@boboldehampsink
Copy link
Owner

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants