nnjeim / world

A Laravel package which provides a list of the countries, states, cities, currencies, timezones and languages.
MIT License
723 stars 97 forks source link

Country translations not working #87

Closed Faridleo1998 closed 19 hours ago

Faridleo1998 commented 1 week ago

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

nnjeim commented 1 week ago

@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.

Faridleo1998 commented 1 week ago

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

nnjeim commented 1 week ago

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);
    }
}
Faridleo1998 commented 1 week ago

How I use the setLocale function in filament php?

nnjeim commented 1 week ago

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()

Faridleo1998 commented 1 week ago

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

nnjeim commented 6 days ago

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.