bobmyles
5 months ago

Laravel's localization on JavaScript Variables

Posted 5 months ago by bobmyles

I have some JavaScript variables that I output on a rating scale as shown below.

I now want to achieve Laravel's localization therefore translate the variables before outputting them on the browser.

How can I achieve this? I tried as below but it does not get converted.

var ratingScale = {1: 'Terrible, 2: 'Poor', 3: 'Average', 4: 'Good', 5: 'Excellent'}; 

I would want to Localize the variables Terrible, Poor, Average, Good, and Excellent.

I tried this

var one = "<?php echo trans('form.one'); ?>";
     
var ratingScale = {1: one, 2: 'Poor', 3: 'Average', 4: 'Good', 5: 'Excellent'};

But on the browser this is outputted <?php echo trans('form.one'); ?>

Anyone lead me here?

Please sign in or create an account to participate in this conversation.