How to Change CSS direction property of the input field automaticly if the user can use an language rtl or ltr

前端 未结 5 1021
失恋的感觉
失恋的感觉 2021-02-06 08:49

Example: if I use arabic language the text field direction will be rtl and if I want to write a new text and I switch to the English language the direction inside the text field

5条回答
  •  星月不相逢
    2021-02-06 08:59

    The first problem you are going to run into is that JavaScript cannot directly detect a user's language setting, that value only shows up in the HTTP 'Accept-Language' header, so you will have to do something server-side to get this value and pass it to the JavaScript. For example, in PHP:

    $headers = apache_request_headers();
    $ltr_languages = array("en-gb", "en-us", ...'); // a list of ltr languages to detect
    if (in_array($headers["Accept-Language"], $ltr_languages)) {
        // some JavaScript or CSS to set the text ltr;
    } else {
        // some JavaScript or CSS to set the text rtl;
    }
    

提交回复
热议问题