SoFunction
Updated on 2025-04-11

Implement an intelligent multilingual Hello World server based on Flask

Introduction

In this article, we will explore how to use the Flask framework to create an intelligent multilingual Hello World server. This server can automatically detect the visitor's browser language settings and return the "Hello World" greeting in the corresponding language version, supporting more than 20 major world languages.

Technology stack

  • Python
  • Flask Framework
  • HTTP request header processing
  • Unicode string processing

Core functions

  • Automatic language detection
  • Multilingual support (supports 20 major languages)
  • Elegant downgrade handling (default in English)
  • Supports region-specific language variants (such as Simplified and Traditional Chinese)

Detailed implementation

1. Basic settings

First, we need to import the necessary Flask components and create an application instance:

from flask import Flask, request
app = Flask(__name__)

2. Multilingual support

We use dictionary data structures to store greetings in different languages, supporting multiple world-wide major languages:

HELLO_WORLD = {
    'en': 'Hello World',      # English    'zh': 'Hello World',       # Simplified Chinese    'zh-tw': 'Hello World',    # Traditional Chinese    'ja': 'The world of 'こんにちはone',   # Japanese    'ko': '안녕하세요 세계',  # Korean    'es': '¡Hola Mundo!',     # Spanish    'fr': 'Bonjour le Monde', # French    'de': 'Hallo Welt',       # German    'it': 'Ciao Mondo',       # Italian    'ru': 'Привет, мир',      # Russian    'pt': 'Olá Mundo',        # Portuguese    'nl': 'Hallo Wereld',     # Dutch    'pl': 'Witaj świecie',    # Polish    'tr': 'Merhaba Dünya',    # Turkish    'ar': 'مرحبا بالعالم',    # Arabic    'hi': 'नमस्ते दुनिया',    # Hindi    'th': 'สวัสดีชาวโลก',     # Thai    'vi': 'Chào thế giới',    # Vietnamese    'id': 'Halo Dunia',       # Indonesian    'el': 'Γειά σου Κόσμε'    # Greek}

3. Enhanced language detection implementation

The server implements language detection through the following steps:

  • Get the request headerAccept-Languageinformation
  • Try to match the complete language code (including regional variants)
  • If the complete match is not found, try the basic language code
  • Return to the corresponding greeting

Core code implementation:

@('/')
def hello():
    accept_language = ('Accept-Language', 'en')
    lang_code = accept_language.split(',')[0].lower()
    
    # Try the complete language code (e.g. zh-tw)    if lang_code in HELLO_WORLD:
        return HELLO_WORLD[lang_code]
    
    # If the complete code does not exist, try the main language code    main_lang = lang_code.split('-')[0]
    greeting = HELLO_WORLD.get(main_lang, HELLO_WORLD['en'])
    
    return greeting

4. Language Processing Processing

Get language information:

  • Extract Accept-Language from HTTP request header
  • If there is no language information, use English by default

Enhanced language code parsing:

  • Keep the complete language code (such as: zh-tw) for the first match
  • If no complete match is found, the basic language code is extracted (such as: zh)
  • Convert to lowercase to ensure a match
  • Extract preferred language (process multilingual priority)

Intelligent matching mechanism:

  • Preferred matching of complete languages ​​- Regional Code
  • Downgrade to basic language code
  • Finally downgraded to English

Server configuration

The server operation configuration is as follows:

(host='0.0.0.0', port=8000, debug=True)
  • host='0.0.0.0': Allow external access
  • port=8000: Service port settings
  • debug=True: Enable debug mode

Example of usage

Start the server:

python 

Access Example:

  • Chinese Simplified Browser: Return to "Hello, World"
  • Traditional Chinese browser: Return to "Hello, World"
  • Japanese browser: Return to "可んにちはsky"
  • Arabic browser: return to "مرحبا بالعالم"
  • Hindi browser: Return to "नमस्ते दुनिया"

Technical Highlights

Multi-level language matching: Supports complete language code and basic language code

Unicode support: Correctly handle various text systems

Modular design: Separate language configuration and business logic

Scalability: Easy to add new language support

Featured functions

1. Extensive language support

  • East Asian languages: Chinese, Japanese, Korean
  • European languages: English, French, German, Spanish, etc.
  • Middle Eastern Languages: Arabic
  • South Asian Languages: Hindi
  • Southeast Asian languages: Thai, Vietnamese, Indonesian

2. Special character processing

  • Supports right to left text (Arabic)
  • Supports special letters (Russian Cyrillic letters)
  • Support tone symbols (Vietnamese)
  • Supports special punctuation (Spanish exclamation mark)

Extension suggestions

  • Add more language variant support
  • Implement language switching interface
  • Add language preference memory function
  • Add response format (such as JSON)
  • Add language detection log
  • Implement language fallback mechanism
  • Add RTL (right to left) text support
  • Integrated translation API support

This is the end of this article about implementing an intelligent multilingual Hello World server based on Flask. For more related content on Flask multilingual Hello World server, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!