Rework to directly edit strings with complexity

This commit is contained in:
Will Bradley 2023-08-27 18:18:47 -07:00
parent 907eb15383
commit bc98c41359

View File

@ -2,15 +2,56 @@
<head> <head>
<script src="https://code.jquery.com/jquery-3.6.1.min.js" integrity="sha256-o88AwQnZB+VDvE9tvIXrMQaPlFFSUTR+nldQm1LuPXQ=" crossorigin="anonymous"></script> <script src="https://code.jquery.com/jquery-3.6.1.min.js" integrity="sha256-o88AwQnZB+VDvE9tvIXrMQaPlFFSUTR+nldQm1LuPXQ=" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css" integrity="sha384-rbsA2VBKQhggwzxH7pPCaAqO46MgnOM80zW1RWuH61DGLwZJEdK2Kadq2F9CUG65" crossorigin="anonymous"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css" integrity="sha384-rbsA2VBKQhggwzxH7pPCaAqO46MgnOM80zW1RWuH61DGLwZJEdK2Kadq2F9CUG65" crossorigin="anonymous">
<style type="text/css">
input[type='text'][readonly] {
background: transparent;
color: white;
border-collapse: collapse;
border: none;
}
</style>
<script type="text/javascript"> <script type="text/javascript">
function loadLocale(){ function parseSoundTxt(raw) {
var commitHash = "b7138ae7f7f7f78da042edc8496d012ad4058334"; var lines = raw.split("\n");
var sourceUrl = "https://raw.githubusercontent.com/organicmaps/organicmaps/"+commitHash+"/data/sound-strings/"+$("#localeSelect").val()+".json/localize.json"; var thisSection = "";
var fancyUrl = "https://github.com/organicmaps/organicmaps/blob/"+commitHash+"/data/sound-strings/"+$("#localeSelect").val()+".json/localize.json" var out = {};
$("#sourceUrl").attr("href", fancyUrl).text($("#localeSelect").val()+".json"); for (var i=0;i<lines.length;i++) {
var line = lines[i];
$.getJSON(sourceUrl, function(data){ var sectionMatch = line.match(/\s+\[(\w+)\]/);
var stringMatch = line.match(/\s+([\w\-]+) = (.+)/);
if (sectionMatch && sectionMatch.length > 1) {
thisSection = sectionMatch[1];
} else if (stringMatch && stringMatch.length > 2) {
if (!out[stringMatch[1]]) {
out[stringMatch[1]] = {};
}
out[stringMatch[1]][thisSection] = stringMatch[2];
} else {
console.log("no match:", line);
}
}
return out;
}
function buildTranStringInput(key, value) {
return "<span class='tranString badge text-bg-primary'><input type='text' class='stringTxt' readonly data-key='"+
key +
"' value='" +
value +
"'>&nbsp;&nbsp;<button class='editbutton btn btn-xs btn-light' type='button' style='display:none;'>✏️</button><button class='savebutton btn btn-xs btn-success' style='display: none;' type='button'>Save</button></span>";
}
function showData(){
var locale = $("#localeSelect").val();
var data = parseSoundTxt(window.soundTxtRaw)[locale];
window.originalSoundData = data;
const distance = [ const distance = [
"in_50_meters", "in_50_meters",
@ -77,7 +118,7 @@
"make_a_u_turn", "make_a_u_turn",
"go_straight", "go_straight",
"exit", "exit",
"destination" "destination" // exception: shouldn't have a street afterwards
]; ];
const nextStreets = [ const nextStreets = [
@ -99,27 +140,44 @@
$("#out-f").html(""); $("#out-f").html("");
$("#out-g").html(""); $("#out-g").html("");
var inputPre = "<div class='input-group mb-3'><button type='button' class='playtts btn btn-outline-secondary'>🔊</button><button class='editbutton btn btn-secondary' type='button'>Edit</button><button class='savebutton btn btn-success' style='display: none;' type='button'>Save</button><input class='form-control' disabled='disabled' type='text' value='"; var inputPre = "<div class='input-group mb-3'><button type='button' class='playtts btn btn-outline-secondary'>🔊</button><div class='form-control'>";
var inputPost = "' /></div>"; var inputPost = "</div></div>";
var tranStringTtsPre = "<span class='tranString badge text-bg-secondary'>";
$.each(direction, function(i, dir){ $.each(direction, function(i, dir){
// can't "you'll arrive" in the present tense // can't "you'll arrive" in the present tense
if (dir != "destination") { if (dir != "destination") {
$("#out-a").append(inputPre + data[dir] + inputPost); $("#out-a").append(inputPre +
buildTranStringInput(dir, data[dir]) +
inputPost);
} }
}); });
/*
$.each(data, function(i, d){
$("#out-a").append(i+":"+d+"<br/>");
});
$.each(direction, function(i, dir){ $.each(direction, function(i, dir){
// can't "then" past your destination // can't "then" past your destination
if (dir != "destination") { if (dir != "destination") {
var nextDir = direction[(i + 1) % direction.length]; var nextDir = direction[(i + 1) % direction.length];
$("#out-b").append(inputPre + data[dir] + " " + data['then'] + " " + data[nextDir] + inputPost); $("#out-b").append(inputPre +
tranStringPre + data[dir] + tranStringPost +
tranStringPre + data['then'] + tranStringPost +
tranStringPre + data[nextDir] + tranStringPost +
inputPost);
} }
}); });
$.each(distance, function(i, dist){ $.each(distance, function(i, dist){
var dir = direction[i % direction.length]; var dir = direction[i % direction.length];
$("#out-c").append(inputPre + data[dist] + " " + data[dir] + inputPost); $("#out-c").append(inputPre +
tranStringPre + data[dist] + tranStringPost +
tranStringPre + data[dir] + tranStringPost +
inputPost);
}); });
$.each(distance, function(i, dist){ $.each(distance, function(i, dist){
@ -127,7 +185,12 @@
// can't "then" past your destination // can't "then" past your destination
if (dir != "destination") { if (dir != "destination") {
var nextDir = direction[(i + 1) % direction.length]; var nextDir = direction[(i + 1) % direction.length];
$("#out-d").append(inputPre + data[dist] + " " + data[dir] + " " + data['then'] + " " + data[nextDir] + inputPost); $("#out-d").append(inputPre +
tranStringPre + data[dist] + tranStringPost +
tranStringPre + data[dir] + tranStringPost +
tranStringPre + data['then'] + tranStringPost +
tranStringPre + data[nextDir] + tranStringPost +
inputPost);
} }
}); });
@ -140,11 +203,13 @@
streetDir = data[dir+"_street"]; // overwrite the direction string if present for street TTS streetDir = data[dir+"_street"]; // overwrite the direction string if present for street TTS
} }
ttsFmt = ttsFmt.replace("%1$s", data[dist].replace(/[\.。।]/g, "")); // no full stops ttsFmt = ttsFmt.replace("%1$s", tranStringPre + data[dist].replace(/[\.。।]/g, "") + tranStringPost); // no full stops
ttsFmt = ttsFmt.replace("%2$s", streetDir.replace(/[\.。।]/g, "")); // no full stops ttsFmt = ttsFmt.replace("%2$s", tranStringPre + streetDir.replace(/[\.。।]/g, "") + tranStringPost); // no full stops
ttsFmt = ttsFmt.replace("%3$s", nextStreets[(i+1) % nextStreets.length]); ttsFmt = ttsFmt.replace("%3$s", tranStringPre + nextStreets[(i+1) % nextStreets.length] + tranStringPost);
$("#out-e").append(inputPre + ttsFmt + inputPost); $("#out-e").append(inputPre +
tranStringTtsPre + ttsFmt + tranStringPost +
inputPost);
}); });
$.each(distance, function(i, dist){ $.each(distance, function(i, dist){
@ -156,35 +221,59 @@
streetDir = data[dir+"_street"]; // overwrite the direction string if present for street TTS streetDir = data[dir+"_street"]; // overwrite the direction string if present for street TTS
} }
ttsFmt = ttsFmt.replace("%1$s", data[dist].replace(/[\.。।]/g, "")); // no full stops ttsFmt = ttsFmt.replace("%1$s", tranStringPre + data[dist].replace(/[\.。।]/g, "") + tranStringPost); // no full stops
ttsFmt = ttsFmt.replace("%2$s", streetDir.replace(/[\.。।]/g, "")); // no full stops ttsFmt = ttsFmt.replace("%2$s", tranStringPre + streetDir.replace(/[\.。।]/g, "") + tranStringPost); // no full stops
ttsFmt = ttsFmt.replace("%3$s", nextStreets[(i+1) % nextStreets.length]); ttsFmt = ttsFmt.replace("%3$s", tranStringPre + nextStreets[(i+1) % nextStreets.length] + tranStringPost);
$("#out-f").append(inputPre + ttsFmt + " " + data['then'] + " " + data[nextDir] + inputPost); $("#out-f").append(inputPre +
tranStringTtsPre + ttsFmt + tranStringPost +
tranStringPre + data['then'] + tranStringPost +
tranStringPre + data[nextDir] + tranStringPost +
inputPost);
}); });
$("#out-g").append(inputPre + data['you_have_reached_the_destination'] + inputPost); $("#out-g").append(inputPre +
$("#out-g").append(inputPre + data['unknown_camera'] + inputPost); tranStringPre + data['you_have_reached_the_destination'] + tranStringPost +
inputPost);
$("#out-g").append(inputPre +
tranStringPre + data['unknown_camera'] + tranStringPost +
inputPost);
*/
$(".editbutton").click(function(){ $(".tranString").hover(function(){
var sibling = $($(this).siblings(".form-control")[0]); let editBtn = $(this).children(".editbutton");
// console.log(sibling); console.log(editBtn);
// console.log(sibling.val()); if (editBtn.prop("disabled") == false) {
sibling.data("orig-value", sibling.val()); editBtn.show(); // only show when not disabled
// console.log(sibling.data("orig-value")); }
sibling.prop( "disabled", false ); }, function(){
sibling.focus(); $(this).children(".editbutton").hide();
$(this).hide(); });
$(".editbutton")
.click(function(){
$(this).siblings(".stringTxt").prop("readonly",false);
$(this).prop("disabled",true).hide();
$(this).siblings(".savebutton").show(); $(this).siblings(".savebutton").show();
}); });
$(".savebutton").click(function(){ $(".savebutton").click(function(){
var changes = []; var changes = [];
var thisSaveButton = $(this);
var thisSibling = $($(this).siblings(".form-control")[0]);
thisSibling.prop( "disabled", true );
thisSaveButton.hide();
thisSaveButton.siblings(".editbutton").show();
$(this).siblings(".stringTxt").prop("readonly",true);
$(this).hide();
$(this).siblings(".editbutton").prop("disabled",false).show();
var stringTxt = $(this).siblings(".stringTxt")[0];
window.originalSoundData[$(stringTxt).data("key")] = $(stringTxt).val();
$("#submitpopup textarea").text("Translation change request:\n```\n"+
"# data/strings/sound.txt\n"+
JSON.stringify(window.originalSoundData)+"```"
);
$("#submitpopup").show();
/*
$("div.output .form-control").each(function(i,o){ $("div.output .form-control").each(function(i,o){
var sibling = $(o); var sibling = $(o);
// console.log("orig",sibling.data("orig-value")); // console.log("orig",sibling.data("orig-value"));
@ -206,12 +295,8 @@
}); });
// console.log("writing", out); // console.log("writing", out);
$("#submitpopup textarea").text("Translation change request:\n```\n"+
"# data/strings/sound.txt\n"+
out+"```"
);
$("#submitpopup").show();
} }
*/
}); });
// $("#closesubmitpopup").click(function(){ // $("#closesubmitpopup").click(function(){
@ -234,7 +319,22 @@
if (window.hideTts) { if (window.hideTts) {
$(".playtts").hide(); $(".playtts").hide();
} }
}
function loadLocale(){
var commitHash = "b7138ae7f7f7f78da042edc8496d012ad4058334";
var sourceUrl = "https://raw.githubusercontent.com/organicmaps/organicmaps/"+commitHash+"/data/strings/sound.txt";
$("#sourceUrl").attr("href", sourceUrl).text("sound.txt");
if (!window.soundTxtRaw) {
$.get(sourceUrl, function(rawData){
window.soundTxtRaw = rawData;
showData();
}); });
} else {
showData();
}
} }
</script> </script>
<style type="text/css"> <style type="text/css">
@ -244,6 +344,11 @@
padding-bottom: 1em; padding-bottom: 1em;
border-bottom: 1px dashed; border-bottom: 1px dashed;
} }
.btn.btn-xs {
--bs-btn-padding-y: .25rem;
--bs-btn-padding-x: .5rem;
--bs-btn-font-size: .5rem;
}
</style> </style>
</head> </head>
<body style="padding: 1em;"> <body style="padding: 1em;">
@ -326,7 +431,7 @@
const synth = window.speechSynthesis; const synth = window.speechSynthesis;
var voices; var voices;
$(document).ready(function(){ function loadTts(){
voices = synth.getVoices(); voices = synth.getVoices();
$.each(voices, function(i, voice){ $.each(voices, function(i, voice){
$("#voiceSelect").append('<option value="'+i+'" lang="'+voice.lang+'">'+voice.name+'</option>'); $("#voiceSelect").append('<option value="'+i+'" lang="'+voice.lang+'">'+voice.name+'</option>');
@ -377,17 +482,21 @@
} }
$("#localeSelect").change(function(){ $("#localeSelect").change(function(){
params.set('lang', $("#localeSelect").val()); showData();
document.location.search = params.toString(); // params.set('lang', $("#localeSelect").val());
// document.location.search = params.toString();
}); });
$("#voiceSelect").change(function(){ $("#voiceSelect").change(function(){
params.set('tts', $("#voiceSelect").val()); loadTts();
document.location.search = params.toString(); // params.set('tts', $("#voiceSelect").val());
// document.location.search = params.toString();
}); });
loadLocale(); loadLocale();
}); }
$(document).ready(function(){ loadTts(); });
</script> </script>
</body> </body>