Adding translations to Laravel Spark 3 "Plans"

Adding translations to Laravel Spark 3 "Plans"

Written by Tina Hammar on Apr 20th, 2022 Views Report Post

After buying an unlimited license of Laravel Spark v3, I discovered that it loads the subscription plans from a config file, which means that you don't have access to Laravels translation magics.

I posted a question in the Laravel Spark chat. The reply was a suggestion to solve it with a Middleware.

The example below is how I solved it in my app.

1. Create Middleware

<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Config;

class SparkTransPlansMiddleware
{
    public function handle(Request $request, Closure $next)
    {
        Config::set('spark.billables', $this->getTranslatedPlans(config('spark.billables')));
        return $next($request);
    }

    private function getTranslatedPlans(array $billables): array
    {
        $translated = [];
        foreach ($billables['user']['plans'] as $plan) {
            $key = $plan['name'];
            $translated[] = array_merge($plan, [
                "short_description" => trans("plans.$key.short_description"),
                "features" => trans("plans.$key.features"),
            ]);
        }
        $billables['user']['plans'] = $translated;
        return $billables;
    }
}

2. Create translation files

Example lang/en/plans.php Make sure that the plan name is the same as in config/spark

<?php

return [
    'Small' => [
        'short_description' => 'Free plan',
        'features' => [
            'Feature 1',
            'Feature 2',
        ],
    ],
    'Medium' => [
        'short_description' => 'Paid plan',
        'features' => [
            'Feature 1',
            'Feature 2',
            'Feature 3',
        ],
    ],
];

3. Add the plans to Spark config

It doesn't matter what you type in short_description and features, as it is replaced in the Middleware at runtime. I typed the translation path as a reminder to myself when I get back to the code after a while.

'billables' => [
        'user' => [
            'model' => User::class,
            'trial_days' => 30,
            'default_interval' => 'monthly',
            'plans' => [
                [
                    'name' => 'Small',
                    'short_description' => 'trans.plans',
                    'monthly_id' => 'stripe_id',
                    //'yearly_id' => 'none',
                    'features' => ['trans.plans'],
                ],
                [
                    'name' => 'Medium',
                    'short_description' => 'trans.plans',
                    'monthly_id' => 'stripe_id',
                    'yearly_id' => 'stripe_id',
                    'features' => ['trans.plans'],
                ],
            ],
        ],
    ]

4. Register the middleware

http/Kernel

protected $routeMiddleware = [
    'spark-trans-plans' => \App\Http\Middleware\SparkTransPlansMiddleware::class,
];

5. Apply the middleware to Spark billing route

config/spark

'middleware' => ['web', 'auth', 'spark-trans-plans'],

Don't forget to clear the config, route and app cache.

Comments (0)