translate
translate copied to clipboard
Add custom translations
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
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?
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
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); } }
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.
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);
}
Ah cool!
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
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?
You are great! Thank you!
Only a comment.
If I add an occurrence and after this occurrence is also into a file, this occurrence appears twice.
Mmm I found some more bugs too, will work some more on it.