Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Laravel Translatable Laravel Package

astrotomic/laravel-translatable

Laravel package for translatable Eloquent models. Store model translations in the database and automatically fetch/save multilingual attributes based on locale, reducing boilerplate when working with multi-language content.

View on GitHub
Deep Wiki
Context7

Validation Rule Factory

If you want to validate translated attributes it could get quite complex to list all rules with corresponding attributes their own. To simplify this we've added a RuleFactory class. It get's a list of rules and you can mark the key you want to get locale aware. The factory will take the rules assigned to this "placeholder" and copy it to all corresponding locale aware keys.

$rules = RuleFactory::make([
    'translations.%title%' => 'sometimes|string',
    'translations.%content%' => ['required_with:translations.%title%', 'string'],
]);

$validatedData = $request->validate($rules);

This will return an array which adjusted the placeholder in key and string value or array with strings to match your configured key format. The result will be:

[
    'translations.en.title' => 'sometimes|string',
    'translations.de.title' => 'sometimes|string',
    'translations.de-DE.title' => 'sometimes|string',
    'translations.de-AT.title' => 'sometimes|string',

    'translations.en.content' => ['required_with:translations.en.title', 'string'],
    'translations.de.content' => ['required_with:translations.de.title', 'string'],
    'translations.de-DE.content' => ['required_with:translations.de-DE.title', 'string'],
    'translations.de-AT.content' => ['required_with:translations.de-AT.title', 'string'],
]

Configuration

To adjust the default format , prefix or suffix used by the factory you can change them in the package configuration file.

{% code title="config/translatable.php" %}

'rule_factory' => [
    'format' => \Astrotomic\Translatable\Validation\RuleFactory::FORMAT_ARRAY,
    'prefix' => '%',
    'suffix' => '%',
]

{% endcode %}

As format we support the two possible variants the fill() method supports.

RuleFactory::FORMAT_ARRAY

This will create the dot-notation to support locale sub-arrays. en.content.

RuleFactory::FORMAT_KEY

This will create the colon separated style. content:en

{% hint style="info" %} If you use the key format and want to use use it as argument for a rule you have to wrap it in quotes. required_with:"translations.content:en" {% endhint %}

Runtime

For sure you can change the default format, prefix, suffix and applied locales during runtime. To do so only pass them as parameter to the make() method.

RuleFactory::make($rules, RuleFactory::FORMAT_KEY, '{', '}', [
    'en',
    'de',
]);

This will use the colon style, use { and } as delimiter and use only de and en as locales.

{% hint style="info" %} You can only use defined locales. Every locale that's not in Locales::all()will throw an InvalidArgumentException. {% endhint %}

Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
hamzi/corewatch
minionfactory/raw-hydrator
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle
atriumphp/atrium
sandermuller/package-boost-laravel
sandermuller/boost-skills
redaxo/core
yusufgenc/filament-api-forge
l3aro/rating-star-for-filament
leek/filament-subtenant-scope