Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Khalid475's avatar

How to use trans method in js file

Hello ... i would like to know how can i use trans method in js file . i have install mcamara package and i can use trans method in blade only like {{ trans('countries.Active') }}

i use the in js file but not working

thanks

  $(document).on("click", ".form-check-input", function () {
        let cb = document.getElementById("CountryStatus");
        let text = document.getElementById("CountryStatusText");
        if (cb.checked == true) {
            text.value = "'{{ trans('countries.Active') }}'";
            text.style.color = "green";
        } else {
            text.value = "'{{ trans('countries.NotActive') }}'";
            text.style.color = "red";
        }
    });
0 likes
12 replies
Khalid475's avatar

i used this code not working

  $(document).on("click", ".form-check-input", function () {
        let cb = document.getElementById("CountryStatus");
        let text = document.getElementById("CountryStatusText");
        if (cb.checked == true) {
            text.value = "'{{ trans('countries.Active') }}'";
            text.style.color = "green";
        } else {
            text.value = "'{{ trans('countries.NotActive') }}'";
            text.style.color = "red";
        }
    });
1 like
Sinnbeck's avatar

@Khalid475 you cannot use php in js files. Is that valid syntax for the package you are using? Link?

1 like
vincent15000's avatar

@Khalid475 If you write your JS code inside <script> tags in a blade file, I think that you can use this syntax.

text.value = "'{{ trans('countries.Active') }}'";
Sinnbeck's avatar

@Khalid475 js cannot parse php, so you need to pass all translations to js and have a helper function in js to read them. This what the package we shared is for

Please or to participate in this conversation.