Search code examples
javascriptlaravelurlamcharts

sent laravel ( app()->getLocale() ) with url from JavaScript


I've added other languages ​​to the site I'm working on .

I make the the medalware :

    class SetLocale {
         public function handle($request, Closure $next)
                {
                   App()->setLocale($request->segment(1));
                   return $next($request);
                }
              }

the route :

  Route::group(
  ['prefix' => '{locale}', 
  'where' => ['locale' => 'en|fr|ar'],
  'middleware' => 'setlocale'],function ()
        {
           Route::get('mapData','HomPageController@mapData')->name('mapData');
        });

and everything works perfectly , but : I have a map from https://www.amcharts.com/ that stop working because I didn't figure out how to pass the

    app()->getLocale()

in side the cod of map that is in JavaScript language

      polygonSeries.dataSource.url = "mapData";  

So how can I pass the $langLocale in dataSource.url to be like this

polygonSeries.dataSource.url = "mapData",  app()->getLocale();

I made a way to make it work but this way it's static not dynamic this is what I did :

       polygonSeries.dataSource.url = "/en/mapData";

so is there any way to figure out what is the the language that comes from this code app()->getLocale(); and send it as parameter form JavaScript file


Solution

  • this is the answer : put the cod of JavaScript in function and just send it the value from php file

      <script>
    mapJs("  {{route('mapData',app()->getLocale())}}");
    </script>
    

    in the javascript file start the the code like this :

    function mapJs(value) {
      polygonSeries.dataSource.url = value;
     }