0

I'm looking for solution to use magento underlying translation system to translate a default text from English to any custom language (not being store default language). Is it even possible?

In my scenario i would like to use translation module to set certain properties (store dependent) in product while creating it in code. Product can be created in two stores, and will be available for both, but default values must be always in one language, and store-specific values must be obviously in other language.

I want to use translation system because its convenient for editing. Unless its of course not possible.

3 Answers 3

3

You can use the locale emulation for this:

$storeId would be the ID of another store with the specified language that you want to use.

$locale = Mage::app()->getLocale();

$locale->emulate($storeId); //push current locale to stack and replace with locale from specified store

//your magic here

$locale->revert(); //use last locale before last emulation
5
  • Fatal error: Uncaught Error: Call to a member function emulate() on string
    – Deus777
    Commented Dec 8, 2016 at 14:40
  • 1
    Mage::app()->getLocale() solved the problem. Please update your answer.
    – Deus777
    Commented Dec 8, 2016 at 14:43
  • $locale->revert() doesn't work as expected, because for some unknown reasons it inits locale for adminhtml not frontend (line 760): Mage::getSingleton('core/translate')->setLocale($this->_locale)->init('adminhtml', true);
    – Deus777
    Commented Dec 9, 2016 at 13:34
  • Works perfectly on 1.9, thanks! Commented Oct 21, 2019 at 15:41
  • Solved it for me, thanks! Commented Feb 22 at 13:11
1

Try this:

$text = "Customer";
$module = "Mage_Customer";
$expr = new Mage_Core_Model_Translate_Expr($text, $module);
$translated = Mage::app()->getTranslator()->setLocale('pt_BR')->translate([$expr]);

Adapted from app/code/core/Mage/Core/Helper/Abstract.php, function __().

3
  • in my scenario I don't want to limit myself to any module, which is required here I suppose...
    – Deus777
    Commented Dec 9, 2016 at 13:36
  • If you use "Mage_Core" as the module and the specific CSV doesn't have the translation, it will probably fallback to any module which has the translation for the text you've specified.
    – Vinícius
    Commented Dec 12, 2016 at 18:28
  • And, actually, I'm pretty sure you can't go on translating Magento without a module, once it requires a .CSV file to store all the translations.
    – Vinícius
    Commented Dec 12, 2016 at 18:30
0

Finally I used simply:

Mage::getSingleton('core/translate')->setLocale($code)->init('frontend', true);

to switch between languages. At first I set default language. Then switch to another and make appropriate updates as suggested here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.