Beyond Static Files: Building a Dynamic AI Translator for Laravel 12

Beyond Static Files: Building a Dynamic AI Translator for Laravel 12

Translating web applications is hard. Really hard. Managing separate language files (lang/en/messages.php, lang/fr/messages.php, etc.) quickly becomes a logistical headache, especially as your project grows and the number of supported languages increases. Getting initial translations, keeping them updated, and ensuring consistency across potentially hundreds or thousands of strings can be a major bottleneck.

What if you could make this process significantly faster, more flexible, and leverage the power of modern AI translation? That’s the challenge I recently tackled with a custom project for Laravel 12.

Like many developers, I have ongoing projects that involve experimenting with new technologies. While some stay under wraps, I thought this one might offer some useful insights and inspiration for fellow Laravel developers struggling with internationalization (i18n) and localization (l10n).

Introducing the Dynamic Laravel Translator

The core idea is simple: instead of managing multiple language files as your source, you manage a single source file that can contain strings in any language. This single source then dynamically generates all your target language files using AI/ML translation.

I built this as a custom Job and an extension to Laravel’s Artisan command structure (specifically, building upon the concepts often found in lang:publish and similar commands).

Here’s how it works:

  1. A Single Source of Truth: I created a new directory within the standard lang/ folder, specifically lang/.source. This folder holds the file(s) containing all the strings you want translated.

  2. Multilingual Source Input: The beauty of this lang/.source file is that developers or content creators can input the source string in the language they are most comfortable with for that particular phrase or sentence.

    Here’s an example of what a file in lang/.source might look like:

    <?php
    
    return [
        'failed' => 'These credentials do not match our records.', // English
        'password' => 'Hindi tama ang ipinasok na password.', // Tagalog
        'throttle' => 'ความพยายามในการเข้าสู่ระบบมากเกินไป โปรดลองอีกครั้งในอีก :seconds วินาที', // Thai
        'continue-with' => 'Continue with', // English
        'error-not-authorized' => 'You are not authorized to continue.', // English US
    ];
    

    Notice how the values are in different languages? This allows the person adding the string to use their native language if it best captures the nuance needed.

  3. Dynamic Translation: My custom tool reads the keys and values from the lang/.source file. For each key, it identifies the source language of the value (using AI language detection) and then sends it to an AI/ML translation service to be translated into all the target languages configured for the application.

  4. Automated Output: The tool then automatically generates the standard Laravel language directories and files under lang/. For example, if you’re translating to English (UK), English (US), and Spanish, it will create lang/en-UK/your_file.php, lang/en-US/your_file.php, and lang/es/your_file.php, populated with the AI-translated strings.

    This process can be triggered whenever needed via a custom Artisan command, lang:translate. This command offers flexible options to target specific files or languages and even force an overwrite of existing translations. While the tool is designed to also run automatically on a schedule via a Job, I currently find myself relying primarily on the granular control offered by the Artisan command for most translation tasks.

Key Benefits of this Approach

  • Speed and Scale: This is arguably the biggest win. Generating initial translation drafts for a large number of strings and languages is incredibly fast.
  • Flexibility in Source Creation: Developers or content editors aren’t restricted to writing source strings only in English. They can use the language they understand best for a specific phrase, potentially leading to more accurate initial source content.
  • Rapid Prototyping: Need to quickly see how your app looks in a new language? Generate the files instantly.
  • Accelerated Review: While AI translation isn’t perfect, having a complete set of automatically generated translations provides a massive head start for Subject Matter Experts (SMEs) or human translators. Instead of translating from scratch, they are reviewing and refining, which is significantly faster.

Handling Human Refinements (The Clever Bit)

Automated translation is powerful, but you inevitably need to make manual tweaks. Maybe you want to shorten a phrase to fit UI constraints, use a specific regional dialect, or incorporate a contraction for a more natural flow.

For instance, the AI might translate 'error-not-authorized' to:

  • lang/en-UK/auth.php: 'You are not authorised to continue.'
  • lang/en-US/auth.php: 'You are not authorized to continue.'

Let’s say you decide to manually edit the en-US file to use a contraction for brevity:

'error-not-authorized' => 'You're not authorized to continue.'

The problem is, the next time the dynamic translator runs, it will see the source string 'You are not authorized to continue.' in lang/.source and overwrite your manual edit with the AI-generated translation again.

The solution? A simple comment marker. My tool checks for a specific comment at the end of a translated line in the target language files. If it finds the marker, it skips updating that line during the next translation run.

So, your manually edited line becomes:

'error-not-authorized' => 'Not authorized to continue.', // Bob - shortened for dialog

Adding any comment like // Bob - shortened for dialog (or any agreed-upon comment) after the line tells the tool, “Leave this line alone, a human has refined it.” This preserves manual edits while still allowing the tool to update all other lines from the dynamic source.

Real-World Impact

The efficiency gains are substantial. Using this tool, I was able to generate initial translation files for 20 source files containing over 1000 individual strings across 50 different languages in less than 10 minutes. That’s a level of speed and scale that would be incredibly time-consuming and expensive with traditional manual workflows. What’s more, preliminary reviews found that 80-90 percent of these machine translations required minimal modifications, even for languages typically considered tough to translate accurately, such as Chinese, Thai, and Burmese. This highlights the remarkable quality of modern AI translation when used as a starting point.

Conclusion

This dynamic translation tool is a prime example of how leveraging AI and rethinking traditional workflows can significantly improve common development challenges. It transforms the initial phase of localization from a laborious manual process into a rapid, automated step, freeing up human experts to focus on the crucial task of reviewing and perfecting the generated translations.

While this project was initially for my own use, the results demonstrate the potential for a more flexible and efficient approach to managing translations in large Laravel applications. It proves that with a little creativity and the right tools, tackling the complexities of l10n doesn’t have to be a bottleneck.

PS: I probably got this in the wrong place, I have never been a forums guy. Apologies in advance if this annoys someone. :wink: