Jump to content

Multilanguage module


Recommended Posts

Hi, I am creating my first module. I want it to be multilingual. Some presta modules on their configuration page give possibility to enter text for different languages (they have for example some kind of button to choose current language near textfield).

 

So I tried to do that, but I didn't manage. I know how to create simple form using HelperForm. But I don't have any idea how to add to this form multi language functionality. Can anybody help?

Link to comment
Share on other sites

You can use the following code to add a multi-language field in the HelperForm class:
 
array(
                       'type' => 'textarea',
                       'label' => $this->l('Enter small note'),
                       'name' => 'age_verification_note',
                       'hint' => $this->l('Enter the small note to be display at the top'),
                       'desc' => $this->l('Only 120 character allowed.'),
                       'class' => 'optn_general',
                       'lang' => true,
                       'required' => true,
                       'maxlength' => 120,
                       'cols' => 100,
                   )
 
 
Check following URL for reference:
 
Link to comment
Share on other sites

Hmm, I hoped it would be that simple

 

 

Check following URL for reference:
 

 

 

I hoped it would be that simple. But it isn't. When I add this attribute: 'lang' => true, my textarea becomes invisible. This is my input definition for this field:

array(
	'type' => 'textarea',
	'class' => 'rte autoload_rte rte',
	'label' => $this->l('Description'),
	'name' => 'html',
        'lang' => true,
	'required' => true
),
Link to comment
Share on other sites

OK, so I noticed that giving 'lang' attribute is not enought. I found out that I have to do some other things:

 

- get available languages, set default language and add them to HelperForm:

//this is not optimal, but it shows how to do this
$languages = Language::getLanguages(false);
foreach ($languages as $k => $language)
	$languages[$k]['is_default'] = (int)$language['id_lang'] == Configuration::get('PS_LANG_DEFAULT');
		
		
$helper->languages = $languages;

But still there is something wrong. When I open this page I get warnings:

 

Notice line 775 in file www\prestashop\tools\smarty\sysplugins\smarty_internal_templatebase.php(157) : eval()'d code
[8] Uninitialized string offset: 1

Notice line 775 in file www\prestashop\tools\smarty\sysplugins\smarty_internal_templatebase.php(157) : eval()'d code
[8] Uninitialized string offset: 2

 

And I just can't figure it out. Could anyone help? Please, it's killing me.

 

[EDIT]

 

Eureka! At last! Field that is to be multilingual must be filled with content for every language. And this field is an array of strings with keys representing language id. So one should do something like that:

foreach ($helper->languages as $language)
{
	$helper->fields_value['html'][$language['id_lang']] = "";
}

Of course this is bad example, because 'html' field will always have empty string. But I didn't figure it out yet, how to get proper values from existing object.

Edited by Juhas0 (see edit history)
  • Like 1
Link to comment
Share on other sites

  • 4 years later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...