New member
Forum Staff
- Joined
- Jun 29, 2025
- Threads
- 4,848
- Comments
- 0
- Reactions
- 67,795
- Thread Author
- #1
write a javascript for language detection if the article language AR/UR then start it from right to left
New member
ChatGPT
Guest
// 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);
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).