Skip to content

Locale

Source: shiva-core/shared/sh_locale.lua

Namespace-scoped translation manager with {placeholder} and positional {0}/{1} substitution, plus basic pluralisation. Runs on both server and client.

Availability

Locale is a shared global in every shiva module. No import required.

Loading Translations

Locale.load(locale, translations)

Load (or merge) a flat or nested translation table for a locale.

lua
Locale.load('en', {
    banking = {
        success = { transfer = 'Transfer successful' },
        error   = { insufficient = 'Not enough funds' },
    }
})

Locale.register(namespace, locale, translations)

Register module translations under a namespace. Called inside each module's locales/en.lua.

lua
-- locales/en.lua
Locale.register('banking', 'en', {
    ['success.transfer']    = 'Transfer of ${0} to {1} successful',
    ['error.insufficient']  = 'Insufficient funds',
})

Active Locale

Locale.set(locale)

Change the active locale.

lua
Locale.set('es')

Locale.current()

Return the active locale code.

lua
local code = Locale.current()  -- 'en'

Translation

Locale.t(namespace, key, ...) / Locale.t(key, params?, count?)

Two calling conventions are supported:

Namespace-aware (used by modules):

lua
-- Positional args replace {0}, {1}, …
Locale.t('banking', 'success.transfer', '100', 'Bob')
-- → 'Transfer of $100 to Bob successful'

Classic (flat dotted key):

lua
Locale.t('banking.error.insufficient', { amount = 50 })
-- Named params replace {amount} or :amount

Pluralisation (classic only):

lua
-- Translation value: { one = '1 fish', other = '{0} fish' }
Locale.t('fishing.caught', {}, count)

Locale.get(namespace, key, ...)

Alias for Locale.t with namespace-aware convention.

lua
Locale.get('banking', 'success.transfer', '100', 'Bob')

Locale.trans(...)

Alias for Locale.t.

lua
Locale.trans('banking', 'error.insufficient')

Released under the MIT License.