1

I am using Laravel localization builtin to translate phrases.

In my view I have {{ trans('site.phrase') }}. Let's say I mispell the keyword and type:

{{ trans('site.prase') }} //without h

Now in my view I will have a: site.prase. Is there any way to obtain a warning from Laravel in the log ?

Edit

Following comments and the answer I am now using a simple custom helper:

// trans_safe
function transs( $key, $params = []) {
    $localized = trans($key, $params);
    if ($localized == $key) {
        \Log::warning("Key: {$key} doesn't exists with language: " . \App::getLocale() . ", url: " . \Request::fullUrl());
    }
    return $localized;
}

Now in the view I use: {{ transs('site.key) }}

giò
  • 3,402
  • 7
  • 27
  • 54
  • Since `trans()` will return the string provided if there's no available translation, you could simply check `if(trans($phrase) == $phrase)`, but this would require your knowing/defining the phrase beforehand. Just a thought. – Tim Lewis Oct 23 '15 at 15:15
  • @TimLewis: i am using basically that – giò Oct 24 '15 at 10:14

1 Answers1

3

Laravel's helper doesn't support this. You'll have to create your own helper for that:

function translate($id = null, $parameters = [], $domain = 'messages', $locale = null)
{
    $translator = app('translator');

    if (is_null($id)) {
        return app('translator');
    }

    if (! $translator->has($id, $locale)) {
        throw new \Exception('Translate key does not exist');
    }

    return app('translator')->trans($id, $parameters, $domain, $locale);
}

Where should I put this helper function? Glad you asked.

Community
  • 1
  • 1
Joseph Silber
  • 214,931
  • 59
  • 362
  • 292
  • interesting! I did something like that. Anyway what is $domain param for ? – giò Oct 23 '15 at 15:14
  • Don't worry about it. [The `trans` function doesn't even utilize it](https://github.com/laravel/framework/blob/aec203a2168a8286bf1fdaf1e90c0d19619b79ea/src/Illuminate/Translation/Translator.php#L182-L185). Probably there for legacy reasons. – Joseph Silber Oct 23 '15 at 15:17
  • 1
    i found a bug, $translator->has doesn't work if the fallback_locale returns a localized value – giò Oct 24 '15 at 11:16