Asking write a javascript for language detection if the article language AR/UR then start it from right to left

Sure! Here is a sample JavaScript code snippet that detects the language of the article and adjusts the direction to right-to-left if the language is Arabic or Urdu:

JavaScript:
// Function to detect language and set text direction
function detectLanguageAndSetDirection(articleText) {
    const arabicPattern = /[\u0600-\u06FF\u0750-\u077F\u08A0-\u08FF\uFB50-\uFDFF\uFE70-\uFEFF]/; // Arabic Unicode range
    const urduPattern = /[\u0600-\u06FF\u0750-\u077F]/; // Urdu Unicode range

    let hasArabic = arabicPattern.test(articleText);
    let hasUrdu = urduPattern.test(articleText);

    if (hasArabic || hasUrdu) {
        document.dir = "rtl"; // Set text direction to right-to-left
    } else {
        document.dir = "ltr"; // Set text direction to left-to-right
    }
}

// Sample article text in Arabic
let arabicArticle = "هذا نص تجريبي باللغة العربية";

// Detect language and set direction
detectLanguageAndSetDirection(arabicArticle);

In this code snippet, the detectLanguageAndSetDirection function takes the article text as a parameter, checks if it contains Arabic or Urdu characters using regular expressions, and sets the text direction to right-to-left (rtl) if the text is in Arabic or Urdu. Otherwise, it sets the text direction to left-to-right (ltr).
 
Back
Top