Sam Bellerose
Sam Bellerose

Reputation: 1812

Translate Laravel Spark plan features

I was wondering what would be the best way to display my Spark plan features in multiple different languages.

Let's say I have the followings features

Spark::plan('Premium', 'monthly-artist-premium')
        ->price(10)
        ->trialDays(14)
        ->features([
            'Online profile', 'Access To More Features',
        ]);

I thought about doing something like this using Laravel's translation tool and the translation keys

Spark::plan('Premium', 'monthly-premium')
        ->price(10)
        ->trialDays(14)
        ->features([
            'base.Online_profile', 'base.Access_to_more_features',
        ]);

And then when rendering the plans using Vue I would do something like this, but it's not translating.

<li class='pricing-feature' v-for="feature in plan.features">
    @lang('@{{ feature }}')
</li>

Any idea how I could implement this to handle multiple languages?

Upvotes: 0

Views: 425

Answers (3)

Anibal Sanchez
Anibal Sanchez

Reputation: 534

You can use the HandleInertiaRequests middleware to translate the strings before they are shared in the request.

For more info: Shared data

Sample code:

namespace Application\Http\Middleware;

use Domain\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Inertia\Middleware;

class HandleInertiaRequests extends Middleware
{
    /**
     * Define the props that are shared by default.
     *
     * @return array
     */
    public function share(Request $request)
    {
        $this->translatePlans();

        ...
    }

    private function translatePlans(): void
    {
        $sparkConfig = config('spark');
        $plans = $sparkConfig['billables']['account']['plans'];
        $translatedPlans = [];

        foreach ($plans as $plan) {
            $plan['short_description'] = __($plan['short_description']);
            $translatedPlans[] = $plan;
        }

        $sparkConfig['billables']['account']['plans'] = $translatedPlans;

        app('config')->set('spark', $sparkConfig);
    }
}

Upvotes: 0

Moises Jafet
Moises Jafet

Reputation: 41

On Laravel Spark 7.0, I've managed to translate the Feature List by:

  1. Appending the translated keys to the language .json files.
  2. On the SparkServiceProvider::booted() method using those keys for the feature list.
  3. On resources/views/vendor/spark/modals/plan-details.blade.php and spark/resources/views/modals/plan-details.blade.php

    <!-- Modal Body -->
    <div class="modal-body">
        <ul class="plan-feature-list p-0 m-0">
            <li v-for="feature in detailingPlan.features">
                @{{ feature }}
            </li>
        </ul>
    </div>
    

Change to:

    <!-- Modal Body -->
    <div class="modal-body">
        <ul class="plan-feature-list p-0 m-0">
            <li v-for="feature in detailingPlan.features">
                @{{ __(feature) }}
            </li>
        </ul>
    </div>

Upvotes: 2

Sam Bellerose
Sam Bellerose

Reputation: 1812

Not the best solution, but here's what I ended up doing:

Spark::freePlan('Basic')
        ->features([
            'free_plan'
]);

Then when showing the plans in register-common.blade.php

I did something like this with the v-if conditions for each different plans

<ul v-if="plan.features[0] === 'free_plan'" class='pricing-feature-list'>
    <li class="pricing-feature">
        @lang('base.Online_profile')
    </li>
</ul>

Upvotes: 0

Related Questions