mirror of
https://github.com/Mezeporta/Erupe.git
synced 2026-05-06 22:35:11 +02:00
feat(i18n): per-session language preference and !lang command
Phase A plumbing for #188. Adds a users.language column (migration 0022), UserRepo.GetLanguage/SetLanguage, and Session.Lang()/SetLang accessors so future phases can resolve localized content per session instead of falling back to the server-wide config.Language. The preference is loaded from the DB on login and persisted via a new !lang <en|jp|fr|es> chat command that shows the current language when called without an argument, validates the code (case-insensitive), and replies in the newly selected language so the switch is visible immediately. An empty stored value falls back to config.Language. sys_language.go exposes getLangStringsFor(code) as the new dispatch primitive; getLangStrings(server) is now a thin wrapper so existing callers keep working unchanged. isSupportedLang + supportedLangs keep the !lang validator in sync with the dispatcher. Localized quest/scenario content and per-session i18n lookups in existing handlers are deliberately out of scope for phase A — this commit ships only the plumbing so it can be reviewed and deployed independently.
This commit is contained in:
@@ -144,6 +144,33 @@ func parseChatCommand(s *Session, command string) {
|
||||
} else {
|
||||
sendDisabledCommandMessage(s, commands["Timer"])
|
||||
}
|
||||
case commands["Language"].Prefix:
|
||||
if commands["Language"].Enabled || s.isOp() {
|
||||
// Use the session's *current* i18n table so replies come back in
|
||||
// the language the player was using before the change — the
|
||||
// success message reflects the new language via its argument.
|
||||
i := getLangStringsFor(s.Lang())
|
||||
if len(args) < 2 {
|
||||
sendServerChatMessage(s, fmt.Sprintf(i.commands.lang.current, s.Lang()))
|
||||
sendServerChatMessage(s, fmt.Sprintf(i.commands.lang.usage, s.server.erupeConfig.CommandPrefix+commands["Language"].Prefix))
|
||||
} else {
|
||||
requested := strings.ToLower(args[1])
|
||||
if !isSupportedLang(requested) {
|
||||
sendServerChatMessage(s, fmt.Sprintf(i.commands.lang.invalid, requested))
|
||||
} else {
|
||||
if err := s.server.userRepo.SetLanguage(s.userID, requested); err != nil {
|
||||
s.logger.Error("Failed to persist language preference", zap.Error(err), zap.String("lang", requested))
|
||||
}
|
||||
s.SetLang(requested)
|
||||
// Reply in the *new* language so the player immediately
|
||||
// sees the server switched.
|
||||
newI := getLangStringsFor(requested)
|
||||
sendServerChatMessage(s, fmt.Sprintf(newI.commands.lang.success, requested))
|
||||
}
|
||||
}
|
||||
} else {
|
||||
sendDisabledCommandMessage(s, commands["Language"])
|
||||
}
|
||||
case commands["PSN"].Prefix:
|
||||
if commands["PSN"].Enabled || s.isOp() {
|
||||
if len(args) > 1 {
|
||||
|
||||
Reference in New Issue
Block a user