Global search and replace in a language pack?

Global search and replace in a language pack?

by Séverin Terrier -
Number of replies: 1

Hi,

I'd like to know how to do a global search & replace in a whole language pack, to harmonise some words.

For exemple, i'd like to replace all occurrences of 'Etudiant' by 'Étudiant', 'Etudiants' by 'Étudiants', and also '...' by '…' (the unicode character).

AMOS doesn't provide a search & replace function.

If i must change manually all occurrences, it's a big (and painful) work.

If i have to use some linux bash commands/scripts, i'd like to know what would be the best/easiest way to do that. The aim would be to generate the needed files (for plugins having to be updated), containing only changed strings, and then pack them all in a zip file.

Thanks in advance,
Séverin

In reply to Séverin Terrier

Re: Global search and replace in a language pack?

by Séverin Terrier -

Hi,

For french reading people, you'd better read this discussion.

Waiting for a search & replace feature in AMOS, i've tried this.

First wrote a little unix shell script to search some defined strings in a language pack:

# Define some constants
DIR_ORI=$PWD                           # Keep origin directory
DIR_LANG="/path/to/moodledata/lang/fr" # Language directory to search in

###############################################################################
# Define some functions
###############################################################################

# Errors in french lang pack : number
function LanguageErrorFRNumber
{
  # For defined strings
  for Text in "Etudiants" "Etudiant" "Etes" "A propos" \
  "« " " »" ", \.\.\." ",\.\.\." "; \.\.\." ";\.\.\." " \.\.\." "\.\.\." " …"
  do
    # Show number of each Text element
    Nb=`grep -r -n -e "$Text" . | wc -l`
    echo $Nb "times '$Text'"
  done
}

# Errors in french lang pack : details
function LanguageErrorFRDetail
{
  # For defined strings
  for Text in "Etudiants" "Etudiant" "Etes" "A propos" \
  ", \.\.\." ",\.\.\." " \.\.\." " …"
  do
    # Show details of each Text element (with filename and line number)
    Result=`grep -r -n -e "$Text" . | grep "string" | grep "';" | cut -c 3-`
    echo "$Result"
  done
}

###############################################################################
# Main program
###############################################################################

cd $DIR_LANG
LanguageErrorFRNumber
echo
LanguageErrorFRDetail
cd $DIR_ORI

When launched, this script shows things like:

8 times 'Etudiants'
15 times 'Etudiant'
2 times 'Etes'
3 times 'A propos'
266 times '« '
248 times ' »'
28 times ', \.\.\.'
1 times ',\.\.\.'
1 times '; \.\.\.'
4 times ';\.\.\.'
45 times ' \.\.\.'
350 times '\.\.\.'
3 times ' …'

and then some lines like:

facetoface.php:120:$string['deletesessionconfirm'] = 'Etes vous sûr(e) de vouloir supprimer cette session et toutes les inscriptions pour cette session?';
realtimequiz.php:126:$string['startnewquizconfirm'] = 'Etes-vous absolument sûr de vouloir interrompre la session de test courante pour en démarrer une nouvelle?';
theme_adaptable.php:28:$string['aboutme'] = 'A propos de moi';
theme_snap.php:28:$string['aboutcourse'] = 'A propos de ce cours';
These exemples are not all existing anymore, since i've made some corrections, that have been integrated.

When you want to do corrections, the second part only, with details, is useful.

You can redirect the script to a file (be sure to be in a directory with no .php files):

. MoodleErrorsLanguageFR.sh > translation.txt
Then you can make all needed changes in a unique file (translation.txt), for strings containing Text you searched previously. My script just consider strings all contained in the same line. Multiline strings are ignored.

When you've modified all needed strings, you can generate all .php files, with appropriate names:

awk -F\: '{print substr($0,index($0,$3)) >$1}' translation.txt
You then have to add a PHP tag on the first line, so that AMOS can take care of them:

sed -i '1i<?php' *.php
Finally create a zip file containing all .php files:

zip translation.zip *.php
You then (just) have to import the ZIP file in AMOS.

Unfortunatly, as wrote in this other discussion, AMOS needs you to specify a Moodle version, and strings belonging to plugins not supporting latest Moodle version won't be integrated with the other ones. You'll have to make multiple imports (specifying different versions ; and deleting non necessary php files each time you make a new import).

I've used this procedure yesterday to make several big changes, and i have other ones to do, and it's a great help.
I'm not a linux shell script guru, had to dig & try before finding this, and this can certainly be greatly improved (don't hesitate to do so).

Of course, AMOS import ZIP file taking care of several versions would be a great time saver. And also if AMOS could directly implement search & replace.

HTH,
Séverin