LanguageAccept
in package
The language accept class retrieves information from a user's browser and returns it in an organized manner so that the application can properly localize itself to serve the user properly.
Please note that, Spitfire will not automatically localize your application, this is a responsibility you have to carry yourself. It will provide you with localization stub classes that you can extend, and a helper function that should ease the process of localizing. But the routing and, concrete implementation is your task.
Table of Contents
- $language : string
- The language part of the locale as described by ISO-638. This is a 2 character string containing the information about which language the user is requesting
- $locale : string
- The locale of the language. This is relevant for languages that are spoken in different areas of the world, causing them to have minor variations between them.
- $priority : float
- The priority is just a float indicating how much precedence this locale has.
- __construct() : mixed
- Creates a new AcceptLanguage header segment object. This object contains information about the language the browser requested from the application to construct pages that are properly localized.
- getLanguage() : string
- Returns the ISO-639 code of the language. Although the RFC allows for some strange tags, we enforce a 2 character language code for the sake of simplicity and coherence.
- getLocale() : string
- The "subtag", as called by the RFC, specifies which variation of the language we're localizing for. This for example will be "GB" in the event of "en-gb" or "us" in the case of using a "en-us" locale.
- getPriority() : float
- The priority of the language. This is only used for sorting the locales when received from the user-agent since, in theory, you could have them sent in an unsorted manner.
- makeLocales() : array<string|int, string>
- Receives a string that contains a locale string (like 'en' or 'en-gb') and then returns the language and locale in an array like [en] or [en, gb] that we can then use to construct the locale.
- makePriority() : float
- Reads the priority assigned to the language accept header and translates it into a numeric value that we can use to properly sort.
- validateFormat() : bool
- Validates the format of a accep-language segment. This ensures that the request was sent with a proper format and can be parsed by us.
Properties
$language
The language part of the locale as described by ISO-638. This is a 2 character string containing the information about which language the user is requesting
private
string
$language
$locale
The locale of the language. This is relevant for languages that are spoken in different areas of the world, causing them to have minor variations between them.
private
string
$locale
E.g. fr-fr and fr-ca which is the french spoken in France and Canada respectively.
$priority
The priority is just a float indicating how much precedence this locale has.
private
float
$priority
This can be any value in the [0,1) range. We do not validate this, the browser should do it, but disregard of the rule is not dangerous.
Methods
__construct()
Creates a new AcceptLanguage header segment object. This object contains information about the language the browser requested from the application to construct pages that are properly localized.
public
__construct([string $str = null ]) : mixed
Parameters
- $str : string = null
Return values
mixed —getLanguage()
Returns the ISO-639 code of the language. Although the RFC allows for some strange tags, we enforce a 2 character language code for the sake of simplicity and coherence.
public
getLanguage() : string
Return values
string —getLocale()
The "subtag", as called by the RFC, specifies which variation of the language we're localizing for. This for example will be "GB" in the event of "en-gb" or "us" in the case of using a "en-us" locale.
public
getLocale() : string
Return values
string —getPriority()
The priority of the language. This is only used for sorting the locales when received from the user-agent since, in theory, you could have them sent in an unsorted manner.
public
getPriority() : float
Spitfire automatically sorts the locales by priority, so there's no need for the application to recheck this.
Return values
float —makeLocales()
Receives a string that contains a locale string (like 'en' or 'en-gb') and then returns the language and locale in an array like [en] or [en, gb] that we can then use to construct the locale.
protected
makeLocales(string $localeStr) : array<string|int, string>
Parameters
- $localeStr : string
Return values
array<string|int, string> —makePriority()
Reads the priority assigned to the language accept header and translates it into a numeric value that we can use to properly sort.
protected
makePriority(string $priority) : float
It would be an interesting consideration to leave the value unparsed, since the spec states that the leading 0 is required, and therefore string sorting would be effectively as efficient as float parsing.
Parameters
- $priority : string
Return values
float —validateFormat()
Validates the format of a accep-language segment. This ensures that the request was sent with a proper format and can be parsed by us.
protected
validateFormat(string $str) : bool
It'd be interesting to move this to the AcceptLanguageParser class, which could evaluate the regular expression just once and validate the entire header in one fell swoop.
Parameters
- $str : string