Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Rehtt committed Apr 3, 2023
1 parent bab18ba commit 4f17a29
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 29 deletions.
4 changes: 4 additions & 0 deletions i18n/geni18n/go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
module github.com/Rehtt/Kit/i18n/geni18n

go 1.17

require github.com/Rehtt/Kit v0.1.2-0.20230403010741-bab18bafcd90

require golang.org/x/text v0.6.0 // indirect
31 changes: 2 additions & 29 deletions i18n/geni18n/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"bytes"
strings2 "github.com/Rehtt/Kit/strings"
"go/ast"
"go/parser"
"go/token"
Expand Down Expand Up @@ -60,7 +61,7 @@ func parseFile(filePath string, m map[string]string) error {
if strings.HasSuffix(s[i+4], "Name: \"GetText\"") && strings.Contains(s[i+12], "Value: \"\\\"") {
values := strings.Split(s[i+12], "\\\"")
v := strings.Join(values[1:len(values)-1], "\\\"")
v = EscapeString(EscapeString(v, true), true)
v = strings2.EscapeStringRepeat(v, 2, true)
m[v] = v
}
}
Expand All @@ -69,31 +70,3 @@ func parseFile(filePath string, m map[string]string) error {
}
return nil
}

func EscapeString(str string, reverse ...bool) string {
replacements := map[string]string{
`\`: `\\`,
`'`: `\'`,
`"`: `\"`,
`%`: `\%`,
`_`: `\_`,
"\n": "\\n",
"\r": "\\r",
"\t": "\\t",
"\a": "\\a",
"\f": "\\f",
"\v": "\\v",
"\b": "\\b",
}
rep := make([]string, 0, len(replacements)*2)
for ori, es := range replacements {
if len(reverse) != 0 && reverse[0] {
rep = append(rep, es, ori)
} else {
rep = append(rep, ori, es)
}

}
str = strings.NewReplacer(rep...).Replace(str)
return str
}

0 comments on commit 4f17a29

Please sign in to comment.