AJAX Mail Translation problem

languages

#1

Salut!

Im sendig a html email by an ajax request. The mail needs to be localized. the function l() returns an empty string, so i tried to set the language for l() manually. That didn’t work either. Could someone explain me why l() isn’t working in the following code and how i could solve this?

From config.php routes-array:

'pattern'  => 'confirm',
'action'  => function() {
      $vars = array(
        'confirm' => l('confirm','',$_POST["lang"])
      );

  $email = email(array(
    ....
    'body'    => snippet('email-confirm', $vars, true, true),
    'service' => 'html-mail',
  ));

As i said, ‘confirm’ => l('confirm') doesnt work either.


#2

Don’t you need l::get() and l::set()?

More info: https://getkirby.com/docs/languages/variables


#3

l() is the short version for l::get(). Of course i do set the language array in the language/de.php file.


#4

I didn’t know that. I read up again and I noticed you’re using this inside a route. Kirby has a thing where you have to be explicit about which language you’re targeting via return site()->visit('some/page', 'de');. Maybe Kirby is lost, and doesn’t know which language file it needs to load?

More info: How is it possible to combine routing, variables and multi language support


#5

Yeah, i had the same idea and tried to pass as third parameter the right language. As you see in my first post, that didn’t work either. :slight_smile:


#6

This doesn’t work because the language variables aren’t available at the time the route is executed, no matter if you define your route in the config or in a plugin.

What you could do, however, is define your variables with c::set() instead of using language variables.

c::set('language.variables', [
  'en' => [
     'variable1' => 'whatever',
     'variable2' => 'something else'
   ],
  'de' => [
    'variable1' => 'was auch immer',
    'variable2' => 'was anderes'
    ]
]);

And then get the correct string via c::get(), for example:

c::get('language.variables')['de']['variable1'])

#7

What you could do, however, is define your variables with c::set() instead of using language variables.
Hey texnixe!

Thanks for your answer! That’s what i was being afraid of- that it’s not loaded yet. Your solution works, but i’m using l::get() all over the place, so your solution would split my translations into two places… Just interested- Is l() loaded when rendering templates or when does it happen?


Display alert notification in the panel
#8

If that’s the culprit, could you source the language file in the route?

E.g.

$file = $this->roots()->languages() . DS . $_POST["lang"] . '.php';
// load the file if it exists
if(file_exists($file)) include_once($file);

Access to language variables within the router
#9

Good idea, that does work.


#10

If that’s the culprit, could you source the language file in the route?

Yeah, hacky but it doesn’t split the translations. Especially when supporting lots of languages that sounds like a solution. :slight_smile:


#11

I wouldn’t say it’s hacky. It’s basically what Kirby does, only later in the bootstrap.

Lots of languages shouldn’t be an issue since you’ld need those languages files anyway…
Or what do you mean by “splitting the translations”?

I would sanitize the $_POST var though, or use the route argument variables instead :wink:


#12

If the language is sent in the post request, that’s probably easier to handle than the route arguments which can be a pain. But definitely sanitize or check if the var is in the language array.