How to Transliterate Serbian Cyrillic to Latin for WordPress

Given the global reach of WordPress, it's important for many users to have their content available in different scripts and languages. Although WordPress by default supports Serbian Cyrillic, the platform does not natively support Serbian Latin. However, with a bit of programming magic, it's entirely possible to transliterate from Serbian Cyrillic to Serbian Latin. Here's how you can achieve this.

Prerequisites

  • Basic understanding of Python
  • Access to your WordPress files (via FTP, cPanel, or directly if hosted locally)
  • A Python environment to execute the script

WordPress logotype

Before you start the transliteration process, you need to set Serbian as the default system language in WordPress settings. After setting Serbian as the language:

  1. Connect to your WordPress installation via FTP.
  2. Navigate to the folder where the language files are located.
  3. Copy all files related to the Serbian language and save them somewhere on your local computer.

To execute the transliteration script (provided later in the text), follow these steps:

  1. If you don't already have Python installed, visit the official Python website and download the latest version.
  2. Install the polib library using pip, Python's package manager. In the command line or terminal, type:
    pip install polib
  3. Open your favorite Python IDE or code editor.
  4. Create a folder and within it, create main.py and paste the following script:
    import os
    import polib
    import json
    
    def cyrillic_to_latin(text):
        transliteration_map = {
            '\u0410': 'A', '\u0411': 'B', '\u0412': 'V', '\u0413': 'G', '\u0414': 'D', '\u0402': 'Đ',
            '\u0415': 'E', '\u0416': 'Ž', '\u0417': 'Z', '\u0418': 'I', '\u0408': 'J', '\u041A': 'K',
            '\u041B': 'L', '\u0409': 'Lj', '\u041C': 'M', '\u041D': 'N', '\u040A': 'Nj', '\u041E': 'O',
            '\u041F': 'P', '\u0420': 'R', '\u0421': 'S', '\u0428': 'Š', '\u0422': 'T', '\u040B': 'Ć',
            '\u0423': 'U', '\u0424': 'F', '\u0425': 'H', '\u0426': 'C', '\u0427': 'Č', '\u040F': 'Dž',
    
            '\u0430': 'a', '\u0431': 'b', '\u0432': 'v', '\u0433': 'g', '\u0434': 'd', '\u0452': 'đ',
            '\u0435': 'e', '\u0436': 'ž', '\u0437': 'z', '\u0438': 'i', '\u0458': 'j', '\u043A': 'k',
            '\u043B': 'l', '\u0459': 'lj', '\u043C': 'm', '\u043D': 'n', '\u045A': 'nj', '\u043E': 'o',
            '\u043F': 'p', '\u0440': 'r', '\u0441': 's', '\u0448': 'š', '\u0442': 't', '\u045B': 'ć',
            '\u0443': 'u', '\u0444': 'f', '\u0445': 'h', '\u0446': 'c', '\u0447': 'č', '\u045F': 'dž',
        }
    
    
        return ''.join(transliteration_map.get(char, char) for char in text)
    
    def process_po_file(file_path, output_path):
        po = polib.pofile(file_path)
        for entry in po:
            entry.msgstr = cyrillic_to_latin(entry.msgstr)
    
        if 'Language' in po.metadata and po.metadata['Language'] == 'sr_RS':
            po.metadata['Language'] = 'sr_latnRS'
    
        po.save(output_path)
        po.save_as_mofile(output_path.replace('.po', '.mo'))
    
    
    def process_json_file(file_path, output_path):
        with open(file_path, 'r', encoding='utf-8') as json_file:
            data = json.load(json_file)
    
        if "locale_data" in data and "messages" in data["locale_data"]:
            messages = data["locale_data"]["messages"]
    
            if "" in messages and "lang" in messages[""]:
                messages[""]["lang"] = "sr_latnRS"
    
            for key in messages:
                if isinstance(messages[key], list):
                    for i in range(len(messages[key])):
                        messages[key][i] = cyrillic_to_latin(messages[key][i])
                elif isinstance(messages[key], str):
                    messages[key] = cyrillic_to_latin(messages[key])
    
        with open(output_path, 'w', encoding='utf-8') as json_outfile:
            json.dump(data, json_outfile, ensure_ascii=False, indent=4)
    
    
    def process_directory(directory):
        for root, _, files in os.walk(directory):
            for file in files:
                if file.endswith('.po') and 'sr_RS' in file:
                    input_path = os.path.join(root, file)
                    output_file = file.replace('sr_RS', 'sr_latnRS')
                    output_path = os.path.join(root, output_file)
                    
                    process_po_file(input_path, output_path)
                    print(f"Processed: {input_path} -> {output_path}")
    
                elif file.endswith('.json') and 'sr_RS' in file:
                    input_path = os.path.join(root, file)
                    output_file = file.replace('sr_RS', 'sr_latnRS')
                    output_path = os.path.join(root, output_file)
    
                    process_json_file(input_path, output_path)
                    print(f"Processed: {input_path} -> {output_path}")
    
    directory_to_process = r'--putanja do foldera sa ćiriličnim fajlovima iz WordPress-a--'
    process_directory(directory_to_process)
  5. In the script, find the line where the location of the language files is defined and change it to match the location where you previously saved the Serbian language files.
  6. Save the changes and run the script.

After the script executes, all Serbian language files will be transliterated to Latin and ready for use on your WordPress site. The following steps will guide you through the process of adding Serbian Latin as an interface language for your WordPress site:

  1. Connect to your server using an FTP client.
  2. Navigate to the folder where the language files are located (usually within wp-content/languages).
  3. Copy all the newly transliterated files (those marked with sr_LatnRS) to this location.
  4. Log in to your WordPress site and go to Settings > Site Language.
  5. In the list of available languages, you will find a new language marked as Serbian (Latin). Select it and click the save settings button.

Congratulations! Your WordPress site now uses Serbian Latin as the interface language.

Additionally, every time you update WordPress to a newer version, there is a possibility that new texts or changes in language files have been added. To ensure everything is translated into Serbian Latin, it is recommended to repeat the above transliteration process after each update.

 

Note: As the author of this guide, I do not take responsibility for any issues, errors, or damages that may arise from using this method. It is recommended to always back up your site before making any changes. You apply all the steps in this guide at your own risk.

 

If you're looking for top-tier software developer to hire, look no further!

What I offer:

  • Website Development: Turn your idea into a fully functional website.
  • Mobile App Creation: Reach your audience on every device.
  • Custom Software Solutions: Software tailored to your business needs.
  • Database Management: Ensure your data is structured, secure, and accessible.
  • Consultations: Not sure where to start? Let's discuss the best tech solutions for your goals.

With 20+ years of experience in the tech industry, I've honed my skills to provide only the best for my clients. Let's turn your vision into reality. Contact me today to kick off your next digital project!