Laravel - specify default string / backup for multilingual

I am starting to add multilingualism to a site that uses Laravel for the interface.

However, due to lack of resources at the moment, I was translating lines when I go. It will be some time until the whole site is translated.

The only problem is that if the text is not translated, then what is displayed is the key. I would like to specify the default / fallback line for such.

eg.

{{ trans('site.'.$name) }}

If I pass "Business" as $ name and there is no translation for "Business" in the site.php lang file, I get site.Businessin the interface. It ruined everything. In the worst case, if not site.Business, Laravel should output Business.

Better yet, it should provide an option for the default / backup row.

Is it possible?

On the side of the note is there a free translation for common words? This will save time to translate everything.

Thank.

+4
source share
1 answer

A backup language is what you should use. See docs

You can also configure a “fallback language” to be used when the active language does not contain a given language line. As the default language, the backup language is also configured in the app/config/app.phpconfiguration file:

'fallback_locale' => 'en',

, , , , , . , , .

, , - . - .

, . , . helpers.php app. composer.json autoload files "app/helpers.php". , ? . . composer dump-autoload.

trans_fb(), trans(), . , ( ).

Laravel ( resources/lang/en/auth.php, auth.failed ), .

<?php

if (! function_exists('trans_fb')) {
    /**
     * Translate the given message with a fallback string if none exists.
     *
     * @param  string  $id
     * @param  string  $fallback
     * @param  array   $parameters
     * @param  string  $domain
     * @param  string  $locale
     * @return \Symfony\Component\Translation\TranslatorInterface|string
     */
    function trans_fb($id, $fallback, $parameters = [], $domain = 'messages', $locale = null)
    {
        return ($id === ($translation = trans($id, $parameters, $domain, $locale))) ? $fallback : $translation;
    }
}

:

{{ trans_fb("i.love.laravel", "I love Laravel!") }}

+2

All Articles