Before: You had two nearly identical functions:
traduzirAutomatico()- for PortuguesetraduzirParaES()- for Spanish
After: Created a single reusable function:
function traduzirPara(texto, idioma, onSuccess)This eliminates ~40 lines of duplicate code!
Before: You had two separate functions:
enviarTraducao()- for translated messagesenviarSemTraducao()- for non-translated messages
After: Created a single unified function:
function enviarMensagem(user, original, traduzido)The function automatically detects if translation is needed by comparing strings.
Before: This code was repeated in multiple places:
var nome = escaparHTML(user.name);
var countryCode = paisUsuario[user.externalIp] || "UN";
var emoji = bandeiraEmoji(countryCode);
var avatar = "(•‿•)";After: Created helper functions:
function getUserCountryCode(user)
function getUserDisplayData(user)Before: URLs and values scattered throughout:
"http://ip-api.com/json/" + ip + "?fields=countryCode"
"http://translate.googleapis.com/translate_a/single?..."
"https://flagcdn.com/w20/" + countryCodeAfter: Centralized configuration:
var CONFIG = {
intervaloAvatar: 15,
molduraURL: "http://i.imgur.com/9uYxq8W.png",
apiURLs: {
geo: "http://ip-api.com/json/",
translate: "http://translate.googleapis.com/translate_a/single",
flag: "https://flagcdn.com/w20/"
}
};Before:
var uri = stripColors(textoOriginal);
uri = encodeURIComponent(uri);After: Combined into single line in traduzirPara():
var uri = encodeURIComponent(stripColors(texto));✅ Reduced from ~300 lines to ~240 lines (20% reduction)
✅ Easier to maintain - Change translation logic once, not twice
✅ Less error-prone - No risk of updating one function but forgetting the other
✅ More flexible - Easy to add new languages (just call traduzirPara(texto, "fr", callback))
✅ Better organization - Related constants grouped together
✅ Clearer intent - Function names clearly describe what they do
// Old way: Write another 30-line function
// New way: One line!
traduzirPara(texto, "fr", function(traducao) {
// handle French translation
});// Old way: Find and replace in multiple places
// New way: Change once in CONFIG
CONFIG.apiURLs.flag = "https://new-api.com/flags/";- All functionality is preserved
- No changes to the visual output
- Same API/event structure
- Same caching logic
- Same performance characteristics