Download the PHP package codewiser/multilingual without Composer
On this page you can find all versions of the php package codewiser/multilingual. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download codewiser/multilingual
More information about codewiser/multilingual
Files in codewiser/multilingual
Informations about the package multilingual
Multilingual Cast
Multilingual attribute keeps a set of values in different locales. Such an attribute is stored in a database as a JSON object.
| id | name |
|---|---|
| 1 | {"en":"Michael","es":"Miguel"} |
Multilingual lets you deal with such an attribute just like a plain one:
Usage
Apply the cast and include HasMultilingual, which gives access to the lowdown
of a multilingual attribute.
Storing
A new value will be implicitly stored in the current locale.
However, you may explicitly define a locale.
Reading
A plain value will be implicitly retrieved in the current locale. It is enough
to properly apply the Accept-Language header from a User-Agent — and the user
will get content in a preferred language.
If there is no value for the requested locale, the fallback locale is tried next, and then the first value in the map — even when that value is empty.
You may explicitly define a locale.
Hydrating
Reading gives a plain value, so ask for the Multilingual object when you need
another locale, or every locale at once.
A hydrated attribute gives back the whole object.
Hydrating a non-multilingual attribute throws an InvalidArgumentException.
Serializing
Serialization is never hydrated implicitly — $model->toArray() holds plain
values.
Ask for it explicitly wherever every locale belongs in the output.
Language tags
You may access a hydrated Multilingual using language tags as well. The
package uses
locale_filter_matches
to find the best variant:
Strict mode
If you try to get a value that is missing, the Multilingual will try to
return any convenient value — using the fallback locale or just the first one:
You may enable strict mode, and then Multilingual will return the exact
value:
Multilingual arrays
As we may keep multilingual scalars, we may keep multilingual arrays as well:
Multilingual objects
Multilingual array could be mapped into an object:
Changing a mapped object
The very same object is handed out on every read, so changing it changes the attribute. The change is written back right before the attributes are read, dirtied, serialized or saved, so it behaves like any other change of the model.
Assigning the attribute replaces the object, so a change made to the old one is gone:
The write back hangs on getAttributes(), which is what getDirty(),
toArray(), save() and friends funnel through. A model defining its own
getAttributes() overrides the one from the trait, and then it has to call
flushMultilingualAttributes() itself.
Multilingual collection
You may cast an attribute to a collection of Multilingual values. It is
possible to map every row into an object. Strict mode is supported as well:
Multilingual collection (Laravel's cast)
It is possible to cast Multilingual to a collection using Laravel's
AsCollection:
AsCollection::of() maps into the class directly, so it never routes through
the cast. Elements are always Multilingual objects here.
The attribute is not recognised as multilingual though, so multilingual()
throws an InvalidArgumentException for it, and Multilingual::of() leaves it
untouched. Use AsMultilingual::collect() above when the whole collection has to
be hydrated.