danielmiessler.fabric/internal/plugins/template/text.go
Kayvan Sylvan e9b1b88d86 feat: replace hardcoded error strings with i18n translation keys
- Replace hardcoded error messages with `i18n.T()` calls across Go source files
- Add ~80 new translation keys to all locale files (en, de, es, fa, fr, it, ja, pt-BR, pt-PT, zh)
- Internationalize error strings in chat, attachment, storage, and template packages
- Internationalize error strings in plugin registry, server, and utility modules
- Internationalize githelper, notifications, patterns, and sessions modules
- Add i18n support for DigitalOcean, Gemini, and OpenAI-compatible providers
- Sort existing locale keys alphabetically in JSON files
- chore: incoming 2019 changelog entry
2026-02-21 13:28:52 -08:00

67 lines
1.6 KiB
Go

// Package template provides text transformation operations for the template system.
package template
import (
"fmt"
"strings"
"unicode"
"github.com/danielmiessler/fabric/internal/i18n"
)
// TextPlugin provides string manipulation operations
type TextPlugin struct{}
// toTitle capitalizes a letter if it follows a non-letter, unless next char is space
func toTitle(s string) string {
// First lowercase everything
lower := strings.ToLower(s)
runes := []rune(lower)
for i := range runes {
// Capitalize if previous char is non-letter AND
// (we're at the end OR next char is not space)
if i == 0 || !unicode.IsLetter(runes[i-1]) {
if i == len(runes)-1 || !unicode.IsSpace(runes[i+1]) {
runes[i] = unicode.ToUpper(runes[i])
}
}
}
return string(runes)
}
// Apply executes the requested text operation on the provided value
func (p *TextPlugin) Apply(operation string, value string) (string, error) {
debugf("TextPlugin: operation=%s value=%q", operation, value)
if value == "" {
return "", fmt.Errorf(i18n.T("template_text_empty_input"), operation)
}
switch operation {
case "upper":
result := strings.ToUpper(value)
debugf("TextPlugin: upper result=%q", result)
return result, nil
case "lower":
result := strings.ToLower(value)
debugf("TextPlugin: lower result=%q", result)
return result, nil
case "title":
result := toTitle(value)
debugf("TextPlugin: title result=%q", result)
return result, nil
case "trim":
result := strings.TrimSpace(value)
debugf("TextPlugin: trim result=%q", result)
return result, nil
default:
return "", fmt.Errorf(i18n.T("template_text_unknown_operation"), operation)
}
}