Sambhav
Sambhav

Reputation: 54

Laravel Localization does not return translated string

I can't seem to figure out what I'm doing wrong. I want my application to use "np" as its default locale. So I change the locale key in config/app.php to 'np.' And when I check for the current location in my controller, it returns 'np', so it's working fine until here. Then I created a "np.json" file directly inside the lang directory, which has the following content:

{
    "Candidate": "उम्मेदवार"
}

Now when I try to return the translated string using:

__('Candidate')

It returns "Candidate" instead of "उम्मेदवार", even if the current locale function still returns "np". So I ran the following commands trying to clear the cache.

php artisan optimize:clear
php artisan config:clear
php artisan cache:clear

But still, the issue persists.

Upvotes: 2

Views: 6751

Answers (2)

Vlad Anoxun
Vlad Anoxun

Reputation: 88

Try moving the lang/ folder with the translations.

If the lang/ folder is located in the root of the program, move it to the resources/ folder.

Otherwise, try moving lang/ to the root of the project.

This is due to the version of Laravel you are using.

Upvotes: 1

Paladin
Paladin

Reputation: 1637

You have to call the translations like <filename>.translation_string, and that file has be placed inside a folder with your language code.

In your case, create a folder inside lang called np, then place a translation file (example: mystrings.php) in it, so you have this file in a folder lang/np/mystrings.php.

Your trans file looks like

<?php
return [
  'Candidate' => 'उम्मेदवार',
];

Now, you have to set the lang to your language np with app()->setLocale('np'); or you set it directly in your .env and now you can call

echo __('mystrings.Candidate');

to get your translation.
Hope it helps

Upvotes: 0

Related Questions