DEV Community

cigati.ir
cigati.ir

Posted on

How I Built a Zero-Backend Binary .MO Compiler & WordPress POT Translator in the Browser

If you have ever localized a WordPress theme or WooCommerce plugin, you probably ran into the same two annoying options:

  1. Pay $39.99/year for desktop software like Poedit Pro just to scan PHP source code and auto-translate strings.
  2. Install an in-dashboard plugin like Loco Translate that runs directly inside your live WordPress site, eating PHP memory and cluttering your admin panel.

I wanted something faster, free, and 100% client-side—so I built POT Translator AI, a browser-based localization suite for WordPress developers. Here is a quick technical look at how the core pieces work under the hood.


1. Compiling Binary .mo Files in 1ms with ArrayBuffer

Many developers don't realize that WordPress never reads .po text files at runtime. PHP strictly requires compiled binary .mo (Machine Object) files.

Instead of spinning up a Linux backend running msgfmt, you can compile GNU Gettext .mo binaries directly in JavaScript using TextEncoder, ArrayBuffer, and DataView:

  • Header (28 bytes): Little-endian magic number (0x950412de), format revision (0), number of strings (N), byte offset of the original strings table (O), byte offset of the translated strings table (T), and hashing table size/offset (0, 0).
  • Sorting Requirement: Gettext requires original (msgid) entries to be sorted lexicographically by their UTF-8 byte representation so PHP's binary search can resolve strings in O(log n).
  • String Tables: Each entry stores (length, offset) as two 32-bit unsigned integers, followed by null-terminated UTF-8 payloads.

The result? Compiling a 3,000-string WooCommerce catalog into a valid .mo binary takes ~1 millisecond in the browser with zero server uploads. You can test the standalone PO to MO online converter directly without uploading anything to a server.


2. Protecting %s and HTML Placeholders During AI Translation

One of the easiest ways to crash a WordPress site (White Screen of Death) is letting an LLM translate or drop a PHP printf placeholder like %s, %d, or %1$s.

Before sending batches to an AI model (or a user's own BYOK endpoint like OpenAI, Claude, DeepSeek, or local Ollama), our tokenizer isolates:

  • printf specifiers (%s, %d, %1$s, %%)
  • HTML tags (<a href="...">, <strong>)
  • Shortcodes and curly-brace variables ({order_number})

After translation, a client-side QA validator compares every msgid against its msgstr and flags any missing or mismatched tokens before you export.


3. Client-Side ZIP Unpacking & PHP/JS Source Scanning

What if a theme author forgot to include a .pot template? Using JSZip in the browser, we unpack the uploaded .zip, scan .php and .js files for WordPress Gettext calls (__(), _e(), _x(), esc_html__(), esc_attr__()), and generate a clean .pot template on the fly—making it a free online Poedit alternative with zero installation.

We also built a dual-pane online PO editor with keyboard shortcuts, Find & Replace, and persistent client-side Translation Memory (IndexedDB) so common strings like "Add to cart" or "Save Changes" are auto-filled at zero token cost across projects.


Safe Deployment Tip for WordPress Developers

Never place your compiled .mo files inside wp-content/plugins/your-plugin/languages/—WordPress wipes that folder on every plugin update. Always deploy to the global update-proof directory:

wp-content/languages/plugins/{textdomain}-{locale}.mo
wp-content/languages/themes/{textdomain}-{locale}.mo
Enter fullscreen mode Exit fullscreen mode

If you want the full breakdown of safe paths, text domains, and Gutenberg Jed 1.x JSON script translations, check out our WordPress plugin translation guide.


I'd love to hear how other WordPress devs handle i18n in 2026—are you still using Poedit, WP-CLI, or something custom? Drop a comment below!

Top comments (0)