Detect arabic or japanese language with out using franc

If you don't want to use external libraries like Franc for language detection, you can use this code. It only detects if the input text contains Arabic or Japanese characters. If both checks return false, the default language is assumed to be English.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Detect Arabic or Japanese Characters</title>
    <!-- Include jQuery 3.6.0 -->
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
    <div class="container">
        <!-- Text input field -->
        <input type="text" id="inputtext" placeholder="Enter text here" style="width: 300px; padding: 5px;">
        
        <!-- Buttons to check for Arabic or Japanese characters -->
        <button id="detectLanguageButton" style="padding: 5px 10px; margin-left: 10px;">Auto Detect</button>


        <br/><span id="result" style="margin-left: 10px; font-weight: bold;"></span>
    </div>

    <script>
        $(document).ready(function() {
            function containsArabic(text) {
                const arabicPattern = /[\u0600-\u06FF]/;
                return arabicPattern.test(text);
            }
            function containsJapanese(text) {
                const japanesePattern = /[\u3040-\u30FF\u4E00-\u9FFF]/;
                return japanesePattern.test(text);
            }

            $('#detectLanguageButton').on('click',function() {
                const inputText = $('#inputtext').val();
                if (containsJapanese(inputText)) {
                    $('#result').text('Japanese');
                } else if(containsArabic(inputText)) {
                    $('#result').text('Arabic');
                } else {
                    $('#result').text('English');
                }
            });


        });
    </script>
</body>
</html>



Additional Search keyword: how to detect language, automatic detect language arabic or nihongo, detect language with out using external Libraries

Last update on Aug 03, 2024

Tags: detect, language, arabic, nihongo, japanese

Back to Posts

Comments

No comments yet.

ForceTeach Corporation 2024