Skip to content

Commit

Permalink
meilleur traduction des erreurs et de l'aide
Browse files Browse the repository at this point in the history
  • Loading branch information
kpym committed Oct 24, 2020
1 parent 7cd9f59 commit da884fa
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 34 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.15

require (
github.com/markbates/pkger v0.17.1
github.com/spf13/pflag v1.0.5
github.com/spf13/pflag v1.0.6-0.20201009195203-85dd5c8bc61c
github.com/tdewolff/canvas v0.0.0-20201021153214-d9228b138ea8
golang.org/x/text v0.3.3 // indirect
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ github.com/remyoudompheng/bigfft v0.0.0-20190728182440-6a916e37a237/go.mod h1:qq
github.com/ruudk/golang-pdf417 v0.0.0-20181029194003-1af4ab5afa58/go.mod h1:6lfFZQK844Gfx8o5WFuvpxWRwnSoipWe/p622j1v06w=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/spf13/pflag v1.0.6-0.20201009195203-85dd5c8bc61c h1:zqmyTlQyufRC65JnImJ6H1Sf7BDj8bG31EV919NVEQc=
github.com/spf13/pflag v1.0.6-0.20201009195203-85dd5c8bc61c/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
Expand Down
48 changes: 15 additions & 33 deletions marianne.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,42 +289,16 @@ func writeImages(c *canvas.Canvas, zp, formats string) {

}

var traductions = map[string]string{
"default": "par défaut",
"strings": " ",
"string": " ",
"ints": " ",
// "bad flag syntax:": "mauvaise syntaxe du paramètre :",
// "unknown flag:": "paramètre inconnu :",
// "unknown shorthand flag:": "paramètre court inconnu :",
// " in ": " dans ",
// "flag needs an argument:": "Le paramètre nécessite d'un argument :",
}

// Affiche l'aide d'utilisation
// C'est un peut plus compliqué que ce que ça devrait être
// car on doit remplacer "default" avec "par défaut"
// voir : https://github.com/golang/go/issues/42124
func Aide() {

fmt.Fprintf(os.Stderr, "marianne (version: %s)\n\n", version)
fmt.Fprintf(os.Stderr, "Ce programme génère le logo de l'institution.\nParamètres disponibles:\n\n")

// Remplace "default" avec "par défaut" dans `flag.PrintDefaults`
var buf = new(bytes.Buffer)
// on redirige la sortie vers buf et on affiche les flags dedans
flag.CommandLine.SetOutput(buf)
defer flag.CommandLine.SetOutput(os.Stderr)
var out = flag.CommandLine.Output()
fmt.Fprintf(out, "marianne (version: %s)\n\n", version)
fmt.Fprintf(out, "Ce programme génère le logo de l'institution.\nParamètres disponibles:\n\n")
flag.PrintDefaults()
// le message avant les ajustements
msg := string(buf.Bytes())
// traduction en français
for from, to := range traductions {
msg = strings.ReplaceAll(msg, from, to)
}
// on affiche finalement le message d'aide
fmt.Fprintf(os.Stderr, msg)
fmt.Fprintf(os.Stderr, "\n")
fmt.Fprintf(out, "\n")
}

func main() {
Expand All @@ -342,13 +316,21 @@ func main() {
flag.BoolVarP(&aide, "aide", "h", false, "Imprime ce message d'aide.")
// Message d'aide
flag.CommandLine.SortFlags = false
flag.CommandLine.SetOutput(FrenchTranslator{flag.CommandLine.Output()})
flag.Usage = Aide
flag.CommandLine.Init("marianne", flag.ContinueOnError)

// récupère les flags
flag.Parse()
err = flag.CommandLine.Parse(os.Args[1:])
// affiche l'aide si demandé
if aide {
if aide || err != nil {
flag.Usage()
os.Exit(0)
if err != nil {
fmt.Fprintln(flag.CommandLine.Output(), "ERREUR : ", err)
os.Exit(2)
} else {
os.Exit(0)
}
}
// au moins une des versions doit être présente (avec marges par défaut)
if !sansMarges {
Expand Down
38 changes: 38 additions & 0 deletions translate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package main

import (
"fmt"
"io"
"strings"
)

type trad struct {
from, to string
}

var traductions = []trad{
{"default", "par défaut"},
{"strings", " "},
{"string", " "},
{"ints", " "},
{"bad flag syntax:", "mauvaise syntaxe du paramètre :"},
{"unknown flag:", "paramètre inconnu :"},
{"unknown shorthand flag:", "paramètre court inconnu :"},
{"flag needs an argument:", "Le paramètre nécessite d'un argument :"},
{" in ", " dans "},
}

type FrenchTranslator struct {
w io.Writer
}

func (fw FrenchTranslator) Write(p []byte) (n int, err error) {
out := string(p)

for _, tr := range traductions {
out = strings.ReplaceAll(out, tr.from, tr.to)
}

fmt.Fprintf(fw.w, out)
return len(p), nil
}

0 comments on commit da884fa

Please sign in to comment.