SoFunction
Updated on 2025-04-08

Python's operating guide to using fastapi to achieve multilingual internationalization

Guide to implementing multilingual internationalization

Project multilingual architecture

Technology stack

  • Translation Management: SQLAlchemy
  • Translation Generation: Babel & Baidu Translate API
  • Front-end localization: Jinja2 internationalization extension
  • Conversion tool: OpenCC (Simplified and Traditional Chinese Conversion)

Directory structure

web/
├── locales/                # Language Resource Directory│   ├── zh/                 # Chinese (Simplified Chinese)│   ├── cht/                # Chinese (Traditional)│   ├── en/                 # English│   └── ...                 #Other languages├── scripts/
│   └── generate_po.py      # Translate file generation script└── utils/
    └──         # Baidu Translation Tool    └──   # Chinese conversion tool

Translation workflow

1. Translation data storage

databasetranslationsTable structure:

  • msgid: Original text (Chinese)
  • msgstr: Translate text
  • lang_code: Language code

2. Translate and generate script generate_po.py

Main functions

  • Get original translated text from database
  • Automatic translation using Baidu Translation API
  • Generate.podocument
  • Supports specified language or full generation

How to use

# Generate all language translationspython generate_po.py

# Generate language-specific translationspython generate_po.py en

3. Front-end localization Jinja2

Template usage

<!-- use _() Functions for text localization -->
<span>{{ _('username') }}</span>

4. Language switching mechanism

Routing processing

@("/change_language")
def change_language(request: Request, lang: str):
    # Set language cookies or sessions    ['lang'] = lang

HTML localization implementation

1. Basic localization syntax

In HTML templates, use{{ _('text') }}Perform text localization.

Example: User Profile Page

<div class="info-item">
    <span class="info-label"> {{ _('Account Type') }} :</span>
    <span>
        {{ _('administrator') if user.is_admin else _('Ordinary User') }}
    </span>
</div>
<div class="info-item">
    <span class="info-label"> {{ _('gender') }} :</span>
    <span>
        {{ _('male') if  == '1' else _('female') if  == '0' else }}
    </span>
</div>

2. Conditional localization

You can use localized functions in conditional statements, as shown in the above example.

3. Message page localization

<form action="" method="">
    <div class="mb-3">
        <input type="hidden" name="id"  value="0">
        <label for="content" class="form-label">{{ _('Inquiry') }} </label>
        <textarea class="form-control"  name="content" rows="3" required></textarea>
    </div>
    <button type="button" class="btn btn-primary">{{ _('submit') }} </button>
</form>

4. JavaScript localization

In JavaScript, localized text can be passed through server rendering.

Example: Message Page JS Localization

$(function(){
    $('.btn-primary').on('click', function(e) {
        $.ajax({
            url:'/addcomment',
            type: 'POST',
            data: (),
            success: function(data) {
                if ( === 'success') {
                    ();
                } else {
                    // Localization error message passed on by server side                    ();
                }
            }
        });
    });
});

5. Localization best practices

  • Always use_()Function wraps the text that needs to be translated
  • Avoid hard-coded text in JavaScript
  • Passing localized text using server-side rendering
  • Provide context comments for complex texts

6. Common Traps

  • Do not localize technical text such as URLs, paths, etc.
  • Pay attention to maintaining consistency in HTML structure
  • Consider text length variations in different languages

Baidu Translation Tool

Functional Overview

baidu_translate()is a powerful translation function that provides the following core functions:

  • Call Baidu Translation API for text translation
  • Supports multiple language conversion
  • Built-in error handling and security mechanisms

Implement code

def baidu_translate(query, from_lang='zh', to_lang='en'):
    """Calling Baidu Translation API for translation"""
    # Security check: Skip specific types of text    if (r'^/?https?://.+\.(jpg|png|jpeg|gif|bmp|mp3|wav|ogg|mp4|avi|mov|wmv|flv|mkv)$', query):
        return query
    elif (r'^\d{1,11}@[\w.-]{2,30}\.[\w.-]{2,10}$', query):
        return query
    
    # Generate the signature required for Baidu Translation    salt = (32768, 65536)
    sign_str = f"{app_id}{query}{salt}{secret_key}"
    sign = hashlib.md5(sign_str.encode('utf-8')).hexdigest()
    
    # Call the translation API    params = {
        'q': query,
        'from': from_lang,
        'to': to_lang,
        'appid': app_id,
        'salt': salt,
        'sign': sign
    }

Use scenarios

1. Basic translation

# Chinese to Englishenglish_text = baidu_translate("International Marriage Network", from_lang='zh', to_lang='en')
# Result: "International Marriage Network"
# English to Chinesechinese_text = baidu_translate("Hello World", from_lang='en', to_lang='zh')
# Results: "Hello, the world"

2. Multilingual support

# Support multiple language conversionjapanese_text = baidu_translate("Hello", from_lang='zh', to_lang='jp')
french_text = baidu_translate("Hello", from_lang='en', to_lang='fra')

Safety and performance characteristics

Security mechanism

  1. Enter verification: Skip file paths, mailboxes and other specific format texts
  2. Signature generation: Prevent unauthorized calls
  3. Error handling: Detailed error code mapping

Error code mapping

error_map = {
    '52003': 'Unauthorized user',
    '54000': 'Required parameters are blank',
    '54001': 'Signature error',
    '54003': 'Access frequency is limited',
    '54004': 'Insufficient Account Balance',
    '54005': 'Long query requests frequently',
    '58000': 'Client IP is illegal',
    '58001': 'The translation language direction is not supported'
}

Configuration and dependencies

Environment variables

# .env file configurationBAIDU_TRANS_APPID=your_app_id
BAIDU_TRANS_SECRET_KEY=your_secret_key
BAIDU_TRANS_HOST=/api/trans/vip/translate

Dependency library

  • requests: Send HTTP request
  • hashlib: Generate MD5 signature
  • python-dotenv: Loading environment variables

Best Practices

  1. Cache translation results: For repeated translations, consider using cache
  2. Limit translation frequency: Avoid exceeding API call limits
  3. Process long text: Translate large text in segments
  4. Alternative translation solution: Prepare for alternative translation services

Things to note

  • API calls have frequency and quota limits
  • Translation quality depends on Baidu Translation API
  • Sensitive or professional texts require manual proofreading
  • Secure API keys

Alternatives

  1. Google Translate API
  2. DeepL Translation API
  3. Local translation dictionary
  4. Manual translation services

Chinese conversion tool

ChineseConverter Tools

Functional Overview

ChineseConverterIt is a powerful Chinese text conversion tool that provides the following core functions:

  • Simplified Chinese ↔ Traditional Chinese Transfer
  • Chinese text to pinyin
  • Supports multiple pinyin styles

Implement code

class ChineseConverter:
    def __init__(self):
        self.s2t = OpenCC('s2t')  # Simplified to Traditional Chinese        self.t2s = OpenCC('t2s')  # Traditional to Simplified
    def to_traditional(self, text: str) -> str:
        """Convert Simplified Chinese to Traditional Chinese"""
        return self.(text)

    def to_simplified(self, text: str) -> str:
        """Convert Traditional Chinese to Simplified Chinese"""
        return self.(text)

    def to_pinyin(self, text: str, style=) -> list:
        """
         Convert Chinese to pinyin
         Supports multiple pinyin styles
         """
        return pinyin(text, style=style)

    def to_pinyin_str(self, text: str, separator=' ', style=) -> str:
        """Convert Chinese to Pinyin String"""
        py_list = self.to_pinyin(text, style=style)
        return ([''.join(p) for p in py_list])

Pinyin style options

: Normal style, without tone

converter.to_pinyin_str("International Marriage Network")
# Output: guo ji hun yin wang

: Pinyin with tone

converter.to_pinyin_str("International Marriage Network", style=)
# Output: guó jì hūn yìn wǎng

Style.TONE2: Digital tone

converter.to_pinyin_str("International Marriage Network", style=Style.TONE2)
# Output: guo2 ji4 hun1 yin4 wang3

Style.FIRST_LETTER: First letter

converter.to_pinyin_str("International Marriage Network", style=Style.FIRST_LETTER, separator='')
# Output: gjhyw

Use scenarios

1. Simplified and traditional Chinese conversion

# Simplified Chinese to Traditional Chinesetraditional_text = converter.to_traditional("International Marriage Network")
# Result: International Marriage Network
# Traditional Chinese to Simplified Chinesesimplified_text = converter.to_simplified("International Marriage Network")
# Result: International Marriage Network

2. Pinyin conversion

# Get pinyin with tonepinyin_with_tone = converter.to_pinyin_str("International Marriage Network", style=)
# Results: guó jì hūn yìn wǎng

Performance and dependencies

  • Dependency library

    • opencc: Handle the simple and traditional transformation
    • pypinyin: Handle pinyin conversion
  • Performance Features

    • Lightweight conversion
    • No need to call external API
    • Fast conversion speed
    • Low memory footprint

Best Practices

Global Singleton Mode

# It is recommended to create a global converter instanceconverter = ChineseConverter()
  1. Avoid repeated creation of converter instances

  2. Choose the right pinyin style

  3. Handle special characters and non-Chinese text

Things to note

  • Some uncommon characters may be inaccurately converted
  • Dialect conversion is not supported
  • Only Simplified and Traditional Chinese are supported

Alternatives

  • For large-scale and high-precision conversions, consider professional translation services
  • For domain-specific terms, manual proofreading is recommended

Translation data model

Database table structure translations

class Translation(Base):
    __tablename__ = 'translations'
    
    # Primary key    id = Column(Integer, primary_key=True, autoincrement=True)
    
    # Original text identifier    msgid = Column(String(255), nullable=False)
    
    # Translated text    msgstr = Column(String(255), nullable=False)
    
    # Language Code    lang_code = Column(String(10), nullable=False)
    
    # Create a timestamp    created_at = Column(DateTime, server_default=())
    
    # Last updated time stamp    updated_at = Column(DateTime, server_default=(), onupdate=())

    # Unique constraint: Make sure that each msgid has only one translation in a specific language    __table_args__ = (
        UniqueConstraint('msgid', 'lang_code', name='unique_translation'),
    )

Detailed explanation of fields

  1. id

    • Primary key, self-increase
    • Uniquely identify each translation record
  2. msgid

    • Original text identifier
    • Usually Chinese (source language) text
    • Maximum length 255 characters
  3. msgstr

    • Translated text
    • Translation results for specific languages
    • Maximum length 255 characters
  4. lang_code

    • Language codes (such as ‘zh’, ‘en’, ‘cht’)
    • Identify the target language for translation
  5. created_at

    • Record creation time
    • Use the database server default time
  6. updated_at

    • Last updated time
    • Automatic update, record translation modification time

Unique constraints

  • UniqueConstraint('msgid', 'lang_code', name='unique_translation')
  • Make sure each text has only one translation in a specific language
  • Prevent duplicate translation and data redundancy

Example of usage scenario

# Create a new translationnew_translation = Translation(
    msgid='username',
    msgstr='Username',
    lang_code='en'
)
(new_translation)
()

# Query specific language translationsenglish_translations = (Translation).filter_by(lang_code='en').all()

Best Practices

  1. KeepmsgidConsistency
  2. Update in timeupdated_atTime stamp
  3. Regularly clean out outdated or invalid translation records
  4. Consider using long textTextType rather thanString

Translation management best practices

1. Translation quality control

  • Use manual audit mechanism
  • Create a translation record log
  • Regularly update translation cache

2. Performance optimization

  • Cache translation results
  • Asynchronous translation processing
  • Limit the number of single translations

Scalability suggestions

1. Support more languages

  • existlanguage_versionAdd new language to the table
  • renewgenerate_po.pySupport new languages

2. Manual translation integration

  • Develop a manual translation review interface
  • Allow administrators to modify automatic translation results

Troubleshooting

Frequently Asked Questions

  1. Translation API call failed

    • Check network connection
    • Verify API Quota
    • Use alternate translation services
  2. Localization display exception

    • confirm.poFile generation is correct
    • Check locale settings
    • Verify Jinja2 localization configuration

PO file generation script generated_po.py

Script Function Overview

generate_po.pyIt is an automated translation file generation tool that provides the following core functions:

  • Get original translated text from database
  • Use Baidu Translation API to automatically generate translations
  • Create.poLocalized files in format
  • Supports full or specified language translation

Main implementation logic

def generate_translations(output_dir='locales', specific_lang=None):
    """
    Generate translation files
    
    Args:
        output_dir (str): Output directory
        specific_lang (str, optional): Specify language code
    """
    # Get basic Chinese (simplified) translation    zh_translations = generator.get_translations('zh')
    
    # Determine the language to generate    if specific_lang:
        other_langs = [specific_lang]
    else:
        other_langs = generator.get_available_languages()

    # Generate translation files for each language    for lang_code in other_langs:
        # Create a language directory        lang_dir = (output_dir, lang_code, 'LC_MESSAGES')
        (lang_dir, exist_ok=True)
        
        # Generate .po files        with open((lang_dir, ''), 'w') as f:
            # Write to file header            # Write translation entries            for zh_trans in zh_translations:
                # Translate using Baidu Translation API                msgstr = baidu_translate(zh_trans['msgid'], from_lang='zh', to_lang=lang_code)
                
                # Write translation entries                (f'msgid "{zh_trans["msgid"]}"\n')
                (f'msgstr "{msgstr}"\n')

### Language prompts are saved in the table
DROP TABLE IF EXISTS translations;
CREATE TABLE translations (
id int NOT NULL AUTO_INCREMENT,
msgid varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL,
msgstr varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL,
lang_code varchar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL,
created_at timestamp NULL DEFAULT CURRENT_TIMESTAMP,
updated_at timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (id) USING BTREE,
UNIQUE INDEX unique_translation(msgid ASC, lang_code ASC) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 32 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;
INSERT INTO translations VALUES (1, ‘front page', 'front page', ‘zh', ‘2025-02-15 13:50:35', ‘2025-02-15 13:53:03');
INSERT INTO translations VALUES (2, ‘Classification', 'Classification', ‘zh', ‘2025-02-15 13:50:35', ‘2025-02-15 13:53:03');
INSERT INTO translations VALUES (3, ‘Label', 'Label', ‘zh', ‘2025-02-15 13:50:35', ‘2025-02-15 13:53:03');
INSERT INTO translations VALUES (4, ‘about', 'about', ‘zh', ‘2025-02-15 13:50:35', ‘2025-02-15 13:53:03');
INSERT INTO translations VALUES (5, ‘Personal Profile', 'Profile', ‘zh', ‘2025-02-15 13:50:35', ‘2025-02-15 13:53:03');
#Chinesemsgid “Email Verification”
msgstr “Email Verification”

msgid “Mail”
msgstr “Mail”

msgid “post code”
msgstr “post code”

msgid “Read more”
msgstr “Read more”

msgid “front page”
msgstr “Read more”

msgid “Verification code”
msgstr “Verification code”
msgid “Email Verification”
msgstr “Email Verification”

msgid “Mail”
msgstr “Email Box”

msgid “post code”
msgstr “Postal Code”

msgid “Read more”
msgstr “Read More”

msgid “front page”
msgstr “Home Page”

msgid “Verification code”
msgstr “Verification Code”

The above is the detailed content of the operating guide for using fastapi to achieve multilingual internationalization. For more information about fastapi multilingual internationalization, please pay attention to my other related articles!