Jump to content

[Solved] Create multilang objects on module install


Recommended Posts

Hi, I'm a newby and need help to understand the best way to solve a problem.


I'm working on a new module and I want it to insert some info in the database when it's installed. No problem.


But what about creating a new category, for example, inside the "install" method of module? I mean, I can create the object but, what about name in multiple languages? Should I just give it the name in the default language?



Edited by marcis (see edit history)
Link to comment
Share on other sites

You only have two options: You either build an array with the same category name for each language installed OR you have a lookup array that contains the category name in EVERY language available to Prestashop and pass the correct translation in each entry of the multi-lang array..... I would go for the first option and let the store owner sort out the translations ;)


I guess a compromise would be to have a lookup for the languages you support in your module and if a particular language isn;t found then use the default.

  • Like 1
Link to comment
Share on other sites

Thanks, Paul!


I want to support english and spanish, and can give the name in these languages. Should I create the name in the other languages (although it's in English, for example)? Or leave them empty?


What about other language (french, for example) being the default one? What will happen if the category has no name/translation in the default lang?



Link to comment
Share on other sites

You should only provide entries in the array for "active" installed languages. If you look at the AdminImport Controller you can see that the import code sets all the languages to just the single entry that's in the import CSV:


static function createMultiLangField($field)
       $languages = Language::getLanguages(false);
       $res = array();
       foreach ($languages AS $lang)
           $res[$lang['id_lang']] = $field;
       return $res;


To use the above you would do something like:


$category = new Category();

$category->name = createMultiLangField('my_category_name');


Note that until run time you have no idea which languages are installed. You could work out in the above loop if 'id_lang' relates to e.g. Spanish and if so use the Spanish translation but otherwise use English (assuming English is the default). If someone then has English, Spanish and French installed then the array will have three entries: English,Spanish,English. French users would then have to translate the category name from English themselves.

  • Like 1
Link to comment
Share on other sites

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...