Skip to content

Commit

Permalink
Now saving language across sessions
Browse files Browse the repository at this point in the history
  • Loading branch information
mesacarlos committed May 5, 2019
1 parent 04ce0c5 commit e3fb367
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 91 deletions.
10 changes: 10 additions & 0 deletions browserNotes2.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

<!--- BrowserNotes 2 --->
<script src="script/service.js"></script>
<script defer src="script/lang.js"></script>
<script src="script/utils.js"></script>
<script src="script/fileReader.js"></script>
<script src="script/fileWriter.js"></script>
Expand All @@ -40,6 +41,15 @@
<a class="dropdown-item" href="#" id="glinkbtn" onclick="saveToLink()"></a>
</div>
</li>
<li class="nav-item dropdown" style="display: none;">
<a class="nav-link dropdown-toggle" data-toggle="dropdown" href="#" role="button" aria-haspopup="true" aria-expanded="false" id="note-dropdown">Note</a>
<div class="dropdown-menu">
<a class="dropdown-item" href="#" id="addNote" onclick="addNote()">Añadir nota</a>
<a class="dropdown-item" href="#" id="deleteNote" onclick="deleteNote()">Borrar nota</a>
<div class="dropdown-divider"></div>
<a class="dropdown-item" href="#" onclick="openNote(0)">Por defecto</a>
</div>
</li>
<li class="nav-item">
<a class="nav-link" href="#" id="chpwd" onclick="chPwd(lang.change_password)"></a>
</li>
Expand Down
99 changes: 99 additions & 0 deletions script/lang.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/**
* Update document language
*/
function setLanguage(locale){
//Used to check if title/content changed
var oldLang = lang;
// This saves user preference in localStorage to
// persist locale across windows (and sessions)
window.localStorage.userLocale = locale;
//Set dialog phrases
switch(locale){
case "en_US":
lang = {
"title": "Encrypted note",
"note_title": "Untitled note",
"note_content": "Hello, World!",
"save_to_file": "Save to file",
"file": "File",
"language": "Language",
"load_file": "Load file",
"change_password": "Change password",
"generate_link": "Generate link",
"password_not_provided": "Password is not set. Please type a password and retry.",
"pwd_ask": "Please type your password.",
"pwd_change": "Please type your new password.",
"pwd_change_confirm": "Confirm your password",
"pwd_change_error": "Passwords do not match so password was not changed.",
"pwd_change_correct": "Done. Save your note to finish password change.",
"get_parameters_set": "Done. Remember to save page to bookmarks!",
"encrypted_note": "Encrypted Note",
"auto_encode_toggle": "Automatic link generation was set to ",
"saved": "Saved.",
"ok_modal": "OK",
"password_modal": "Password",
"file_loaded": "File loaded successfully.",
"save_storage": "Save to WebStorage",
"load_storage": "Load from WebStorage"
}
break;
case "es_ES":
lang = {
"title": "Nota encriptada",
"note_title": "Nota sin título",
"note_content": "¡Hola mundo!",
"save_to_file": "Guardar a archivo",
"file": "Archivo",
"language": "Idioma",
"load_file": "Cargar archivo",
"change_password": "Cambiar contraseña",
"generate_link": "Generar enlace",
"password_not_provided": "No has introducido contraseña. Inserta una e inténtalo de nuevo.",
"pwd_ask": "Por favor, introduce la contraseña.",
"pwd_change": "Escribe tu nueva contraseña",
"pwd_change_confirm": "Confirma tu contraseña",
"pwd_change_error": "Las contraseñas no coinciden por lo que tu contraseña no ha sido cambiada.",
"pwd_change_correct": "Hecho. Guarda la nota para guardar la nueva contraseña.",
"get_parameters_set": "Hecho. Recuerda guardar la página en marcadores o descargar el fichero.",
"encrypted_note": "Nota encriptada",
"auto_encode_toggle": "La generacion automatica de enlaces ha sido cambiada a ",
"saved": "Guardado.",
"ok_modal": "Aceptar",
"password_modal": "Contraseña",
"file_loaded": "Archivo cargado con éxito.",
"save_storage": "Guardar WebStorage",
"load_storage": "Leer WebStorage"
}
break;
default:
console.error("No language set");
}
//Set navbar phrases
document.title = lang.title;
document.getElementById("save").textContent = lang.save_to_file;
document.getElementById("load").textContent = lang.load_file;
document.getElementById("chpwd").textContent = lang.change_password;
document.getElementById("glinkbtn").textContent = lang.generate_link;
document.getElementById("savestorage").textContent = lang.save_storage;
document.getElementById("loadstorage").textContent = lang.load_storage;
document.getElementById("file-dropdown").textContent = lang.file;
document.getElementById("lang-dropdown").textContent = lang.language;
document.getElementById("modalSetPasswordButton").textContent = lang.ok_modal;
document.getElementById("modalChangePasswordButton").textContent = lang.ok_modal;
document.getElementById("modalSetPasswordTitle").textContent = lang.password_modal;
document.getElementById("modalSetPasswordInput").placeholder = lang.password_modal;
document.getElementById("modalSetPasswordDescription").textContent = lang.pwd_ask;
document.getElementById("modalChangePasswordTitle").textContent = lang.password_modal;
document.getElementById("modalChangePasswordInput1").placeholder = lang.password_modal;
document.getElementById("modalChangePasswordInput2").placeholder = lang.pwd_change_confirm;
if(!oldLang || oldLang.note_title == getTitle())
document.getElementById("title").innerText = lang.note_title;
if(!oldLang || oldLang.note_content == getText())
document.getElementById("main").innerText = lang.note_content;
}

if (typeof window.localStorage.userLocale == "undefined") {
//User did not select language, changing to default
window.localStorage.userLocale = "en_US";
}
setLanguage(window.localStorage.userLocale);
91 changes: 0 additions & 91 deletions script/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ $(document).ready(function() {
jsonFile.version = 1;
jsonFile.notes = new Array();
jsonFile.notes[index] = new Object();
setLanguage("en_US");
if(window.location.search){
loadURL();
}
Expand Down Expand Up @@ -130,96 +129,6 @@ function getText(){
return document.getElementById("main").innerText;
}

/**
* Update document language
*/
function setLanguage(locale){
var oldLang = lang; //Used to check if title/content changed
//Set dialog phrases
switch(locale){
case "en_US":
lang = {
"title": "Encrypted note",
"note_title": "Untitled note",
"note_content": "Hello, World!",
"save_to_file": "Save to file",
"file": "File",
"language": "Language",
"load_file": "Load file",
"change_password": "Change password",
"generate_link": "Generate link",
"password_not_provided": "Password is not set. Please type a password and retry.",
"pwd_ask": "Please type your password.",
"pwd_change": "Please type your new password.",
"pwd_change_confirm": "Confirm your password",
"pwd_change_error": "Passwords do not match so password was not changed.",
"pwd_change_correct": "Done. Save your note to finish password change.",
"get_parameters_set": "Done. Remember to save page to bookmarks!",
"encrypted_note": "Encrypted Note",
"auto_encode_toggle": "Automatic link generation was set to ",
"saved": "Saved.",
"ok_modal": "OK",
"password_modal": "Password",
"file_loaded": "File loaded successfully.",
"save_storage": "Save to WebStorage",
"load_storage": "Load from WebStorage"
}
break;
case "es_ES":
lang = {
"title": "Nota encriptada",
"note_title": "Nota sin título",
"note_content": "¡Hola mundo!",
"save_to_file": "Guardar a archivo",
"file": "Archivo",
"language": "Idioma",
"load_file": "Cargar archivo",
"change_password": "Cambiar contraseña",
"generate_link": "Generar enlace",
"password_not_provided": "No has introducido contraseña. Inserta una e inténtalo de nuevo.",
"pwd_ask": "Por favor, introduce la contraseña.",
"pwd_change": "Escribe tu nueva contraseña",
"pwd_change_confirm": "Confirma tu contraseña",
"pwd_change_error": "Las contraseñas no coinciden por lo que tu contraseña no ha sido cambiada.",
"pwd_change_correct": "Hecho. Guarda la nota para guardar la nueva contraseña.",
"get_parameters_set": "Hecho. Recuerda guardar la página en marcadores o descargar el fichero.",
"encrypted_note": "Nota encriptada",
"auto_encode_toggle": "La generacion automatica de enlaces ha sido cambiada a ",
"saved": "Guardado.",
"ok_modal": "Aceptar",
"password_modal": "Contraseña",
"file_loaded": "Archivo cargado con éxito.",
"save_storage": "Guardar WebStorage",
"load_storage": "Leer WebStorage"
}
break;
default:
console.error("No language set");
}
//Set navbar phrases
document.title = lang.title;
document.getElementById("save").textContent = lang.save_to_file;
document.getElementById("load").textContent = lang.load_file;
document.getElementById("chpwd").textContent = lang.change_password;
document.getElementById("glinkbtn").textContent = lang.generate_link;
document.getElementById("savestorage").textContent = lang.save_storage;
document.getElementById("loadstorage").textContent = lang.load_storage;
document.getElementById("file-dropdown").textContent = lang.file;
document.getElementById("lang-dropdown").textContent = lang.language;
document.getElementById("modalSetPasswordButton").textContent = lang.ok_modal;
document.getElementById("modalChangePasswordButton").textContent = lang.ok_modal;
document.getElementById("modalSetPasswordTitle").textContent = lang.password_modal;
document.getElementById("modalSetPasswordInput").placeholder = lang.password_modal;
document.getElementById("modalSetPasswordDescription").textContent = lang.pwd_ask;
document.getElementById("modalChangePasswordTitle").textContent = lang.password_modal;
document.getElementById("modalChangePasswordInput1").placeholder = lang.password_modal;
document.getElementById("modalChangePasswordInput2").placeholder = lang.pwd_change_confirm;
if(!oldLang || oldLang.note_title == getTitle())
document.getElementById("title").innerText = lang.note_title;
if(!oldLang || oldLang.note_content == getText())
document.getElementById("main").innerText = lang.note_content;
}

/**
* Show info to user
*/
Expand Down

0 comments on commit e3fb367

Please sign in to comment.