hs.locale
ModuleRetrieve information about the user's Language & Region settings, and respond to changes.
Locales encapsulate linguistic, cultural, and technological conventions — things like the symbol used for a decimal separator, or the way dates and calendars are formatted.
Reading locale information
console.log("Current locale: " + hs.locale.current())
const info = hs.locale.details()
console.log("Uses metric: " + info.usesMetricSystem)
Watching for changes
hs.locale.on('change', () => {
console.log("Locale settings changed: " + JSON.stringify(hs.locale.details()))
})
Event Types
The events this module's watchers emit, and when. In TypeScript, each type is a union of its event names that you can use as a type of your own:
HSLocaleEvent
An event name for hs.locale.on(), hs.locale.off() and hs.locale.once()
| Event | Description |
|---|---|
"change" |
Any of the user's locale settings changed |
Properties
This module has no properties.
Methods
hs.locale.availableLocales() -> string[]
Returns the identifiers for all locales available on the system.
hs.locale.availableLocales() -> string[]
["en_US", "de_CH", "ja_JP"]).hs.locale.availableLocales().forEach(id => console.log(id))
hs.locale.current() -> string
Returns the user's currently selected locale identifier.
hs.locale.current() -> string
"en_US").console.log("Current locale: " + hs.locale.current())
hs.locale.preferredLanguages() -> string[]
Returns the user's preferred languages, in priority order.
hs.locale.preferredLanguages() -> string[]
hs.locale.preferredLanguages().forEach(l => console.log(l))
hs.locale.details(identifier) -> object
Returns detailed information about the current or a specified locale.
hs.locale.details(identifier) -> object
| Name | Type | Description |
|---|---|---|
| identifier optional | string | A locale identifier from availableLocales(). If omitted, the user's currently selected locale is used. |
| Field | Type | Description |
|---|---|---|
| identifier | string | The locale's identifier, e.g. "de_CH" |
| languageCode optional | string | e.g. "de" |
| countryCode optional | string | e.g. "CH" |
| scriptCode optional | string | e.g. "Latn" |
| variantCode optional | string | |
| currencyCode optional | string | e.g. "CHF" |
| currencySymbol optional | string | |
| decimalSeparator optional | string | |
| groupingSeparator optional | string | |
| collationIdentifier | string | |
| measurementSystem | "metric" | "us" | "uk" | |
| usesMetricSystem | boolean | |
| temperatureUnit optional | string | e.g. "Celsius" |
| timeFormatIs24Hour | boolean | |
| quotationBeginDelimiter optional | string | |
| quotationEndDelimiter optional | string | |
| alternateQuotationBeginDelimiter optional | string | |
| alternateQuotationEndDelimiter optional | string | |
| calendar | object | The locale's calendar |
| identifier | string | e.g. "gregorian" |
| firstWeekday | number | The first day of the week, where 1 is Sunday |
| minimumDaysInFirstWeek | number | |
| amSymbol | string | |
| pmSymbol | string | |
| eraSymbols | string[] | |
| longEraSymbols | string[] | |
| monthSymbols | string[] | |
| shortMonthSymbols | string[] | |
| standaloneMonthSymbols | string[] | |
| shortStandaloneMonthSymbols | string[] | |
| veryShortMonthSymbols | string[] | |
| veryShortStandaloneMonthSymbols | string[] | |
| quarterSymbols | string[] | |
| shortQuarterSymbols | string[] | |
| standaloneQuarterSymbols | string[] | |
| shortStandaloneQuarterSymbols | string[] | |
| weekdaySymbols | string[] | |
| shortWeekdaySymbols | string[] | |
| standaloneWeekdaySymbols | string[] | |
| shortStandaloneWeekdaySymbols | string[] | |
| veryShortWeekdaySymbols | string[] | |
| veryShortStandaloneWeekdaySymbols | string[] |
const info = hs.locale.details("de_CH")
console.log(info.currencySymbol + " " + info.decimalSeparator)
console.log(info.calendar.monthSymbols.join(", "))
hs.locale.localizedName(localeCode, baseLocaleCode) -> [String: String]
Returns the localized display name for a locale identifier.
hs.locale.localizedName(localeCode, baseLocaleCode) -> [String: String]
| Name | Type | Description |
|---|---|---|
| localeCode | string | The locale identifier to look up (e.g. "de_CH"). Must be one of the strings returned by availableLocales(). |
| baseLocaleCode optional | string | The locale to display the name in. If omitted, the user's currently selected locale is used. Must be one of the strings returned by availableLocales(). |
name (e.g. "German") and nameWithDialect (e.g. "German (Switzerland)"), or null if either locale code is invalid.const name = hs.locale.localizedName("de_CH")
console.log(name.name + " / " + name.nameWithDialect)
hs.locale.on(event, listener) -> None
Register a listener that fires whenever any of the user's locale settings change.
Read current() or details() inside the listener to inspect the new state.
hs.locale.on(event, listener) -> None
| Name | Type | Description |
|---|---|---|
| event | HSLocaleEvent | The event to listen for (the only event this module emits) |
| listener | () => void | Called with no arguments when locale settings change |
| Event | Description |
|---|---|
"change" |
Any of the user's locale settings changed |
try {
hs.locale.on('change', () => console.log("Locale changed to: " + hs.locale.current()))
} catch (err) {
console.error(err.message)
}
hs.locale.off(event, listener) -> None
Remove a previously registered locale change listener.
hs.locale.off(event, listener) -> None
| Name | Type | Description |
|---|---|---|
| event | HSLocaleEvent | The event the listener was registered for |
| listener | () => void | The function originally passed to on |
| Event | Description |
|---|---|
"change" |
Any of the user's locale settings changed |
const onChange = () => console.log("changed")
hs.locale.on('change', onChange)
// later…
hs.locale.off('change', onChange)
hs.locale.once(event, listener) -> None
Register a listener that fires at most once, the next time locale settings change.
hs.locale.once(event, listener) -> None
| Name | Type | Description |
|---|---|---|
| event | HSLocaleEvent | The event to listen for |
| listener | () => void | Called once, then automatically removed |
| Event | Description |
|---|---|
"change" |
Any of the user's locale settings changed |
try {
hs.locale.once('change', () => console.log("First change detected"))
} catch (err) {
console.error(err.message)
}