Download the PHP package kudashevs/accept-language without Composer

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

Accept-Language

Latest Version on Packagist Run Tests

This PHP package retrieves a user's language of preference from the HTTP Accept-Language request-header field.

Features

By default, the retrieved language uses the format similar to the Unicode Locale Identifier. This format consists of a mandatory 2-/3-letter primary language and a region subtag separated with an underscore (e.g., en_GB). However, the format is configurable and can be changed, as the set of subtags included in the final result.

Main package features:

The package goes with the built-in Laravel framework support. For more information see Laravel integration section.

Installation

You can install the package via composer:

You may also want to publish the configuration file (optional).

Usage

To retrieve a preferred language you need to instantiate the AcceptLanguage class and call a process method on the instance. This should be done before the point where you want the user's preferred language (for example, in a front controller or in a middleware). Note: if you don't call the process method, the values will remain empty.

Note: The AcceptLanguage class, at the moment of instantiation, can throw a few exceptions: InvalidOptionType, InvalidOptionValue, InvalidLogEventName, InvalidLogLevelName. All of these exceptions extend a common built-in InvalidArgumentException class, so they are easy to deal with.

Once obtained, the preferred language value can be accessed in any part of your application through these methods:

If you need the original HTTP Accept-Language header, it is available via the getHeader method.

Configuration

After publishing, the configuration settings are located in the config/accept-language.php file.

There configuration options are currently supported:

1 - the default_language option should contain a valid Language Tag (it will be formatted according to the settings)
2 - the accepted_languages option should include valid Language Tags only (the primary subtags are limited to 2-/3-letters for now)
3 - the separator can accept any string value, however it is recommended to use the URL Safe Alphabet.

Notes

Some options require additional explanations:

Important note! the package supports the - and _ separators by default. If you want to use any other separator, use the separator option.

Important note! the values of the accepted_languages option will be formatted according to the settings. Therefore, if you want to retrieve languages including script subtags you should enable the use_script_subtag option.

Let's dig deeper in the matching behavior. Let's assume that the header is fr-CH, fr;q=0.9, *;q=0.5: exact_match_only option accepted_languages option       the result language
false ['fr'] the first tag with the quality 1 (derivative case)
false ['fr-ch'] the first tag with the quality 1
true ['fr'] the second tag with the quality 0.9
true ['fr-ch'] the first tag with the quality 1
, but, when we swap the first two tags and the Accept-Language header becomes fr, fr-CH;q=0.9, *;q=0.5: exact_match_only option accepted_languages option       the result language
false ['fr'] the first tag with the quality 1
false ['fr-ch'] the second tag with the quality 0.9
true ['fr'] the first tag with the quality 1
true ['fr-ch'] the second tag with the quality 0.9

Logging

The package is capable to log information gathered throughout the execution process. To start the logging process, you must provide an instance of Psr\Log\LoggerInterface implementation to the useLogger method.

Log events

To distinguish different stages of the execution process the package introduces log events. If you want to log specific events only, please add these events to the log_only option. If the log_only is empty, the package logs all known events.

Usage example

Let's imaging that we have a web application that uses three different languages: American, British, and Canadian English. We want to redirect users according to their HTTP Accept-Language header settings to specific sections: en_US, en_GB, en_CA. All routes are set correctly, and we just want to retrieve the preferred language, if user has any, to redirect them.

To work properly, the package requires us to provide two initial options: default_language let's give it the value en_US accepted_languages let's give it the value ['en_US', 'en_GB', 'en_CA']

These options instruct the package to retrieve only the values that are listed in the accepted_languages option. If one of the language tags in an HTTP Accept-Language header matches any of these values, it will be retained for the further processing. If none of them matches the listed values, the default language will be returned.

Laravel integration

Install the package. If you don't use auto-discovery just add a ServiceProvider to the config/app.php file.

The AcceptLanguageServiceProvider will instantiate the AcceptLanguage class and apply some initial configuration settings. Then, it will call the process method. However, you can comment it and call the proccess method in whatever place you want. After being registered, the AcceptLanguage instance becomes accessible via the Laravel's service container. The instance can be accessed by the class name or through an alias (e.g app('acceptlanguage')).

If you want to add a Laravel Facade, add it to the aliases array in the config/app.php file.

If you want to log the execution process, don't forget to set the log_activity option to true.

The configuration settings are located in the config/accept-language.php file.

for more information about different options, please refer to the Options section

If you want to change the default values, don't forget to publish the configuration file.

Testing

References

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Note: Please make sure to update tests as appropriate.

License

The MIT License (MIT). Please see the License file for more information.


All versions of accept-language with dependencies

PHP Build Version
Package Version
Requires php Version ^8.1
psr/log Version ^1.0|^2.0|^3.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 kudashevs/accept-language contains the following files

Loading the files please wait ....