Skip to content

PHP Translation

Lingo.dev supports translating PHP language files used by Laravel framework.

Setting Up

First, make sure you've got a i18n.json config file in your project root. Here's an example:

json
{
  "version": 1,
  "locale": {
    "source": "en",
    "targets": ["es", "fr", "de"]
  },
  "buckets": {
    "json": {
      "include": ["lang/[locale]/messages.php"]
    }
  }
}

This config tells Lingo.dev:

  1. Your source language is English;
  2. You want to translate to Spanish, French, and German;
  3. Your PHP files are in the lang folder, each file is named messages.php and is inside its own locale folder (e.g., lang/en/messages.php)

Available Languages

Lingo.dev supports a wide range of languages. To see what's available:

bash
npx lingo.dev@latest show locale sources  # List available source languages
npx lingo.dev@latest show locale targets  # List available target languages

Use these to pick the right locale codes for your project.

Translating

With your config set, run:

bash
npx lingo.dev@latest i18n

Lingo.dev will:

  1. Read your source PHP file (e.g., lang/en/messages.php);
  2. Identify new or changed strings;
  3. Translate them to your target languages;
  4. Update or create the target PHP files (e.g., lang/es/messages.php, lang/fr/messages.php, lang/de/messages.php).

Why This Works

Lingo.dev's approach is effective because:

  1. It respects your existing file structure, fitting into your workflow instead of forcing you to change it;
  2. It uses AI to understand context, giving you more accurate translations than traditional word-for-word methods;
  3. It's incremental, only translating what's new or changed, saving time and processing power.

Pro Tips

  1. Nested Structures: Lingo.dev handles nested PHP arrays like a champ. Your complex i18n structures are safe.

    json
    <?php
    
    return [
      "nav" => [
        "home" => "Home",
        "about" => [
          "team" => "Our Team",
          "mission" => "Our Mission"
        ]
      ]
    ]
  2. Pluralization: Many i18n frameworks use special syntax for pluralization and placeholders. Lingo.dev understands these:

    json
    <?php
    
    return array(
      "items" => array(
        "one" => "{{count}} item",
        "other" => "{{count}} items"
      )
    )
  3. File Exclusions: You can exclude specific files from translation. This is useful for files that shouldn't be translated, for whatever reason:

    json
    {
      "buckets": {
        "json": {
          "include": ["lang/[locale]/*.php"],
          "exclude": ["lang/[locale]/my-file.php"]
        }
      }
    }

    This setup will translate all PHP files in the lang/[locale]directory, except for files named my-file.php.

  4. Multiple File Types: You can configure multiple file types in your i18n.json:

    json
    {
      "buckets": {
        "php": {
          "include": ["lang/[locale]/messages.php"]
        },
        "json": {
          "include": ["lang/[locale].json"]
        }
      }
    }

    This configuration allows Lingo.dev to handle both PHP and JSON files in your project making it follow the default Laravel project structure.

By leveraging these features, you can maintain a robust, scalable i18n setup for your PHP Laravel project or any other frameworks that support the same format defined as PHP arrays. Lingo.dev's flexibility in handling different file structures and types makes it a powerful tool for managing translations in complex projects.