Skip to content

Commit e5069e5

Browse files
feat: implement client-side translation tool with GitHub API, caching, and data structures
1 parent 0796a36 commit e5069e5

File tree

1 file changed

+30
-16
lines changed

1 file changed

+30
-16
lines changed

docs/assets/js/translate.js

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -486,15 +486,17 @@ function showGlossaryUpdateNotification() {
486486
setTimeout(() => notification.remove(), 30000);
487487
}
488488

489-
// Hash simples para strings
490-
String.prototype.hashCode = function() {
491-
let hash = 0;
492-
for (let i = 0; i < this.length; i++) {
493-
const char = this.charCodeAt(i);
494-
hash = ((hash << 5) - hash) + char;
495-
hash = hash & hash;
496-
}
497-
return hash;
489+
// Hash cyrb53 para strings (melhor distribuição, menos colisões que hashCode simples)
490+
String.prototype.hashCode = function(seed = 0) {
491+
let h1 = 0xdeadbeef ^ seed, h2 = 0x41c6ce57 ^ seed;
492+
for (let i = 0, ch; i < this.length; i++) {
493+
ch = this.charCodeAt(i);
494+
h1 = Math.imul(h1 ^ ch, 2654435761);
495+
h2 = Math.imul(h2 ^ ch, 1597334677);
496+
}
497+
h1 = Math.imul(h1 ^ (h1 >>> 16), 2246822507) ^ Math.imul(h2 ^ (h2 >>> 13), 3266489909);
498+
h2 = Math.imul(h2 ^ (h2 >>> 16), 2246822507) ^ Math.imul(h1 ^ (h1 >>> 13), 3266489909);
499+
return 4294967296 * (2097151 & h2) + (h1 >>> 0);
498500
};
499501

500502
// Substitui todas as variáveis {{VAR}} pelos valores reais
@@ -1043,16 +1045,26 @@ function validateVariableSyntax(text) {
10431045
}
10441046

10451047
// Verificar variáveis com formato inválido
1046-
// Procurar por {{ não seguido de letras/números/_
1047-
const badOpen = text.match(/\{\{(?![A-Z_0-9]+\}\})/g);
1048-
if (badOpen && badOpen.length > 0) {
1049-
// Verificar se não é apenas um caso de variável lowercase
1050-
const lowercaseVars = text.match(/\{\{[a-z_0-9]+\}\}/gi);
1051-
if (lowercaseVars) {
1052-
errors.push(`Variáveis devem estar em MAIÚSCULAS: ${lowercaseVars.slice(0, 3).join(', ')}`);
1048+
// Detectar qualquer variável que não está em MAIÚSCULAS (CamelCase, lowercase, mixed)
1049+
const allVarsPattern = /\{\{([^}]+)\}\}/g;
1050+
let match;
1051+
const invalidCaseVars = [];
1052+
1053+
while ((match = allVarsPattern.exec(text)) !== null) {
1054+
const varContent = match[1];
1055+
// Variável válida: apenas letras maiúsculas, números e underscores
1056+
const isValidFormat = /^[A-Z_0-9]+$/.test(varContent);
1057+
1058+
if (!isValidFormat && varContent.trim().length > 0) {
1059+
invalidCaseVars.push(`{{${varContent}}}`);
10531060
}
10541061
}
10551062

1063+
if (invalidCaseVars.length > 0) {
1064+
const examples = invalidCaseVars.slice(0, 3).join(', ');
1065+
errors.push(`Variáveis devem estar em MAIÚSCULAS: ${examples}`);
1066+
}
1067+
10561068
// Procurar por }} órfãos (sem {{ antes)
10571069
let tempText = text;
10581070
while (tempText.includes('{{') && tempText.includes('}}')) {
@@ -1980,6 +1992,8 @@ async function validateToken() {
19801992

19811993
if (response.ok) {
19821994
githubUser = await response.json();
1995+
// Salvar username no localStorage para uso em outras páginas (glossary-admin)
1996+
localStorage.setItem('github_username', githubUser.login);
19831997
updateAuthButton();
19841998
} else {
19851999
// Token inválido

0 commit comments

Comments
 (0)