Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to localize all strings output to console #4464

Merged
merged 32 commits into from
Jun 21, 2019
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
0b79a57
Initial attempt at localization with gotext
sharifelgamal May 13, 2019
e5ac5d8
Merge branch 'master' of github.com:kubernetes/minikube into localiza…
sharifelgamal May 13, 2019
fd75433
fixing go mods
sharifelgamal May 14, 2019
03ebb0e
Merge branch 'master' of github.com:kubernetes/minikube into localiza…
sharifelgamal May 16, 2019
e174f3b
Add semi-functional extractor
sharifelgamal May 17, 2019
fbc11c3
Recursive search for extractor
sharifelgamal May 29, 2019
483f9fb
Merge branch 'master' of github.com:kubernetes/minikube into localiza…
sharifelgamal May 30, 2019
610e4f1
More extractor work
sharifelgamal Jun 6, 2019
3447499
Robust extraction yaaaaaaaay
sharifelgamal Jun 10, 2019
f452f0c
Merge branch 'master' of github.com:kubernetes/minikube into localiza…
sharifelgamal Jun 10, 2019
4630ccc
linting
sharifelgamal Jun 10, 2019
0c43731
Small fixes to make things work
sharifelgamal Jun 10, 2019
aabce58
Refactor extract to not use global variables
sharifelgamal Jun 11, 2019
aa5c2ec
adding back extract
sharifelgamal Jun 11, 2019
3ce070c
extract is now a command
sharifelgamal Jun 13, 2019
58dd381
Merge branch 'master' of github.com:kubernetes/minikube into localiza…
sharifelgamal Jun 13, 2019
b573cb8
adding extract cmd file
sharifelgamal Jun 13, 2019
41f258c
Merge branch 'localization-poc' of github.com:sharifelgamal/minikube …
sharifelgamal Jun 13, 2019
dae9425
small changes
sharifelgamal Jun 14, 2019
5ef94b4
Adding comments for clarity
sharifelgamal Jun 14, 2019
b499226
shorten name of translate function
sharifelgamal Jun 17, 2019
6cd86b7
don't make extract an end-user command
sharifelgamal Jun 17, 2019
8da5f2b
General cleanup for clarity
sharifelgamal Jun 17, 2019
8308aa5
adding test
sharifelgamal Jun 18, 2019
97e2cd1
Simplifying and correcting the extract code. Test passes now.
sharifelgamal Jun 20, 2019
062a453
test actually passes now
sharifelgamal Jun 20, 2019
d1eea53
add demo translation file for zh-CN
sharifelgamal Jun 20, 2019
e8329f6
Merge branch 'master' of github.com:kubernetes/minikube into localiza…
sharifelgamal Jun 20, 2019
7fc7b24
fix linting errors
sharifelgamal Jun 20, 2019
3be580c
add comment for top level command and add chinese transkations
sharifelgamal Jun 20, 2019
7a8fb7d
add more entries to the blacklist and clean up translation files
sharifelgamal Jun 20, 2019
8f5026d
small fix
sharifelgamal Jun 20, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/minikube/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ var RootCmd = &cobra.Command{
}

if enableUpdateNotification {
notify.MaybePrintUpdateTextFromGithub(os.Stderr)
notify.MaybePrintUpdateTextFromGithub()
}
},
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/minikube/cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ func runStart(cmd *cobra.Command, args []string) {
// Makes minikube node ip to bypass http(s) proxy. since it is local traffic.
err = proxy.ExcludeIP(ip)
if err != nil {
console.ErrStyle(console.FailureType, "Failed to set NO_PROXY Env. please Use `export NO_PROXY=$NO_PROXY,%s`.", ip)
console.ErrStyle(console.FailureType, "Failed to set NO_PROXY Env. Please use `export NO_PROXY=$NO_PROXY,%s`.", ip)
}
// Save IP to configuration file for subsequent use
config.KubernetesConfig.NodeIP = ip
Expand Down
3 changes: 2 additions & 1 deletion cmd/minikube/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"k8s.io/minikube/pkg/minikube/console"
"k8s.io/minikube/pkg/minikube/constants"
"k8s.io/minikube/pkg/minikube/machine"
"k8s.io/minikube/pkg/minikube/translate"
_ "k8s.io/minikube/pkg/provision"
)

Expand All @@ -40,6 +41,6 @@ func main() {
}
console.SetOutFile(os.Stdout)
console.SetErrFile(os.Stderr)
console.DetermineLocale()
translate.DetermineLocale()
cmd.Execute()
}
8 changes: 5 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ require (
github.com/fatih/color v1.7.0 // indirect
github.com/ghodss/yaml v0.0.0-20150909031657-73d445a93680 // indirect
github.com/gogo/protobuf v0.0.0-20170330071051-c0656edd0d9e // indirect
github.com/golang-collections/collections v0.0.0-20130729185459-604e922904d3
github.com/golang/glog v0.0.0-20141105023935-44145f04b68c
github.com/golang/groupcache v0.0.0-20160516000752-02826c3e7903 // indirect
github.com/google/btree v1.0.0 // indirect
Expand Down Expand Up @@ -55,6 +56,7 @@ require (
github.com/moby/hyperkit v0.0.0-20171020124204-a12cd7250bcd
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 // indirect
github.com/nicksnyder/go-i18n/v2 v2.0.0-beta.7
github.com/olekukonko/tablewriter v0.0.0-20160923125401-bdcc175572fd
github.com/onsi/ginkgo v1.8.0 // indirect
github.com/onsi/gomega v1.5.0 // indirect
Expand Down Expand Up @@ -84,10 +86,10 @@ require (
github.com/xeipuuv/gojsonreference v0.0.0-20150808065054-e02fc20de94c // indirect
github.com/xeipuuv/gojsonschema v0.0.0-20160623135812-c539bca196be
github.com/zchee/go-vmnet v0.0.0-20161021174912-97ebf9174097
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2
golang.org/x/crypto v0.0.0-20190506204251-e1dfcc566284
golang.org/x/oauth2 v0.0.0-20190115181402-5dab4167f31c
golang.org/x/sync v0.0.0-20190227155943-e225da77a7e6
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223
golang.org/x/sync v0.0.0-20190423024810-112230192c58
golang.org/x/sys v0.0.0-20190507160741-ecd444e8653b
golang.org/x/text v0.3.2
golang.org/x/time v0.0.0-20161028155119-f51c12702a4d // indirect
gopkg.in/airbrake/gobrake.v2 v2.0.9 // indirect
Expand Down
19 changes: 18 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
github.com/Azure/go-ansiterm v0.0.0-20170929234023-d6e3b3328b78 h1:w+iIsaOQNcT7OZ575w+acHgRric5iCyQh+xv+KJ4HB8=
github.com/Azure/go-ansiterm v0.0.0-20170929234023-d6e3b3328b78/go.mod h1:LmzpDX56iTiv29bbRTIsUNlaFfuhWRQBWjQdVyAevI8=
github.com/BurntSushi/toml v0.3.0 h1:e1/Ivsx3Z0FVTV0NSOv/aVgbUWyQuzj7DDnFblkRvsY=
github.com/BurntSushi/toml v0.3.0/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/Parallels/docker-machine-parallels v1.3.0 h1:RG1fyf3v1GwXMCeHRiZkB4tL9phFZEv6ixcvRZ1raN8=
github.com/Parallels/docker-machine-parallels v1.3.0/go.mod h1:HCOMm3Hulq/xuEVQMyZOuQlA+dSZpFY5kdCTZWjMVis=
github.com/Sirupsen/logrus v0.0.0-20170822132746-89742aefa4b2 h1:k1A7eIeUk6rnX2yuagwljW/pDezkK8oSpvPumT9zdZY=
Expand Down Expand Up @@ -38,6 +40,8 @@ github.com/ghodss/yaml v0.0.0-20150909031657-73d445a93680 h1:ZktWZesgun21uEDrwW7
github.com/ghodss/yaml v0.0.0-20150909031657-73d445a93680/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
github.com/gogo/protobuf v0.0.0-20170330071051-c0656edd0d9e h1:ago6fNuQ6IhszPsXkeU7qRCyfsIX7L67WDybsAPkLl8=
github.com/gogo/protobuf v0.0.0-20170330071051-c0656edd0d9e/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
github.com/golang-collections/collections v0.0.0-20130729185459-604e922904d3 h1:zN2lZNZRflqFyxVaTIU61KNKQ9C0055u9CAfpmqUvo4=
github.com/golang-collections/collections v0.0.0-20130729185459-604e922904d3/go.mod h1:nPpo7qLxd6XL3hWJG/O60sR8ZKfMCiIoNap5GvD12KU=
github.com/golang/glog v0.0.0-20141105023935-44145f04b68c h1:CbdkBQ1/PiAo0FYJhQGwASD8wrgNvTdf01g6+O9tNuA=
github.com/golang/glog v0.0.0-20141105023935-44145f04b68c/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
github.com/golang/groupcache v0.0.0-20160516000752-02826c3e7903 h1:LbsanbbD6LieFkXbj9YNNBupiGHJgFeLpO0j0Fza1h8=
Expand Down Expand Up @@ -121,6 +125,8 @@ github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 h1:Esafd1046DLDQ0W1YjYsBW+p8U2u7vzgW2SQVmlNazg=
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/nicksnyder/go-i18n/v2 v2.0.0-beta.7 h1:wCBv/ZWBkVl/x3xvw4MAMXgjtYbzyNTcZXO5jpmVQuA=
github.com/nicksnyder/go-i18n/v2 v2.0.0-beta.7/go.mod h1:JXS4+OKhbcwDoVTEj0sLFWL1vOwec2g/YBAxZ9owJqY=
github.com/olekukonko/tablewriter v0.0.0-20160923125401-bdcc175572fd h1:nEatQ6JnwCT9iYD5uqYUiFqq8tJGX25to8KVKXqya7k=
github.com/olekukonko/tablewriter v0.0.0-20160923125401-bdcc175572fd/go.mod h1:vsDQFd/mU46D+Z4whnwzcISnGGzXWMclvtLoiIKAKIo=
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
Expand Down Expand Up @@ -190,12 +196,17 @@ github.com/zchee/go-vmnet v0.0.0-20161021174912-97ebf9174097 h1:Ucx5I1l1+TWXvqFm
github.com/zchee/go-vmnet v0.0.0-20161021174912-97ebf9174097/go.mod h1:lFZSWRIpCfE/pt91hHBBpV6+x87YlCjsp+aIR2qCPPU=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2 h1:VklqNMn3ovrHsnt90PveolxSbWFaJdECFbxSq0Mqo2M=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190506204251-e1dfcc566284 h1:rlLehGeYg6jfoyz/eDqDU1iRXLKfR42nnNh57ytKEWo=
golang.org/x/crypto v0.0.0-20190506204251-e1dfcc566284/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e h1:bRhVy7zSSasaqNksaRZiA5EEI+Ei4I1nO5Jh72wfHlg=
golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190311183353-d8887717615a h1:oWX7TPOiFAMXLq8o0ikBYfCJVlRHBcsciT5bXOrH628=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190503192946-f4e77d36d62c h1:uOCk1iQW6Vc18bnC13MfzScl+wdKBmM9Y9kU7Z83/lw=
golang.org/x/net v0.0.0-20190503192946-f4e77d36d62c/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.0.0-20190115181402-5dab4167f31c h1:pcBdqVcrlT+A3i+tWsOROFONQyey9tisIQHI4xqVGLg=
golang.org/x/oauth2 v0.0.0-20190115181402-5dab4167f31c/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
Expand All @@ -204,18 +215,24 @@ golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4 h1:YUO/7uOKsKeq9UokNS62b8FY
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190227155943-e225da77a7e6 h1:bjcUS9ztw9kFmmIxJInhon/0Is3p+EHBKNgquIzo1OI=
golang.org/x/sync v0.0.0-20190227155943-e225da77a7e6/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190423024810-112230192c58 h1:8gQV6CLnAEikrhgkHFbMAEhagSSnXWGV915qUMm9mrU=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a h1:1BGLXjeY4akVXGgbC9HugT3Jv3hCI0z56oJR5vAMgBU=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223 h1:DH4skfRX4EBpamg7iV4ZlCpblAHI6s6TDM39bFZumv8=
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190507160741-ecd444e8653b h1:ag/x1USPSsqHud38I9BAC88qdNLDHHtQ4mlgQIZPPNA=
golang.org/x/sys v0.0.0-20190507160741-ecd444e8653b/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/time v0.0.0-20161028155119-f51c12702a4d h1:TnM+PKb3ylGmZvyPXmo9m/wktg7Jn/a/fNmr33HSj8g=
golang.org/x/time v0.0.0-20161028155119-f51c12702a4d/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190506145303-2d16b83fe98c h1:97SnQk1GYRXJgvwZ8fadnxDOWfKvkNQHH3CtZntPSrM=
golang.org/x/tools v0.0.0-20190506145303-2d16b83fe98c/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
google.golang.org/appengine v1.4.0 h1:/wp5JvzpHIxhs/dumFmF7BXTf3Z+dd4uXta4kVyO508=
google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
Expand Down
253 changes: 253 additions & 0 deletions hack/extract.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,253 @@
/*
Copyright 2019 The Kubernetes Authors All rights reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package main
sharifelgamal marked this conversation as resolved.
Show resolved Hide resolved

import (
"encoding/json"
"fmt"
"go/ast"
"go/parser"
"go/token"
"io/ioutil"
"net/url"
"os"
"path/filepath"
"strconv"
"strings"

"github.com/golang-collections/collections/stack"
)

var funcs map[string]struct{}
sharifelgamal marked this conversation as resolved.
Show resolved Hide resolved
var funcStack *stack.Stack
var translations map[string]interface{}

func main() {
sharifelgamal marked this conversation as resolved.
Show resolved Hide resolved
translations = make(map[string]interface{})
paths := []string{"../cmd", "../pkg"}
//paths := []string{"../cmd/minikube/cmd/delete.go"}
//paths := []string{"../pkg/minikube/cluster/cluster.go"}
funcs = map[string]struct{}{"Translate": {}}
funcStack = stack.New()
for f := range funcs {
funcStack.Push(f)
}

fmt.Println("Compiling translation strings...")
for funcStack.Len() > 0 {
f := funcStack.Pop().(string)
//fmt.Printf("-----%s------\n", f)
for _, root := range paths {
err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
if shouldCheckFile(path) {
return inspectFile(path, f)
}
return nil
})

if err != nil {
panic(err)
}
}
}

err := writeStringsToFiles()

if err != nil {
panic(err)
}

fmt.Println("Done!")
}

func writeStringsToFiles() error {
sharifelgamal marked this conversation as resolved.
Show resolved Hide resolved
translationsFiles := "../pkg/minikube/translate/translations"
e := filepath.Walk(translationsFiles, func(path string, info os.FileInfo, err error) error {
if info.Mode().IsDir() {
return nil
}
fmt.Printf("Writing to %s\n", filepath.Base(path))
var currentTranslations map[string]interface{}
f, err := ioutil.ReadFile(path)
if err != nil {
return err
sharifelgamal marked this conversation as resolved.
Show resolved Hide resolved
}
err = json.Unmarshal(f, &currentTranslations)
if err != nil {
return err
}

//fmt.Println(currentTranslations)

for k := range translations {
//fmt.Println(k)
if _, ok := currentTranslations[k]; !ok {
currentTranslations[k] = " "
}
}

c, err := json.MarshalIndent(currentTranslations, "", "\t")
if err != nil {
return err
}
err = ioutil.WriteFile(path, c, info.Mode())
return err
})

return e
}

func addFuncToList(f string) {
if _, ok := funcs[f]; !ok {
funcs[f] = struct{}{}
funcStack.Push(f)
}
}

func shouldCheckFile(path string) bool {
return strings.HasSuffix(path, ".go") && !strings.HasSuffix(path, "_test.go")
}

func inspectFile(filename string, f string) error {
fset := token.NewFileSet()
r, err := ioutil.ReadFile(filename)
if err != nil {
return err
}
//fmt.Printf(" Parsing %s\n", filename)
file, err := parser.ParseFile(fset, "", r, parser.ParseComments)
if err != nil {
return err
}

ast.Inspect(file, func(x ast.Node) bool {
fd, ok := x.(*ast.FuncDecl)
if !ok {
/*gd, ok := x.(*ast.GenDecl)
if !ok {
return true
}
for _, spec := range gd.Specs {
if vs, ok := spec.(*ast.ValueSpec); ok {
for _, v := range vs.Values {
if ue, ok := v.(*ast.UnaryExpr); ok {
fmt.Printf("%s: %s\n", ue.X, reflect.TypeOf(ue.X))
}
}
}
}*/
return true
}

for _, stmt := range fd.Body.List {
checkStmt(stmt, fd.Name.String(), f)
}
return true
})

return nil
}

func checkStmt(stmt ast.Stmt, parentFunc string, f string) {
// If this line is an expression, see if it's a function call
if t, ok := stmt.(*ast.ExprStmt); ok {
checkCallExpression(t, parentFunc, f)
}

// If this line is the beginning of an if statement, then check of the body of the block
if b, ok := stmt.(*ast.IfStmt); ok {
checkIfStmt(b, parentFunc, f)
}

// Same for loops
if b, ok := stmt.(*ast.ForStmt); ok {
for _, s := range b.Body.List {
checkStmt(s, parentFunc, f)
}
}
}

func checkIfStmt(stmt *ast.IfStmt, parentFunc string, f string) {
for _, s := range stmt.Body.List {
checkStmt(s, parentFunc, f)
}
if stmt.Else != nil {
// A straight else
if block, ok := stmt.Else.(*ast.BlockStmt); ok {
for _, s := range block.List {
checkStmt(s, parentFunc, f)
}
}

// An else if
if elseif, ok := stmt.Else.(*ast.IfStmt); ok {
checkIfStmt(elseif, parentFunc, f)
}

}
}

func checkCallExpression(t *ast.ExprStmt, parentFunc string, f string) {
if s, ok := t.X.(*ast.CallExpr); ok {
sharifelgamal marked this conversation as resolved.
Show resolved Hide resolved
sf, ok := s.Fun.(*ast.SelectorExpr)
if !ok {
addFuncToList(parentFunc)
return
}
if f == sf.Sel.Name && len(s.Args) > 0 {
addFuncToList(parentFunc)
for _, arg := range s.Args {
// Find references to strings
if i, ok := arg.(*ast.Ident); ok {
if i.Obj != nil {
if as, ok := i.Obj.Decl.(*ast.AssignStmt); ok {
if rhs, ok := as.Rhs[0].(*ast.BasicLit); ok {
if addStringToList(rhs.Value) {
break
}
}
}
}
}
// Find string arguments
if argString, ok := arg.(*ast.BasicLit); ok {
if addStringToList(argString.Value) {
break
}
}
}
}
}
}

func addStringToList(s string) bool {
// Don't translate empty strings
if len(s) > 2 {
// Parse out quote marks
stringToTranslate := s[1 : len(s)-1]
// Don't translate integers
if _, err := strconv.Atoi(stringToTranslate); err != nil {
//Don't translate URLs
if u, err := url.Parse(stringToTranslate); err != nil || u.Scheme == "" || u.Host == "" {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make sure whatever example .go file you use tests this. It's a really interesting edge case.

translations[stringToTranslate] = ""
//fmt.Printf(" %s\n", stringToTranslate)
return true
}
}
}
return false
}
Loading