PHP Class Mcamara\LaravelLocalization\LaravelLocalization

Show file Open project: mcamara/laravel-localization Class Usage Examples

Protected Properties

Property Type Description
$app Illuminate\Foundation\Application Illuminate request class.
$baseUrl string Illuminate request class.
$configRepository Illuminate\Config\Repository Config repository.
$currentLocale string Current locale
$defaultLocale string Default locale
$request Illuminate\Routing\Request Illuminate request class.
$routeName string Name of the translation key of the current route, it is used for url translations
$router Illuminate\Routing\Router Illuminate router class.
$supportedLocales array Supported Locales
$translatedRoutes array An array that contains all routes that should be translated
$translator Illuminate\Translation\Translator Illuminate translator class.
$view Illuminate\View\Factory Illuminate view Factory.

Public Methods

Method Description
__construct ( ) Creates new instance.
checkLocaleInSupportedLocales ( string | boolean $locale ) : boolean Check if Locale exists on the supported locales array
createUrlFromUri ( string $uri ) : string Create an url from the uri
getConfigRepository ( ) : Illuminate\Config\Repository Returns the config repository for this instance
getCurrentLocale ( ) : string Returns current language
getCurrentLocaleDirection ( ) : string Returns current locale direction
getCurrentLocaleName ( ) : string Returns current locale name
getCurrentLocaleNative ( ) : string Returns current locale native name
getCurrentLocaleNativeReading ( ) : string Returns current language's native reading
getCurrentLocaleRegional ( ) : string Returns current regional
getCurrentLocaleScript ( ) : string Returns current locale script
getDefaultLocale ( ) : string Returns default locale
getLocalizedURL ( string | boolean $locale = null, string | false $url = null, array $attributes = [] ) : string | false Returns an URL adapted to $locale
getNonLocalizedURL ( string | false $url = null ) : string It returns an URL without locale (if it has it) Convenience function wrapping getLocalizedURL(false)
getRouteNameFromAPath ( string $path ) : string | false Returns the translation key for a given path
getSupportedLanguagesKeys ( ) : array Returns supported languages language key
getSupportedLocales ( ) : array Return an array of all supported Locales
getURLFromRouteNameTranslated ( string | boolean $locale, string $transKeyName, array $attributes = [] ) : string | false Returns an URL adapted to the route name and the locale given
hideDefaultLocaleInURL ( ) : boolean Returns the translation key for a given path
localizeURL ( string $url = null, string | boolean $locale = null ) : string Returns an URL adapted to $locale or current locale
setBaseUrl ( string $url ) Sets the base url for the site
setLocale ( string $locale = null ) : string Set and return current locale
setRouteName ( string $routeName ) Set current route name
setSupportedLocales ( array $locales ) Set and return supported locales
transRoute ( string $routeName ) : string Translate routes and save them to the translated routes array (used in the localize route filter)

Protected Methods

Method Description
checkUrl ( string $url ) : boolean Returns true if the string given is a valid url
extractAttributes ( boolean | false | null | string $url = false, string $locale = '' ) : array Extract attributes for current url
findTranslatedRouteByPath ( string $path, string $url_locale ) : string | false Returns the translated route for the path and the url given
findTranslatedRouteByUrl ( string | false | null $url, array $attributes, string $locale ) : string | false Returns the translated route for an url and the attributes given and a locale
getTranslatedRoutes ( ) : array Returns translated routes
substituteAttributesInRoute ( $attributes, string $route ) : string Change route attributes for the ones in the $attributes array
unparseUrl ( array | false $parsed_url ) : string Build URL using array data from parse_url
useAcceptLanguageHeader ( ) : boolean Returns the translation key for a given path

Method Details

__construct() public method

Creates new instance.
public __construct ( )

checkLocaleInSupportedLocales() public method

Check if Locale exists on the supported locales array
public checkLocaleInSupportedLocales ( string | boolean $locale ) : boolean
$locale string | boolean string|bool Locale to be checked
return boolean is the locale supported?

checkUrl() protected method

Returns true if the string given is a valid url
protected checkUrl ( string $url ) : boolean
$url string String to check if it is a valid url
return boolean Is the string given a valid url?

createUrlFromUri() public method

Create an url from the uri
public createUrlFromUri ( string $uri ) : string
$uri string Uri
return string Url for the given uri

extractAttributes() protected method

Extract attributes for current url
protected extractAttributes ( boolean | false | null | string $url = false, string $locale = '' ) : array
$url boolean | false | null | string to extract attributes, if not present, the system will look for attributes in the current call
$locale string
return array Array with attributes

findTranslatedRouteByPath() protected method

Returns the translated route for the path and the url given
protected findTranslatedRouteByPath ( string $path, string $url_locale ) : string | false
$path string Path to check if it is a translated route
$url_locale string Language to check if the path exists
return string | false Key for translation, false if not exist

findTranslatedRouteByUrl() protected method

Returns the translated route for an url and the attributes given and a locale
protected findTranslatedRouteByUrl ( string | false | null $url, array $attributes, string $locale ) : string | false
$url string | false | null Url to check if it is a translated route
$attributes array Attributes to check if the url exists in the translated routes array
$locale string Language to check if the url exists
return string | false Key for translation, false if not exist

getConfigRepository() public method

Returns the config repository for this instance
public getConfigRepository ( ) : Illuminate\Config\Repository
return Illuminate\Config\Repository Configuration repository

getCurrentLocale() public method

Returns current language
public getCurrentLocale ( ) : string
return string current language

getCurrentLocaleDirection() public method

Returns current locale direction
public getCurrentLocaleDirection ( ) : string
return string current locale direction

getCurrentLocaleName() public method

Returns current locale name
public getCurrentLocaleName ( ) : string
return string current locale name

getCurrentLocaleNative() public method

Returns current locale native name
public getCurrentLocaleNative ( ) : string
return string current locale native name

getCurrentLocaleNativeReading() public method

Returns current language's native reading
public getCurrentLocaleNativeReading ( ) : string
return string current language's native reading

getCurrentLocaleRegional() public method

Returns current regional
public getCurrentLocaleRegional ( ) : string
return string current regional

getCurrentLocaleScript() public method

Returns current locale script
public getCurrentLocaleScript ( ) : string
return string current locale script

getDefaultLocale() public method

Returns default locale
public getDefaultLocale ( ) : string
return string

getLocalizedURL() public method

Returns an URL adapted to $locale
public getLocalizedURL ( string | boolean $locale = null, string | false $url = null, array $attributes = [] ) : string | false
$locale string | boolean Locale to adapt, false to remove locale
$url string | false URL to adapt in the current language. If not passed, the current url would be taken.
$attributes array Attributes to add to the route, if empty, the system would try to extract them from the url.
return string | false URL translated, False if url does not exist

getNonLocalizedURL() public method

It returns an URL without locale (if it has it) Convenience function wrapping getLocalizedURL(false)
public getNonLocalizedURL ( string | false $url = null ) : string
$url string | false URL to clean, if false, current url would be taken
return string URL with no locale in path

getRouteNameFromAPath() public method

Returns the translation key for a given path
public getRouteNameFromAPath ( string $path ) : string | false
$path string Path to get the key translated
return string | false Key for translation, false if not exist

getSupportedLanguagesKeys() public method

Returns supported languages language key
public getSupportedLanguagesKeys ( ) : array
return array keys of supported languages

getSupportedLocales() public method

Return an array of all supported Locales
public getSupportedLocales ( ) : array
return array

getTranslatedRoutes() protected method

Returns translated routes
protected getTranslatedRoutes ( ) : array
return array translated routes

getURLFromRouteNameTranslated() public method

Returns an URL adapted to the route name and the locale given
public getURLFromRouteNameTranslated ( string | boolean $locale, string $transKeyName, array $attributes = [] ) : string | false
$locale string | boolean Locale to adapt
$transKeyName string Translation key name of the url to adapt
$attributes array Attributes for the route (only needed if transKeyName needs them)
return string | false URL translated

hideDefaultLocaleInURL() public method

Returns the translation key for a given path
public hideDefaultLocaleInURL ( ) : boolean
return boolean Returns value of hideDefaultLocaleInURL in config.

localizeURL() public method

Returns an URL adapted to $locale or current locale
public localizeURL ( string $url = null, string | boolean $locale = null ) : string
$url string URL to adapt. If not passed, the current url would be taken.
$locale string | boolean Locale to adapt, false to remove locale
return string URL translated

setBaseUrl() public method

Sets the base url for the site
public setBaseUrl ( string $url )
$url string Base url for the site

setLocale() public method

Set and return current locale
public setLocale ( string $locale = null ) : string
$locale string Locale to set the App to (optional)
return string Returns locale (if route has any) or null (if route does not have a locale)

setRouteName() public method

Set current route name
public setRouteName ( string $routeName )
$routeName string current route name

setSupportedLocales() public method

Set and return supported locales
public setSupportedLocales ( array $locales )
$locales array Locales that the App supports

substituteAttributesInRoute() protected method

Change route attributes for the ones in the $attributes array
protected substituteAttributesInRoute ( $attributes, string $route ) : string
$attributes array Array of attributes
$route string string route to substitute
return string route with attributes changed

transRoute() public method

Translate routes and save them to the translated routes array (used in the localize route filter)
public transRoute ( string $routeName ) : string
$routeName string Key of the translated string
return string Translated string

unparseUrl() protected method

Build URL using array data from parse_url
protected unparseUrl ( array | false $parsed_url ) : string
$parsed_url array | false Array of data from parse_url function
return string Returns URL as string.

useAcceptLanguageHeader() protected method

Returns the translation key for a given path
protected useAcceptLanguageHeader ( ) : boolean
return boolean Returns value of useAcceptLanguageHeader in config.

Property Details

$app protected property

Illuminate request class.
protected Application,Illuminate\Foundation $app
return Illuminate\Foundation\Application

$baseUrl protected property

Illuminate request class.
protected string $baseUrl
return string

$configRepository protected property

Config repository.
protected Repository,Illuminate\Config $configRepository
return Illuminate\Config\Repository

$currentLocale protected property

Current locale
protected string $currentLocale
return string

$defaultLocale protected property

Default locale
protected string $defaultLocale
return string

$request protected property

Illuminate request class.
protected Request,Illuminate\Routing $request
return Illuminate\Routing\Request

$routeName protected property

Name of the translation key of the current route, it is used for url translations
protected string $routeName
return string

$router protected property

Illuminate router class.
protected Router,Illuminate\Routing $router
return Illuminate\Routing\Router

$supportedLocales protected property

Supported Locales
protected array $supportedLocales
return array

$translatedRoutes protected property

An array that contains all routes that should be translated
protected array $translatedRoutes
return array

$translator protected property

Illuminate translator class.
protected Translator,Illuminate\Translation $translator
return Illuminate\Translation\Translator

$view protected property

Illuminate view Factory.
protected Factory,Illuminate\View $view
return Illuminate\View\Factory