nnjeim / world

A Laravel package which provides a list of the countries, states, cities, currencies, timezones and languages.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Country translations not working

Faridleo1998 opened this issue · comments

Describe the bug
I published the package config, I moved the translations (/resources/lang/vendor/world/*) to /lang folder in root project, but translations not loading.

I moved folder because I use filament php and shield packages and this package create /lang folder.

My .env file has APP_LOCALE=es

To Reproduce
Steps to reproduce the behavior:

  1. Execute php artisan vendor:publish --tag=world --force.
  2. Move /resources/lang/vendor/world to /lang in root project. result path: /lang/vendor/world/*

image

image

@Faridleo1998 hi and thank you for your input. My understanding is that the .env props are undefined in production and you should try to programatically set the locale using the config helper.

please check the code where the country translations is done and it will be clearer.

My project is local environment and it has .env file with language configuration but the countries names not show in spanish.

to set the locale you need to programatically set it not only .env

the package is set to translate the countries based on a locale header in the request when used as an API
if you are use the facades to consume the package
you can consider calling the setLocale function here below

$action = World::setLocale('es')->countries();

look at this parts of the code in the package

/**
	 * @param string $requestLocale
	 * @return $this
	 */
	public function setLocale(string $requestLocale): self
	{
		$setLocale = in_array($requestLocale, config('world.accepted_locales'), true)
			? $requestLocale
			: config('app.fallback_locale');

		app()->setLocale($setLocale);

		return $this;
	}
<?php

namespace Nnjeim\World\Http\Middleware;

use Illuminate\Http\Request;
use Closure;

class Localization
{
	/**
	 * @param  Request  $request
	 * @param  Closure  $next
	 * @return mixed
	 */
	public function handle(Request $request, Closure $next): mixed
	{
		$requestLocale = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'] ?? config('app.fallback_locale'), 0, 2);

		$setLocale = in_array($requestLocale, config('world.accepted_locales'), true)
			? $requestLocale
			: config('app.fallback_locale');

		app()->setLocale($setLocale);

		return $next($request);
	}
}

How I use the setLocale function in filament php?

How I use the setLocale function in filament php?

i guess you call the facade of the world package and then call the World::setLocale('localeString')->countries()

I trying use the facade but not working, I put in a service provider in boot method.

I trying use the facade but not working, I put in a service provider in boot method.

Please refer to the Readme file it shows an example how the facade is used.