4

It is beautifully described in the docs how to translate static text using translation files.

As the client has no access to that file and managing a php array is not what I want them to do, I am looking for a way to make edits to the translation file feasible directly in the Control Panel.

This would allow me to set things up in the template and then hand off the actual translation work to someone else.

Yes, you probably have to write a plugin for that. But can you edit the files from a plugin? Is there any API or service available that might help?

Lindsey D
  • 23,974
  • 5
  • 53
  • 110
carlcs
  • 36,220
  • 5
  • 62
  • 139

2 Answers2

5

I'm pretty new to Craft but I'll tell you what I'd try anyway for fun.

I'd create a "Translations" assets source that would hold files like es.csv.

Then, in craft/translations/es.php, I'd run through that CSV and build the array Craft wants and return it.

<?php

$path = $_SERVER['DOCUMENT_ROOT'].'/../craft/translations/translations.csv';
$delimiter = ';';
$skip_rows = 1; //skip header rows
$primary_locale_column = 0; //set primary locale column
$this_locale_column = 1; //set secondary locale column for this `lang.php` file

$translations = array();
$translations_file = fopen($path, 'r');

if ($translations_file) {
    while(($translation = fgetcsv($translations_file, 1024, $delimiter)) !== FALSE) {
        if($skip_rows != 0) { $skip_rows--; continue; }
        $translations[$translation[$primary_locale_column]] = $translation[$this_locale_column];
    }
}

return $translations;

This is totally untested.

carlcs
  • 36,220
  • 5
  • 62
  • 139
Bill Criswell
  • 962
  • 1
  • 10
  • 14
  • Wow Bill, this is what I'd call a creative answer! :) Thanks. Will definitely try that (so you don't have to, hehe). A csv is without a doubt better then a php-array, but actually I was hoping for some nice html form or sth. Thanks again! – carlcs Jun 15 '14 at 02:52
  • No problem! Hopefully it can help you get to a solution you want. – Bill Criswell Jun 15 '14 at 03:10
  • That's a very clever solution! I'm curious to hear if this works... Please let us know @ChristianSeelbach! – Lindsey D Jun 15 '14 at 03:14
  • Just tested this and it works like a charm, Bill & @Lindsey! – carlcs Aug 06 '14 at 08:43
  • 1
    Very nice side effect of this technique is, that you can now manage all your translations in one csv file. Made some edits to the code to make this more easy. Hope you didn't mind, Bill! – carlcs Aug 06 '14 at 09:40
  • I do not mind at all! I'm glad it worked out. How did you get them all into one sheet? Just a lang column? – Bill Criswell Aug 06 '14 at 13:10
  • Yes. Just copy the script into each lang.php file (es.php, fr.php, de.php, etc.) and change the value of $this_locale_column. – carlcs Aug 06 '14 at 15:33
  • This solution is working great for me. I'm wondering if there is a way to adapt the code so that it could allow for comments and empty lines within the CSV file storing the static translations. The ability to add a few comments and add some spacing between groups of translated items would be great. So far, any attempt at comments or empty lines has broken the functionality. I don't know much php. – Justin K Jun 17 '16 at 17:21
0

I assume this is for a language that the CP hasn't been translated into, yet?

If so, maybe the better solution here is to help us get the CP translated into that language at which point we'll officially start supporting it in future releases. We're currently managing our translations on PhraseApp (http://phraseapp.com) and gladly accept contributions.

Brad Bell
  • 67,440
  • 6
  • 73
  • 143
  • Ouups Brad, didn't talk about stings of the CP but static stings in the template being translated with |translate filter. Added a link to make this clearer. Sometimes having trouble to articulate what I want in english O.o ! – carlcs Jun 15 '14 at 02:35
  • Ahh... no worries. I can guarantee you that your English is far superior my attempt at your native language. – Brad Bell Jun 15 '14 at 02:38
  • lol you speak german? – carlcs Jun 15 '14 at 02:41
  • 2
    Ich bin ein Berliner! – Brad Bell Jun 15 '14 at 02:48
  • definitely a bun -- though I had a quite educated man assure me that what John Kennedy said was 'perfectly fine'. For the spirit of it, unquestionably. We forget too easily, under our own propaganda -- of certain parties. – narration_sd Jun 15 '14 at 03:26