To retrieve the current user language code, you can utilize the navigator.language
property. Once obtained, you have the ability to globally modify the font or apply changes specifically to a collection of input elements.
It's worth noting that in Persian, there are two distinct language codes - "fa" for Persian and "fa-ir" for Persian/Iran.`
- "fa": "Persian",
- "fa-ir": "Persian/Iran"`
// Should more language codes be acquired
const langCodes = ['fa', 'fa-ir']
const checkLang = (languages) => (
navigator.language === languages
// Alternately, refer to - https://stackoverflow.com/a/55206806/13749957
)
langCodes.some(checkLang)
if (checkLang) {
changeFont()
// Function changeFont may implement global font alterations
// Alternatively, target a group of inputElements and adjust font styles
}
Further Details
Considering how keyboard layout could impact the
input language without affecting navigator.language
, @Peter Seliger highlights the necessity to speculate this by
devising a solution involving change
and onpaste
events to infer their keyboard
layout
An approach is outlined to predict Persian language characters post input (bear in mind it excludes other potential scenarios like paste operations) -
navigator.language
represents the browser's preferred language but could deviate from the system's os
language setting. As no standard API offers this information, discerning user error/preference remains challenging and falls within an edge case scenario - Refer to this comprehensive explanation