Best practice for multilingual extensions
I wrote a small extension a while back. Now I want to extend it and make it multilingual. Sadly I could nowhere find any information about the recommended way of doing it.
Can someone give me some information about it? Is there a mandatory way of doing it? Or a recommended way?
Re: Best practice for multilingual extensions
As far as I know you make a new folder in your modules languages folder named with the two-letter country code you want(say "de" for german)....copy in the language files from your existing folder ("en" ??) then just translate the labels.
Look in system/modules/faq/languages for an example... its in german and english
Re: Best practice for multilingual extensions
OK. From what I get it is like this:
1) I create a directory "languages" in the extension root directory. In there I create for each language a new subdirectory.
2) In those directories I create files with the translations similar to this:
Code:
$GLOBALS['TL_LANG']['MSC']['faqCreatedBy'] = 'Last update on %s by %s.';
Here I have some questions:
- In this FAQ module I have seen that there are several language files. Do I have to make several ones and are there rules on how I have to name them?
- I have seen that sometimes arrays are given as translations. For what is this? I didn't get the concept behind it.
- Do I have to put everything into $GLOBALS['TL_LANG']['MSC'] or can I put it into $GLOBALS['TL_LANG']['foobar'] as well? Are there some rules I need to follow?
3) Then I have to but into the main file some commands to where the strings are given to the template. E.g. like this:
Code:
$this->Template->back = $GLOBALS['TL_LANG']['MSC']['goBack'];
And here I have some questions related to the ones above:
- How do I tell contao which file with the translation will be taken? I guess this is given somehow and I need to follow some rules on how to name the file. But I didn't find information about it.
4) Then in the template the translated string can be accessed like this:
And at the end another question. How can I define the fall back language? E.g. if my extension would not provide the fall back language defined by the administrator in contao?
Re: Best practice for multilingual extensions
Have you seen this?
http://dev.contao.org/projects/typol...rialsTutorials
Specifically http://dev.contao.org/projects/typol...rialsExtension and http://dev.contao.org/projects/typol...ewLanguagePack
Quote:
I wrote a small extension a while back.
How have you made this???... I thought you'd already have a languages/en folder by neccessity in it.
Re: Best practice for multilingual extensions
I used the "Extension creator" to create the base structure for the extension. And there was no language directory in it.
I did look at the two tutorials now but none of them answer my questions I had in the last post.
Re: Best practice for multilingual extensions
Quote:
In this FAQ module I have seen that there are several language files.
There are two, English and German.
Quote:
Do I have to make several ones and are there rules on how I have to name them?
The rule is countrycodes in the folder name ... languages/en, languages/de languages/fr etc. Only one is required (and I'm puzzled that you don't have one).
Quote:
I have seen that sometimes arrays are given as translations. For what is this? I didn't get the concept behind it.
The array will show in the backend as a label beside the field and a "tooltip" underneath the field.
Quote:
Do I have to put everything into $GLOBALS['TL_LANG']['MSC'] or can I put it into $GLOBALS['TL_LANG']['foobar'] as well? Are there some rules I need to follow?
If in your DCA folder you have a DCA file called tl_foobar.php and a field in it is called min_nights, it might look like this.
Code:
'min_nights' => array
(
'label' => &$GLOBALS['TL_LANG']['tl_foobar']['min_nights'],
'inputType' => 'text',
'eval' => array('mandatory'=>false, 'maxlength'=>2, 'rgxp'=>'digit', 'tl_class'=>'w50'),
'filter' => true,
'default' => '1'
),
In which case you would have a file BOTH in languages/en and languages/fr also called tl_foobar.php
and that would look like
Code:
$GLOBALS['TL_LANG']['tl_foobar']['min_nights'] = array('Minimum Booking', 'Will not show if 0 or 1');
in the en/ one and
Code:
$GLOBALS['TL_LANG']['tl_foobar']['min_nights'] = array('Le Boooking du Minumum', 'Nes pas l'view if 0 or 1');
in the fr/ one
Quote:
$GLOBALS['TL_LANG']['MSC']['goBack'];
is an already defined label for the goback button that you can use, or override if you want to define your own label
Note in the DCA/tl_foobar.php the label is defined
Code:
'label' => &$GLOBALS['TL_LANG']['tl_foobar']['min_nights']
If you wanted this label to say "Go Back" you could define it as
Code:
'label' => &$GLOBALS['TL_LANG']['MSC']['goBack']
Quote:
How do I tell contao which file with the translation will be taken? I guess this is given somehow and I need to follow some rules on how to name the file.
Not sure... If the users language is fr/ they'll see the french one, but if its not maybe it uses the defined fallback language.
Just guessing
Re: Best practice for multilingual extensions
Quote:
Originally Posted by ramjet
Quote:
Do I have to make several ones and are there rules on how I have to name them?
The rule is countrycodes in the folder name ... languages/en, languages/de languages/fr etc. Only one is required (and I'm puzzled that you don't have one).
I was asking about the files within those directorties. Not about the directories itself. I understood the rules behind the directory name.
Quote:
Originally Posted by ramjet
The array will show in the backend as a label beside the field and a "tooltip" underneath the field.
OK. Since I do not have a backend I do not need this. But good to know.
Quote:
Originally Posted by ramjet
Quote:
Do I have to put everything into $GLOBALS['TL_LANG']['MSC'] or can I put it into $GLOBALS['TL_LANG']['foobar'] as well? Are there some rules I need to follow?
If in your DCA folder you have a DCA file called tl_foobar.php and a field in it is called min_nights, it might look like this.
My question was partly answered. I do not have a database table for this extension. It is a pure frontend extension which is doing only some calculation. Can I use in my case $GLOBALS['TL_LANG']['foobar'] or is there another array I would have to put it in?
Quote:
Originally Posted by ramjet
Quote:
$GLOBALS['TL_LANG']['MSC']['goBack'];
is an already defined label for the goback button that you can use, or override if you want to define your own label
I already unerstood this. I only wanted to use this as an example to ask about the structure of the sub-arrays, e.g. if ['MSC'] is a fix term I need to use or not. You answered it with the answer above.
Quote:
Originally Posted by ramjet
Quote:
How do I tell contao which file with the translation will be taken? I guess this is given somehow and I need to follow some rules on how to name the file.
Not sure... If the users language is fr/ they'll see the french one, but if its not maybe it uses the defined fallback language.
Just guessing
This was for me a more or less general question. It would only happen if I would release the extension for others. Because the following situation could happen: I develop the extension with the languages German and English. But it will be used in a website where the front-end is only in French and Spanish? What would happen in this case?
Thank you for answering the questions till now.
Re: Best practice for multilingual extensions
Don't know sorry. Best to experiment in my experience.
Re: Best practice for multilingual extensions
Quote:
Originally Posted by modir
How do I tell contao which file with the translation will be taken? I guess this is given somehow and I need to follow some rules on how to name the file.
just name the file as you like and "load" it into your extension with (assuming you have a class that extends Module):
Code:
$this->loadLanguageFile('the_name_you_have_assigned_to_the_file_without_extension');
Quote:
Originally Posted by modir
I develop the extension with the languages German and English. But it will be used in a website where the front-end is only in French and Spanish? What would happen in this case?
all the strings that haven't a translation will be displayed in english
Re: Best practice for multilingual extensions
Hi ga.n,
Thank's for the information. This helps me a lot.