Download the PHP package alnaggar/turjuman without Composer

On this page you can find all versions of the php package alnaggar/turjuman. 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 turjuman

Turjuman - Laravel Route Localization Package

Turjuman is a Laravel route localization package designed to simplify the process of generating localized routes effortlessly. The name "turjuman" is derived from the Arabic word تُرجُمان, meaning the person who translates or localizes. This term holds historical significance as it was the name of one of alsahaba (companions) of the Prophet Muhammad may Allah bless him and grant him peace, Abdullah bin Abbas. He earned the title of "turjuman" (the interpreter) of the holy Qur’an due to his renowned skills in interpreting the holy Qur’an, despite being one of the youngest companions.

Table of Contents

Installation

Turjuman is compatible with Laravel 10 and above.

Install the package using Composer:

Next, publish the configuration file:

Then, register these middlewares in your app/http/kernel.php before the \Illuminate\Routing\Middleware\SubstituteBindings middleware

Configuration

The Configuration file (config/turjuman.php) provides flexibility in defining supported locales, default locale, display options, hiding the default locale, route aliases, and locale identifier.

Supported Locales

The supported_locales key in allows you to specify the locales you want to support. With a vast array of +800 locales available, you can uncomment or add new ones based on your project needs. Each locale can be customized with additional properties such as currency, country, etc., making them accessible through the corresponding Locale instance.

[!NOTE] Ensure compliance with ISO 15897 for locale codes and language directories, as recommended by Laravel for better experience.

Default Locale

The default_locale key sets the code for the default locale. Turjuman will fallback to this locale when no locale is detected.

Display Locale in URL

The display_location key controls how the locale is displayed in the URL. It can be a positive integer, indicating the segment to display, a string specifying the query name for display, or null to hide the locale from the URL while setting it internally.

Hide Default Locale

The hide_default key, when set to true, determines whether to display the default locale in the URL if it's displayable.

Route Aliases

The route_aliases key allows you to translate specific URLs in different locales. For instance, you can provide custom slugs and/or domains for URLs by adding them under the corresponding locale keys.

Locale Identifier

The locale_identifier key allows you to set the locale token name used in url parameters, session, cookies and form input to identify the current locale.

Middlewares

SetLocale.php Middleware

This middleware is responsible for setting the current locale based on various sources in a predefined order, such as request input, URL, session, cookies, browser languages, and user preferences to determine the current locale.

In addition to this, you have the option to publish a middleware named TurjumanSetLocale using the artisan command:

By doing so, you can effortlessly override the fetchUserLocale(): ?string function within this middleware. This function allows you to retrieve the user's preferred locale from the database or any other source if it is not determined in the predefined sources. After publishing, replace the existing \Alnaggar\Turjuman\Middleware\SetLocale::class middleware in your app/http/kernel.php with \App\Http\Middleware\TurjumanSetLocale::class middleware. This provides a convenient way to customize how the user's preferred locale is fetched, allowing you to adapt it according to your specific requirements.

LocalizeRoutes.php Middleware

This middleware is responsible for handling localization of routes based on the current locale settings. It checks if the request is a GET request and if current route is localized. It ensures that the URL matches the localized URL, and if not, it redirects to the correct URL.

Features

Route Localization

Easily create localized routes for different languages or regions by utilizing the group function.

Locale Aliases

You can define an alias for the locale to appear in the url in the configuration of the locale instead of the locale code.

Localized URL Generation

Generate localized URLs based on configured locales and group attributes.

Group Attributes

Define custom attributes for route groups to control display type, default locale, and more.

Extensible and Macroable

Extend the functionality of Turjuman by adding macros and mixins.

Alnaggar\Turjuman\Locale and Alnaggar\Turjuman\GroupAttributes are utilizing Alnaggar\Turjuman\Traits\Extensible triat, making them macroable and may have additional properties, this properties may be accessed by array-access, property-access, or get-method-access.

[!NOTE] The custom properties must be named using snake_case.

Redirecting Only When Needed

The redirection mechanism is selectively applied, exclusively triggering when a GET request is encountered. For other types of requests, the locale is internally set without initiating any redirection.

Standard Route Caching

Feel free to employ any of Laravel's standard route caching commands as usual.

Usage

Route Grouping

group(\Closure $routes, array $attributes = []): \Alnaggar\Turjuman\Group

The group function is a powerful feature allowing you to group routes for localization. It takes a closure containing the routes to localize and optional attributes. You can add custom attributes overriding the conifg ones, making it ideal for multi-tenancy applications with different locales.

ignore(\Illuminate\Routing\Route|\Closure $routes): void

Use the ignore function within the group closure to exclude specific routes from localization. It takes a closure or a single route to ignore.

Configuration Management

addConfigLocales(array $locales): \Alnaggar\Turjuman\Localizer

Add additional locales to the supported locales in the configuration.

Example:

setConfigLocales(array $locales): \Alnaggar\Turjuman\Localizer

Set the supported locales in the configuration.

Example:

setConfigDefaultLocale(string $locale): \Alnaggar\Turjuman\Localizer

Set the default locale in the configuration.

Example:

getConfigAttributes(): \Alnaggar\Turjuman\GroupAttributes

Retrieve the configuration attributes.

Example:

Group Information

getCurrentGroup(): ?\Alnaggar\Turjuman\Group

Retrieve the Alnaggar\Turjuman\Group instance responsible for localizing the current route. Returns null if not found.

Example:

getCurrentAttributes(): \Alnaggar\Turjuman\GroupAttributes

Retrieve the current group attributes.

Example:

Locale Management

setCurrentLocale(string $locale): \Alnaggar\Turjuman\Locale

Set the current locale.

Example:

getLocale(string $locale): ?\Alnaggar\Turjuman\Locale

Retrieve an Alnaggar\Turjuman\Locale instance based on its code. Returns null if the locale is not supported.

Example:

getSupportedLocales(): array

Retrieve the current group's supported locales.

Example:

getDefaultLocale(): \Alnaggar\Turjuman\Locale

Retrieve the current group's default locale instance.

Example:

getCurrentLocale(): ?\Alnaggar\Turjuman\Locale

Retrieve the current locale instace. Returns null if the current route is not localized.

Example:

URL Localization

getLocalizedUrl(string $url, ?string $locale = null): ?string

Get the localized URL for the provided URL and locale. Returns null if the provided URL is external or not localized.

Example:

getNonLocalizedUrl(string $url): ?string

Get the non-localized URL for the provided URL. Returns null if the provided URL is external.

Example:

Localization Validation

isLocalizedUrl(string $url, string $method = 'GET'): bool

Check if the given URL is localized in any of the registered groups.

Example:

isLocalizedRoute(\Illuminate\Routing\Route|string|null $route = null): bool

Check if the provided route is localized within any of the registered groups. The check is done against the current route if null is passed.

Example:

Locale Validation

isSupportedLocale(string $locale): bool

Check if the provided locale is supported within the current group.

Example:

isCurrentLocale(string $locale): bool

Check if the provided locale is the current locale.

Example:

isDefaultLocale(string $locale): bool

Check if the provided locale is the default locale.

Example:

Miscellaneous

getLocalesByProperty(string $property, mixed $default = null): string

Retrieve an associative array of locale codes and their corresponding property values based on the current group's supported locales.

The $default optional parameter specifies the value to be returned if the property does not exist for a certain locale instance. If a closure is passed, the locale instance will be provided as a parameter. This function is useful for obtaining specific information or settings for each supported locale within the currently defined group. It provides flexibility by allowing a default value or behavior to be specified in case the requested property is not available for a particular locale.

Example:

getLocalizedPagePath(string $path, ?string $locale = null): string

Get the localized page path for the provided path and locale. It works for Laravel views and Inertia pages.

Example:

[!NOTE] Before using functions that rely on the current group or current attributes, it is essential to include an if statement to check whether the current route is localized.

Helper Function

The turjuman helper function can be used to localize URLs:

Contributing

If you find any issues or have suggestions for improvements, feel free to open an issue or submit a pull request on the GitHub repository.

Credits

License

Turjuman is open-sourced software licensed under the MIT license.


All versions of turjuman with dependencies

PHP Build Version
Package Version
Requires laravel/framework Version ^10.0
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 alnaggar/turjuman contains the following files

Loading the files please wait ...