Translation for $e->getMessage()

Hi People.

Is there any possibility to translate the error message of the related Exception?

try {

  page('mypage')->update(array(
    'title'        => 'A new title',
    'text'         => 'Some text',
    'anotherfield' => 'Some more data'
  ));

} catch(Exception $e) {

  echo $e->getMessage();

}

For example:

“The page UID exists” -> {YOUR_LANGUAGE}

Thanks in advance for the answer!

Yes, you can use the language variables here.
https://getkirby.com/docs/languages/variables

Well, I searched everywhere in the docs, even on this page. But as far as I can see, this won’t work with variables, because of the output of the error. Do you have an example how i can hook into the output of the error message with those variables?

I’m not sure I understand what you mean by:

Have you tried creating variables in your language files and echoing them?

try {
  page('mypage')->update(array(
    'title'        => l::get('custom_title'),
    'text'         => l::get('custom_text'),
    'anotherfield' => l::get('custom_other')
  ));
} catch(Exception $e) {
  echo $e->getMessage();
}

Simple example: I’m executing this code

try {
  $page->create('projects/project-d', 'project', array(
    'title' => 'A page title',
    'date' => '2016-02-21',
    'text' => 'Lorem ipsum dolor set amet ...'
  ));
} catch(Exception $e) {

  echo $e->getMessage();

}

But I’m getting an error, because the page “project-d” already exists. So the $e->getMessage() outputs “The page UID exists”. But I need it in german language. So how can I translate “The page UID exists”?

I was completely wrong in my understanding of your problem. My answer was quite stupid actually…

Doesn’t getMessage() return the error in the current language by default?
I actually can’t find where it’s defined in the code…

Edit: getMessage seems to be a PHP function and not a Kirby function. Trying to find where Kirby defines the error messages…

Edit 2: It’s English only from what I can see:

No problem mate. Maybe I was bad at explaining it :wink:

It is indeed a php function, my example is located in kirby/core/page.php

if($parent->children()->findBy('uid', $uid)) {
  throw new Exception('The page UID exists');
}

Edit: So it is written into Kirby Core. But there must be a possibility to translate it without editing the core files?!

Yes, it’s English only. Those error messages are probably not meant to be user-facing.

1 Like

That could be a reason. Then I will need to keep it generic. Thanks for your time!

You can actually do it with language variables:

  $page->create('projects/project-d', 'project', array(
    'title' => 'A page title',
    'date' => '2016-02-21',
    'text' => 'Lorem ipsum dolor set amet ...'
  ));
} catch(Exception $e) {

  l($e->getMessage());

}
```
And then define your variables in the language files:

```
l::set('The page UID exists',   'Die Seiten-UID existiert bereits');
```
You would have to find out what sort of error messages exist and could possibly be thrown. Maybe use an if statement to provide a fallback if there is a message you haven't covered.
1 Like

That’s super smart !

Indeed, awesome. Maybe this could be a good info for the official doc?

Nevertheless: THANK YOU @texnixe :raised_hands: :+1: