Documentation
¶
Overview ¶
Package i18n loads translation catalogs and resolves the request locale.
Catalog contract: every catalog FS (the embedded core one and any returned by a module's kernel.CatalogProvider) has files at "catalog/<module>.<locale>.json", each a flat {"key": "text"} object. Keys are stored with the module prefix: "app.name" in core.en.json is looked up as "core.app.name". Duplicate full keys across catalogs are an error, and so is a key that already starts with "<module>." (it would be stored as "core.core.x"). T falls back locale → en → the key itself.
Index ¶
- Variables
- func Locale(ctx context.Context) string
- func LocaleFromRequest(r *http.Request, userPref string) string
- func LocalePrefix(loc, site string) string
- func LocalizePath(loc, site, p string) string
- func NormalizeArabic(s string) string
- func RequestLocale(ctx context.Context) (string, bool)
- func SplitLocalePath(p string) (loc, rest string)
- func WithLocale(ctx context.Context, locale string) context.Context
- type Bundle
Constants ¶
This section is empty.
Variables ¶
var DefaultLocale = "ar"
DefaultLocale is used when neither the user preference nor Accept-Language names a supported locale. GoRun is Arabic/RTL-first.
var NativeNames = map[string]string{"ar": "العربية", "en": "English"}
NativeNames are the supported locales in their own language (language switchers).
var Supported = []string{"ar", "en"}
Supported lists the locales LocaleFromRequest may return.
Functions ¶
func LocaleFromRequest ¶
LocaleFromRequest picks the locale: a supported userPref wins, then the highest-weighted supported Accept-Language entry, then DefaultLocale.
func LocalePrefix ¶
LocalePrefix is the public path prefix of loc on a site whose locale is site: "/<loc>" for another supported locale, "" for the site locale, "" (unprefixed) or an unsupported one.
func LocalizePath ¶
LocalizePath puts p (a site path starting with "/") under the prefix of loc.
func NormalizeArabic ¶
NormalizeArabic folds Arabic spelling variants so that search matches what a reader considers the same word. It is applied to both the indexed text and the query, and is idempotent. Text outside the Arabic block is returned unchanged (Latin case folding is left to the search engine).
Rules:
- alef with hamza or madda, and alef wasla (أ إ آ ٱ) → bare alef ا
- alef maqsura ى → ي, and taa marbuta ة → ه (the two common spelling swaps)
- waw with hamza ؤ → و, and yaa with hamza ئ → ي: writers often drop the seat's hamza, so the bare letter is what a query is most likely to contain. The standalone hamza ء is a letter of its own and is kept.
- tatweel ـ, the harakat and other combining marks U+064B–U+065F (tanween, shadda, sukun, and the combining hamza above/below, so decomposed forms fold like the precomposed ones), the superscript alef U+0670 and the combining Quranic annotation marks U+06D6–U+06DC, U+06DF–U+06E4, U+06E7–U+06E8, U+06EA–U+06ED are removed. The visible Quranic symbols ( ۞ ۩) are kept.
- Arabic-Indic ٠-٩ and Extended (Persian) ۰-۹ digits → ASCII 0-9.
func RequestLocale ¶ added in v0.11.0
RequestLocale returns the locale attached by WithLocale, if any. Unlike Locale it does not fall back to DefaultLocale, so callers can distinguish an explicit request locale from an unset context.
func SplitLocalePath ¶
SplitLocalePath splits a leading supported-locale segment off p: "/en/post/x" → "en", "/post/x"; "/en" → "en", "/". Any other path is returned whole with loc "".