1

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.

Karl Hill
  • 12,937
  • 5
  • 58
  • 95
Sambhav
  • 46
  • 5
  • You need to send first, `app()->setLocale('np');` then try – STA Sep 19 '22 at 08:30
  • Double check to confirm that the file is readable try `dd(File::isReadable(lang_path('np.json')))` in the controller – apokryfos Sep 19 '22 at 09:26
  • 1
    @apokryfos ah yes therein lies the issue. File::isReadable(lang_path('np.json')) returned false. It looks like the lang_path() was the lang folder inside resources directory. I don't know if I caused this or if this is the default path. Anyway, putting the np.json file inside the resources/lang directory worked. So thank you for your help. – Sambhav Sep 19 '22 at 12:19
  • 1
    The issue is that until Laravel 8 the language path was indeed under resources and in Laravel 9 it moved **but** to not break older applications during migration it does still default to /resources/lang and only if that does not exist does it use /lang. Presumably the app you are working on previously used an older version and was upgraded (as is common). You can move that whole folder to /lang if you want or keep it where it is, as long as you are aware where it is. – apokryfos Sep 19 '22 at 12:46
  • @apokryfos Thank you for the explanation. It looks like one of the package that I used created the resources/lang folder when I published it's resource files. – Sambhav Sep 20 '22 at 10:16

1 Answers1

0

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

Paladin
  • 1,637
  • 13
  • 28