The LocalizationService acts as a "gateway" to Umbraco data for operations which are related to Dictionary items and Languages.
Browse the API documentation for ILocalizationService.
Namespace: Umbraco.Cms.Core.Services
Assembly: Umbraco.Core.dll
All samples in this document will require references to the following dll:
Umbraco.Core.dll
All samples in this document will require the following using statements:
For Razor views:
If you wish to use the localization service in a class, you need to specify the ILocalizationService
interface in your constructor:
In Razor views, you can access the localization service through the @inject
directive:
Retrieving languages See examples on how to retrieve languages via the localization service - either individually or as a collection.
The localization service contains a number of methods for looking up languages. If you already know the ID of a specific language (eg. the default language has ID 1
), you can use the GetLanguageById
method to get the reference to that language:
Alternative, you can look up a language by its iso code via the GetLanguageByIsoCode
method:
The ISO code is a combination of the two-letter ISO 639-1 language code (lowercase) and two-letter ISO-3166 country code (uppercase). Eg. en-US
for English in the United States, en-GB
for English in the United Kingdom and da-DK
for Danish in Denmark.
Both methods will return an instance of the ILanguage interface, which has traditional properties like Id
and Key
, but also properties specific to the language like CultureName
, CultureInfo
and IsoCode
. You can see the API reference for further information on the properties of the interface.
If you need instead need a list of all installed languages, you can use the GetAllLanguages
method. It takes no parameters, and as such a returns a collection of all languages (with no pagination like some of the other services):
As shown in the example above, you can get the System.Globalization.CultureInfo
instance of each language, which determines how numbers, dates and similar should be either parsed or formatted in .NET.
Below you can see a full example of the examples shown above - including the necessary imports: