2

I use translate.rainlab plugin for localization, but not sure how to translate flash messages for ajax form.

function onMailSend() {
    Mail::sendTo('name@example.com', 'contact.form', post());
    Flash::success('Message has been sent');
}
qwaz
  • 1,285
  • 4
  • 23
  • 47

2 Answers2

3

You can stick with using the Translate plugin's features, no need to use another translation mechanism.

use RainLab\Translate\Models\Message;

function onMailSend() {
    Mail::sendTo('name@example.com', 'contact.form', post());
    Flash::success(Message::trans('Message has been sent'));
}

This assumes that "Message has been sent" is the string in the default locale.

hlev
  • 551
  • 1
  • 7
  • 20
2

translate plugin is used for translating content front-end side, but for translating messages within code, its good idea to use locale lang messages.

suppose this is your site : http://octdev.local.com/demo/ajax (default lang is set to en)

then you can create plugin and within lang/en/lang.php file you can define translation messages

en lang file will be there and default content will be look like this

// lang/en/lang.php
<?php return [
    'plugin' => [
        'name' => 'TestPlugin',
        'description' => ''
    ]
];

you can access this messages any where

\Flash::success(\Lang::get('hardiksatasiya.testplugin::lang.plugin.name'));

hardiksatasiya.testplugin => plugin auther name . pluginname

lang.plugin.name => worked like array lang stands for file name (language) then get plugin array then its key name so in our case it will out put TestPlugin

now you can use new url : http://octdev.local.com/de/demo/ajax its in de

so you can create new lang file in your plugin directory lang/de/lang.php and put same above php code with translated messages

// lang/de/lang.php
<?php return [
    'plugin' => [
        'name' => 'TestPlugin In de',
        'description' => ''
    ]
];

and it will work. if you need whole document you can use this reference : https://octobercms.com/docs/plugin/localization

update if you think i only needed to do in one place you can do something like this (but not preferred way)

$locale = \Lang::getLocale();
switch($locale) {
  case 'en':
     \Flash::success('Message has been sent - EN');
     break;
  case 'de':
     \Flash::success('Message has been sent - DE');
     break;
  default:
     \Flash::success('Message has been sent - default');
}
Hardik Satasiya
  • 9,547
  • 3
  • 22
  • 40
  • 1
    So, in any way, I have to create a new separate plugin just to translate couple of messages and there's no simpler native way to do it, correct? – qwaz Dec 19 '17 at 11:45
  • 1
    If this is the only message you want to translate you can do it manually. \Lang::getLocale() will return you selected locale `de` or `en` now you can put switch..case or if else , and assign your flash message (added updated block in answer just check it once) – Hardik Satasiya Dec 19 '17 at 12:31
  • 1
    yeah, that will do it. Thank you so much for such an informative answer. – qwaz Dec 19 '17 at 12:58