Download the PHP package laravel-tipi/localization without Composer

On this page you can find all versions of the php package laravel-tipi/localization. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package localization

Laravel Tipi Localization

Locales, locale contexts and localized routing for Laravel 13.

Requirements

Installation

Using a database driver (for locales or contexts)? Publish and run the migrations:

The package does not run migrations automatically. Two migrations are published, and together they are the complete schema:

Migration Purpose
create_locales_table locales table, with a database-level "at most one default" constraint (a partial unique index on SQLite and PostgreSQL, a unique generated column on MySQL and MariaDB)
create_locale_contexts_tables locale_contexts and locale_context_locales (only needed with the database contexts driver)

Supported databases: SQLite, MySQL, MariaDB and PostgreSQL.

Concepts

Term Meaning API
Active locale A locale that exists globally and is enabled Localization::locales()->active()
Global default The registry's default locale; always active Localization::locales()->default()
Context A named subset of active locales for one interface Localization::context('admin')
Supported locale An active locale that a context offers $context->supported()
Context default A context's default; may differ from the global default $context->default()
Current locale The application locale right now (App::getLocale()) Localization::current()
Preference A locale the visitor explicitly chose, stored per context Localization::preference()

Contexts only narrow what an interface offers. They never change which locales exist. Content translation (the translations package) works with the global registry.

Application code works with immutable domain objects, whichever driver stores the data:

Class Role
Tipi\Support\Locale One locale. isActive() and isDefault() describe it in the global list; isDefault() is the global default
Tipi\Localization\LocaleContext A resolved context: its supported locales and its default ($context->isDefault($locale))
Tipi\Localization\Contexts\LocaleContextDefinition A context as stored (config or database), before resolution; for management UIs
Tipi\Localization\Data\*Data Validated input for the management actions

Eloquent models live only in Tipi\Localization\Models (Models\Locale, Models\LocaleContext). They are the persistence layer for the database drivers, and are what the management actions, policies and admin UIs work with. Everything else reads the domain objects (Models\Locale::toLocale() converts one).

Locales

Config driver (default)

Every configured locale is active, and the array order is the display order. The configuration is validated on first use. Mistakes such as a typo in a driver name, an invalid code, a duplicate code or an unknown default throw InvalidLocaleConfigurationException with an explanation.

Locale codes follow a practical BCP 47 subset: en, eng, en-US, en-001, zh-Hans, zh-Hans-CN (at most 12 characters). native_abbreviation is up to 3 characters, country_code is two uppercase letters, and text_direction is ltr (the default) or rtl.

Database driver

Locales live in the locales table and are managed with the actions described under "Managing locales". They are ordered by position, then code.

Reading locales

Contexts

With no contexts at all, the package provides a single web context that contains every active locale. That matches the behavior of an application without contexts.

Invariants, enforced when a context is resolved and before one is stored:

  1. A locale list must contain the global default locale.
  2. A context's own default must belong to the context.
  3. Listed locales must exist in the global registry (a context cannot introduce locales).
  4. Inactive locales are never offered, even when a context lists them.
  5. A stored context default must be active.

A violation throws InvalidLocaleContextException, naming the context and the rule. The default context (default_context) always exists. It is used by localized routes, console commands and queued jobs.

Selecting a context for a part of the application

Localized routes always use the default context. For other areas (admin panels, dashboards, APIs) use the middleware:

The middleware makes the context current and sets the application locale to the visitor's preference for that context. Failing that, it uses the best Accept-Language match within the context, and then the context default.

Database-backed contexts

The two drivers are independent: config-defined locales can be combined with database-managed contexts, and the reverse.

Source of truth. With the database driver, the locale_contexts tables own the context definitions. Contexts listed in config('localization.contexts'), plus the default context, are declared contexts:

Contexts created with CreateLocaleContext are ordinary rows and can be deleted.

All context actions validate the invariants, run in a transaction, lock the global default row (so they cannot race a default-locale change) and return the Models\LocaleContext model ($model->localeCodes() returns null for *). The stored, unresolved definition is available as Localization::contexts()->definition('admin'), which has locales, default, required and persisted.

Global default vs context defaults

Changing the global default (MakeLocaleDefault) keeps the invariants intact:

A locale that is a context default cannot be deactivated or deleted until the context chooses another default.

Localized routing

With en as the default context's default and hide_default_locale => true:

URL Result
/wines/10 English
/ka/wines/10 Georgian
GET /en/wines/10?page=2 301 to /wines/10?page=2
POST /en/contact handled in English (never redirected)
/fr/wines/10 (unknown or inactive) 404

With hide_default_locale => false, every locale is prefixed (/en/wines), unprefixed localized URLs return 404, and / redirects to the root route (root_route) in the default or negotiated locale.

Rules and caveats:

URLs with Laravel's own helpers

Laravel's route() and everything built on it work with the logical names:

This uses Laravel's UrlGenerator::resolveMissingNamedRoutesUsing() hook. Laravel asks the package only when no route has the exact name, so:

Without a locale parameter, the current locale is used when the default context supports it, otherwise that context's default. A locale outside the routing context throws UnsupportedLocaleException.

Localization::route($name, $parameters, $locale, $absolute) is the same operation with a typed $locale argument.

What does not see logical names: these read Laravel's physical route names directly and have no extension hook. Use the package equivalents:

Laravel Use instead
request()->routeIs('wines.*'), Route::is() Localization::routeIs('wines.*')
Route::has('wines.show') Localization::hasRoute('wines.show')
Route::currentRouteName() Localization::currentRouteName()
action([WineController::class, 'show'], ...) route('wines.show', ...) (action URLs resolve to the prefixed variant and need a locale parameter)

Other notes:

Current page and route name

SEO alternates

alternates() returns hreflang => absolute URL for every supported locale of the default context, plus x-default. It is empty on non-localized pages.

Switching and remembering the locale

The package registers GET /locale/{locale} (named localization.switch, in the web middleware group). It records the choice for the context and redirects to a same-site path only: absolute URLs, //host, backslashes and control characters fall back to /.

On a localized page, the switch URL returns to the same page in the chosen locale. On other pages it returns to the current path.

From your own code (a Livewire action, a controller, an API endpoint):

Root negotiation

A request for the unprefixed root route is redirected according to the preference for the default context, then Accept-Language, and is served as-is when the result is the default. Only that route is negotiated. Responses carry Vary: Accept-Language, Cookie. Because the switch endpoint records explicit choices, a visitor who picks the default language is not redirected away again.

Accept-Language matching is case-insensitive and accepts _ or -. It tries the exact tag, then the tag with trailing subtags removed (zh-Hant-TW → zh-Hant → zh), then any supported code with the same language (en → en-US). It skips q=0 and returns the context default when nothing matches.

Current locale

current() reflects App::getLocale(), so App::setLocale() and withLocale() (used by mailables and notifications) work as expected. When the application locale isn't an active locale (for example Laravel's app.locale in a console command), it falls back to the current context's default instead of throwing. To change the locale for a request, use App::setLocale(); to remember a choice, use Localization::select().

The registry, contexts and current context reset for every request, queued job and Octane request (scoped bindings).

Managing locales (database driver)

For forms, LocaleRules::create(), LocaleRules::update(), LocaleRules::attributes(), LocaleContextRules::create() and LocaleContextRules::update() return matching Laravel validation rules. Context invariants are left to the actions.

Custom models

Set $table and $connection on the model. Migrations, repositories, actions and validation rules all use them. With both database drivers, give both models the same $connection (see above). Override membershipTable() on the context model to rename the membership table.

Authorization

The package registers LocalePolicy and LocaleContextPolicy. Both deny everything unless the user is granted the manage-localization ability:

LocalePolicy abilities are viewAny, view, create, update, activate, deactivate, makeDefault, reorder and delete. LocaleContextPolicy abilities are viewAny, view, create, update and delete. Gate::before() callbacks apply as usual. Replace a policy with config('localization.policies.locale') or config('localization.policies.context').

Policies only answer "may this user do this?". Whether an operation is possible (for example deactivating the default) is decided by the actions. The model helpers canBeActivated(), canBeDeactivated(), canBeMadeDefault() and canBeDeleted() check a locale's own state for showing or hiding buttons.

Building a management UI (e.g. Filament)

The future filament-localization resources should be thin layers over this API:

Exceptions

All of them implement Tipi\Localization\Exceptions\LocalizationException.

Exception When
InvalidLocaleConfigurationException Invalid config/localization.php, or the switch route is used while disabled
LocaleNotFoundException A code doesn't exist in the registry ($e->locale)
UnsupportedLocaleException A locale is inactive, or not supported by a context ($e->locale, $e->context)
DefaultLocaleMissingException No active default locale (for example an empty locales table)
LocaleContextNotFoundException Unknown context name
InvalidLocaleContextException A context definition breaks an invariant
InvalidLocaleDataException A DTO received malformed data
InvalidLocaleOperationException A management action was rejected, or a driver mismatch
InvalidLocalizedRouteException Unnamed, duplicate or colliding localized route names; a locale code equal to a reserved static segment; a malformed locale route parameter

Unknown or unsupported locales in URLs and on the switch endpoint return 404 rather than throwing.

Testing and code style

The suite runs on in-memory SQLite by default. To run it against a real server:

Other variables: LOCALIZATION_TEST_DB_HOST, LOCALIZATION_TEST_DB_DATABASE (default tipi) and LOCALIZATION_TEST_DB_USERNAME. The database is wiped before each test. CI runs the suite on SQLite, MySQL 8.4, MariaDB 11 and PostgreSQL 16.

License

MIT.


All versions of localization with dependencies

PHP Build Version
Package Version
Requires php Version ^8.5
illuminate/contracts Version ^13.0
illuminate/database Version ^13.0
illuminate/http Version ^13.0
illuminate/routing Version ^13.0
illuminate/support Version ^13.0
illuminate/validation Version ^13.0
laravel-tipi/support Version ^1.0.12
spatie/laravel-package-tools Version ^1.93
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package laravel-tipi/localization contains the following files

Loading the files please wait ...