Skip to content
This repository has been archived by the owner on Jul 11, 2020. It is now read-only.

Commit

Permalink
add Duplicate string to main strings page
Browse files Browse the repository at this point in the history
  • Loading branch information
kjk committed Jun 11, 2014
1 parent e710b9a commit 4599d7f
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 65 deletions.
41 changes: 12 additions & 29 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,8 @@ type AppConfig struct {
DataDir string
// we authenticate only with Twitter, this is the twitter user name
// of the admin user
AdminTwitterUser string
AdminTwitterUser string
AdminTwitterUser2 string
// an arbitrary string, used to protect the API for uploading new strings
// for the app
UploadSecret string
Expand Down Expand Up @@ -204,37 +205,16 @@ func (a *App) storeCsvFilePath() string {

func readAppData(app *App) error {
var path string
if !*inProduction {
path = app.storeCsvFilePath()
if u.PathExists(path) {
if l, err := store.NewStoreCsv(path); err == nil {
app.store = l
return nil
}
path = app.storeCsvFilePath()
if u.PathExists(path) {
if l, err := store.NewStoreCsv(path); err == nil {
app.store = l
return nil
}
}
path = app.storeBinaryFilePath()
l, err := store.NewStoreBinary(path)
if err != nil {
return err
}
app.store = l
return nil
return fmt.Errorf("readAppData: %q data file doesn't exist", path)
}

/*
type StringsSeq []string
func (s StringsSeq) Len() int { return len(s) }
func (s StringsSeq) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
type SmartString struct{ StringsSeq }
func (s SmartString) Less(i, j int) bool {
return transStringLess(s.StringsSeq[i], s.StringsSeq[j])
}
*/

func findApp(name string) *App {
for _, app := range appState.Apps {
if app.Name == name {
Expand Down Expand Up @@ -317,7 +297,10 @@ func serveErrorMsg(w http.ResponseWriter, msg string) {
}

func userIsAdmin(app *App, user string) bool {
return user == app.AdminTwitterUser
if user == "" {
return false
}
return user == app.AdminTwitterUser || user == app.AdminTwitterUser2
}

// reads the configuration file from the path specified by
Expand Down
2 changes: 2 additions & 0 deletions tmpl/app.html
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ <h2><a href="/">Home</a> : Translations for {{.App.Name}}
{{range .RecentEdits}}
<li><a href="/user/{{.User}}">{{.User}}</a> translated '{{.TextDisplay}}' in <a href="/app/{{$appName}}/{{.Lang}}">{{.Lang}}</a></li>
{{end}}
<!--
<li><a href="/app/{{$appName}}/edits">see all...</a></li>
-->
</ul>
</div>
{{end}}
Expand Down
52 changes: 26 additions & 26 deletions tmpl/appstrings.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ <h2><a href="/">Home</a> : <a href="/app/{{.App.Name}}">{{.App.Name}}</a> : stri
<span style="font-size:50%;float:right;">{{if .User}}Logged in as {{.User}} (<a href="/logout?redirect={{.RedirectUrl}}">logout</a>){{else}}Not logged in. <a href="/login?redirect={{.RedirectUrl}}">Log in with Twitter</a>{{end}}</span>
</h2>
<div class="lead">
{{.StringsCount}} active strings, {{len .DeletedStrings }} obsolete strings
{{.StringsCount}} active strings, {{len .DeletedStrings }} unused strings
{{if not .User}}(log in to duplicate translations){{end}}</div>
</header>

Expand All @@ -52,7 +52,7 @@ <h2><a href="/">Home</a> : <a href="/app/{{.App.Name}}">{{.App.Name}}</a> : stri

{{if len .DeletedStrings}}
<p></p>
<b>{{len .DeletedStrings}} obsolete strings:<br></b>
<b>{{len .DeletedStrings}} unused strings:<br></b>
{{range $idx, $el := .DeletedStrings}}
<div class="trans" id="idObsolete{{$idx}}">
<span class="origstr">{{$el}}</span>
Expand All @@ -61,20 +61,20 @@ <h2><a href="/">Home</a> : <a href="/app/{{.App.Name}}">{{.App.Name}}</a> : stri
{{end}}

{{if .User}}
<div class="modal hide" id="idEditTrans">
<div class="modal hide" id="idDupTrans">
<div class="modal-header">
<a class="close" data-dismiss="modal">×</a>
<h3><span id="idEditTransHdr"></span></h3>
<h3><span id="idDupTransHdr"></span></h3>
</div>
<div>
<form class="well" action="/duptranslation" method="POST">
<div class="modal-body">
<label>Duplicate translation of string:</label>
<textarea rows=3 readonly="readonly" name="string" id="idEditFormString" style="width:90%"></textarea>
<textarea rows=3 readonly="readonly" name="string" id="idDupFormString" style="width:90%"></textarea>
<label>As:</label>
<textarea rows=3 name="duplicate" id="idEditFormTrans" style="width:90%"></textarea>
<textarea rows=3 name="duplicate" id="idDupFormTrans" style="width:90%"></textarea>
<input type="hidden" name="app" value="{{.App.Name}}">
<p id="mismatchedStringFormattingError" style="color:red;visibility:hidden"><bold>
<p id="mismatchedDupStringFormattingError" style="color:red;visibility:hidden"><bold>
Error: translation cannot be empty and string formatting directives (%s, %d etc.)
must be in the same order.
</bold></p>
Expand All @@ -90,10 +90,10 @@ <h3><span id="idEditTransHdr"></span></h3>
</div>
</div>
{{else}}
<div class="modal hide" id="idEditTrans">
<div class="modal hide" id="idDupTrans">
<div class="modal-header">
<a class="close" data-dismiss="modal">×</a>
<h3><span id="idEditTransHdr"></span></h3>
<h3><span id="idDupTransHdr"></span></h3>
</div>
<div>
<form class="well" action="/nowhere" method="POST">
Expand Down Expand Up @@ -130,35 +130,35 @@ <h3><span id="idEditTransHdr"></span></h3>
return stringFormattingModifiersAreSame(text, translation);
}

var prevTranslationValue = "";
function updateEditTransState() {
var txt = $("#idEditFormString").text();
prevTranslationValue = $("#idEditFormTrans").val();
if (canSubmitDuplication(txt, prevTranslationValue)) {
var prevDupValue = "";
function updateDupTransState() {
var txt = $("#idDupFormString").text();
prevDupValue = $("#idDupFormTrans").val();
if (canSubmitDuplication(txt, prevDupValue)) {
$("button[type=submit]").removeAttr("disabled");
$("#mismatchedStringFormattingError").css({"visibility":"hidden"});
$("#mismatchedDupStringFormattingError").css({"visibility":"hidden"});
} else {
$("button[type=submit]").attr("disabled", "disabled");
$("#mismatchedStringFormattingError").css({"visibility":"visible"});
$("#mismatchedDupStringFormattingError").css({"visibility":"visible"});
}
}

$(document).ready(function() {

$(".dupbtn").click(function() {
$("#idEditTransHdr").text("Duplicate translation");
$("#idDupTransHdr").text("Duplicate translation");
var el = $(this).parent().find(".origstr");
$("#idEditFormString").text(el.text());
$("#idEditFormTrans").text(el.text());
$("#idEditTrans").modal('show');
$("#idEditFormTrans").focus();
updateEditTransState();
$("#idDupFormString").text(el.text());
$("#idDupFormTrans").text(el.text());
$("#idDupTrans").modal('show');
$("#idDupFormTrans").focus();
updateDupTransState();
});

$("#idEditFormTrans").bind("keyup paste", function(e) {
if ($(this).val() == prevTranslationValue) { return; }
updateEditTransState();
});
$("#idDupFormTrans").bind("keyup paste", function(e) {
if ($(this).val() == prevDupValue) { return; }
updateDupTransState();
});

});

Expand Down
93 changes: 84 additions & 9 deletions tmpl/apptrans.html
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ <h2><a href="/">Home</a> : <a href="/app/{{.App.Name}}">{{.App.Name}}</a> : {{.L

<p style="margin-bottom:24px"></p>

{{$canDuplicate := .UserIsAdmin}}

{{if .ShowTranslationEditedMsg}}
<div class="alert alert-success">
<button class="close" data-dismiss="alert">×</button>
Expand All @@ -54,22 +56,33 @@ <h2><a href="/">Home</a> : <a href="/app/{{.App.Name}}">{{.App.Name}}</a> : {{.L
{{if .Current}}
<span style="color:blue">=&gt;</span>
<span class="transstr">{{.Current}}</span> <a href="#" class="editbtn" id="idEdit{{$idx}}">Edit</a>

{{if $canDuplicate}}
&bull;&nbsp;<a href="#" class="dupbtn" id="idDup{{$idx}}">Duplicate translation...</a>
{{end}}

{{range .History}}
<br><span style="color: #888;padding-left:28px">previous: {{.}}</span>
{{end}}
{{else}}
<a href="#" class="addbtn" id="idEdit{{$idx}}">Add a translation...</a>

{{if $canDuplicate}}
&bull;&nbsp;<a href="#" class="dupbtn" id="idDup{{$idx}}">Duplicate translation...</a>
{{end}}

{{end}}
</div>
{{end}}
{{end}}

{{if .User}}
<div class="modal hide" id="idEditTrans">
<div class="modal-header">
<a class="close" data-dismiss="modal">×</a>
<h3><span id="idEditTransHdr"></span></h3>
</div>

{{if .User}}
<div>
<form class="well" action="/edittranslation" method="POST">
<div class="modal-body">
Expand All @@ -93,13 +106,7 @@ <h3><span id="idEditTransHdr"></span></h3>
</div>
</form>
</div>
</div>
{{else}}
<div class="modal hide" id="idEditTrans">
<div class="modal-header">
<a class="close" data-dismiss="modal">×</a>
<h3><span id="idEditTransHdr"></span></h3>
</div>
{{else}}
<div>
<form class="well" action="/nowhere" method="POST">
<div class="modal-body">
Expand All @@ -114,6 +121,37 @@ <h3><span id="idEditTransHdr"></span></h3>
</div>
</form>
</div>
{{end}}
</div>

{{if $canDuplicate}}
<div class="modal hide" id="idDupTrans">
<div class="modal-header">
<a class="close" data-dismiss="modal">×</a>
<h3><span id="idDupTransHdr"></span></h3>
</div>
<div>
<form class="well" action="/duptranslation" method="POST">
<div class="modal-body">
<label>Duplicate translation of string:</label>
<textarea rows=3 readonly="readonly" name="string" id="idDupFormString" style="width:90%"></textarea>
<label>As:</label>
<textarea rows=3 name="duplicate" id="idDupFormTrans" style="width:90%"></textarea>
<input type="hidden" name="app" value="{{.App.Name}}">
<p id="mismatchedDupStringFormattingError" style="color:red;visibility:hidden"><bold>
Error: translation cannot be empty and string formatting directives (%s, %d etc.)
must be in the same order.
</bold></p>
<p></p>
<p>Note: by adding/editing a translation you agree to place your translations
into <a href="http://en.wikipedia.org/wiki/Public_domain">Public Domain</a>.</p>
</div>
<div class="modal-footer">
<button type="submit" class="btn btn-primary">Submit</button>
<a href="#" class="btn" data-dismiss="modal">Cancel</a>
</div>
</form>
</div>
</div>
{{end}}

Expand All @@ -140,6 +178,27 @@ <h3><span id="idEditTransHdr"></span></h3>
}
}

// 1. can't be the same
// 2. their string formatting instructions (%d, %s etc.) match
function canSubmitDuplication(text, translation) {
var t = $.trim(translation);
if (t.length == 0) { return false; }
return stringFormattingModifiersAreSame(text, translation);
}

var prevDupValue = "";
function updateDupTransState() {
var txt = $("#idDupFormString").text();
prevDupValue = $("#idDupFormTrans").val();
if (canSubmitDuplication(txt, prevDupValue)) {
$("button[type=submit]").removeAttr("disabled");
$("#mismatchedDupStringFormattingError").css({"visibility":"hidden"});
} else {
$("button[type=submit]").attr("disabled", "disabled");
$("#mismatchedDupStringFormattingError").css({"visibility":"visible"});
}
}

$(document).ready(function() {

$(".addbtn").click(function() {
Expand All @@ -162,11 +221,27 @@ <h3><span id="idEditTransHdr"></span></h3>
$("#idEditFormTrans").focus();
updateEditTransState();
});

$("#idEditFormTrans").bind("keyup paste", function(e) {
if ($(this).val() == prevTranslationValue) { return; }
updateEditTransState();
});
});


$(".dupbtn").click(function() {
$("#idDupTransHdr").text("Duplicate translation");
var el = $(this).parent().find(".origstr");
$("#idDupFormString").text(el.text());
$("#idDupFormTrans").text(el.text());
$("#idDupTrans").modal('show');
$("#idDupFormTrans").focus();
updateDupTransState();
});

$("#idDupFormTrans").bind("keyup paste", function(e) {
if ($(this).val() == prevDupValue) { return; }
updateDupTransState();
});
});

</script>
Expand Down
3 changes: 2 additions & 1 deletion tools/listbackup/listbackup.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ type AppConfig struct {
DataDir string
// we authenticate only with Twitter, this is the twitter user name
// of the admin user
AdminTwitterUser string
AdminTwitterUser string
AdminTwitterUser2 string
// an arbitrary string, used to protect the API for uploading new strings
// for the app
UploadSecret string
Expand Down

0 comments on commit 4599d7f

Please sign in to comment.