|
| 1 | +# Locales helper |
| 2 | + |
| 3 | +In addition to the trait we also provide a helper class `\Astrotomic\Translatable\Locales` it's a singleton service available as `translatable.locales` and `\Astrotomic\Translatable\Locales` . |
| 4 | +This helper manages all locales available for translation and provides several methods. |
| 5 | + |
| 6 | +This class also implements the `ArrayAccess` interface to allow you to use it like a normal array. |
| 7 | + |
| 8 | +## Methods |
| 9 | + |
| 10 | +### load\(\) |
| 11 | + |
| 12 | +This method will \(re\)load all locales from the `translatable.locales` config - it's called during service instantiation so you will only need it if you change the config during runtime. |
| 13 | + |
| 14 | +{% hint style="info" %} |
| 15 | +If you don't have to do so to work with other packages we recommend to use the provided methods of this helper service to manipulate the available locales instead of changing the config during runtime. |
| 16 | +{% endhint %} |
| 17 | + |
| 18 | +### all\(\) |
| 19 | + |
| 20 | +**Alias:** `toArray()` |
| 21 | + |
| 22 | +Returns all available locales as an array - the structure differs from the config one, the return final generated array with combined country locales. |
| 23 | + |
| 24 | +```php |
| 25 | +[ |
| 26 | + 'en', |
| 27 | + 'de', |
| 28 | + 'es', |
| 29 | + 'es-MX', |
| 30 | + 'es-CO', |
| 31 | +] |
| 32 | +``` |
| 33 | + |
| 34 | +### current\(\) |
| 35 | + |
| 36 | +Returns the current locale string. |
| 37 | + |
| 38 | +### has\(string $locale\) |
| 39 | + |
| 40 | +Checks if the given locale is available in the configured set of locales. |
| 41 | + |
| 42 | +### get\(string $locale\) |
| 43 | + |
| 44 | +Returns the provided locale or `null` if it's not set. |
| 45 | + |
| 46 | +{% hint style="info" %} |
| 47 | +At all this isn't really useful except you want to build your own check if a locale is set. |
| 48 | +{% endhint %} |
| 49 | + |
| 50 | +### add\(string $locale\) |
| 51 | + |
| 52 | +Adds the given locale to the set of available locales. |
| 53 | + |
| 54 | +{% hint style="info" %} |
| 55 | +The set of available locales will keep unique and this method won't throw an exception if the locale is already present. |
| 56 | +{% endhint %} |
| 57 | + |
| 58 | +### forget\(string $locale\) |
| 59 | + |
| 60 | +Removes the given locale of the available locales set. |
| 61 | + |
| 62 | +{% hint style="info" %} |
| 63 | +This method won't throw an exception if the locale isn't present. |
| 64 | +{% endhint %} |
| 65 | + |
| 66 | +### getLocaleSeparator\(\) |
| 67 | + |
| 68 | +Returns the configured `translatable.locale_separator` locale separator used to combine language with country locales. |
| 69 | + |
| 70 | +### getCountryLocale\(string $locale, string $country\) |
| 71 | + |
| 72 | +Returns the formatted country based locale. |
| 73 | + |
| 74 | +### isLocaleCountryBased\(string $locale\) |
| 75 | + |
| 76 | +Checks if the given locale is a country specific locale. |
| 77 | + |
| 78 | +### getLanguageFromCountryBasedLocale\(string $locale\) |
| 79 | + |
| 80 | +Returns the language locale from given country based locale. |
| 81 | + |
0 commit comments