{"record":{"id":"ef5a49aaaca68049","repo":"kataras/iris","slug":"expected-language-index-to-be-lower-or-equal-than","errorCode":null,"errorMessage":"expected language index to be lower or equal than %d but got %d","messagePattern":"expected language index to be lower or equal than (.+?) but got (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"i18n/internal/catalog.go","lineNumber":105,"sourceCode":"\t}\n\n\treturn c, nil\n}\n\n// Set sets a simple translation message.\nfunc (c *Catalog) Set(tag language.Tag, key string, msgs ...catalog.Message) error {\n\t// fmt.Printf(\"Catalog.Set[%s] %s:\\n\", tag.String(), key)\n\t// for _, msg := range msgs {\n\t// \tfmt.Printf(\"%#+v\\n\", msg)\n\t// }\n\treturn c.builder.Set(tag, key, msgs...)\n}\n\n// Store stores the a map of values to the locale derives from the given \"langIndex\".\nfunc (c *Catalog) Store(langIndex int, kv Map) error {\n\tloc := c.getLocale(langIndex)\n\tif loc == nil {\n\t\treturn fmt.Errorf(\"expected language index to be lower or equal than %d but got %d\", len(c.Locales), langIndex)\n\t}\n\treturn loc.Load(c, kv)\n}\n\n/* Localizer interface. */\n\n// SetDefault changes the default language based on the \"index\".\n// See `I18n#SetDefault` method for more.\nfunc (c *Catalog) SetDefault(index int) bool {\n\tif index < 0 {\n\t\tindex = 0\n\t}\n\n\tif maxIdx := len(c.Locales) - 1; index > maxIdx {\n\t\treturn false\n\t}\n\n\t// callers should protect with mutex if called at serve-time.","sourceCodeStart":87,"sourceCodeEnd":123,"githubUrl":"https://github.com/kataras/iris/blob/7bedaf55a0b64bbb2248a5845a2c60d81a30996a/i18n/internal/catalog.go#L87-L123","documentation":"Catalog.Store writes a key/value translation map into the locale located by langIndex (index into c.Locales, i.e. the ordered languages given at construction). If langIndex is negative or >= len(Locales) there is no such locale and this error reports the allowed bound versus the given index.","triggerScenarios":"Calling catalog.Store(n, kv) (directly or via i18n loader wrappers) with an index outside 0..len(languages)-1 — e.g. iterating locales from a mismatched source list, or languages slice shrunk/changed after catalog creation.","commonSituations":"Loading translation files in arbitrary directory order while assuming sorted order matches the languages slice; adding a language to the folder but not to the languages list (or vice versa); off-by-one in index computation.","solutions":["Use langIndex values between 0 and len(catalog.Locales)-1","Derive langIndex from the same ordered languages slice passed to NewCatalog, not from directory iteration order","After adding a locale file, re-create the catalog with the languages list including it","Look up the index via the catalog's language matching instead of hardcoding"],"exampleFix":"// before\nfor i := 0; i < len(files); i++ {\n    c.Store(i, m) // files may exceed languages\n}\n// after\nfor i := range c.Locales {\n    if err := c.Store(i, maps[i]); err != nil { return err }\n}","handlingStrategy":"validation","validationCode":"func idxOf(langs []language.Tag, want language.Tag) (int, error) {\n    for i, l := range langs { if l == want { return i, nil } }\n    return -1, fmt.Errorf(\"language %v not registered\", want)\n}","typeGuard":"func validIndex(i, n int) bool { return i >= 0 && i < n }","tryCatchPattern":"if err := c.Store(idx, kv); err != nil {\n    if strings.Contains(err.Error(), \"expected language index\") {\n        log.Printf(\"langIndex %d out of bounds (%d locales)\", idx, len(c.Locales))\n    }\n    return err\n}","preventionTips":["Derive indices from the same ordered languages slice given to NewCatalog","Never assume directory iteration order equals language order","Rebuild the catalog whenever the language set changes","Iterate range c.Locales rather than external file counts"],"tags":["go","i18n","index-out-of-range","catalog"],"backgroundTag":"language-index-out-of-range","analyzedSha":"7bedaf55a0b64bbb2248a5845a2c60d81a30996a","analyzedAt":"2026-08-30T20:38:16.250Z","schemaVersion":2},"datasetVersion":"2026-08-30T23:17:21.991Z"}