create amount.go:localizedLanguagePrinter global that parses $LANG or defaults to AmericanEnglish
parent
706de4d627
commit
ea64f6ea98
|
|
@ -1,9 +1,49 @@
|
|||
package analyzer
|
||||
|
||||
import "math"
|
||||
import (
|
||||
"math"
|
||||
"os"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"golang.org/x/text/currency"
|
||||
"golang.org/x/text/language"
|
||||
"golang.org/x/text/message"
|
||||
)
|
||||
|
||||
type Amount float64
|
||||
|
||||
var localizedLanguagePrinter = func() *message.Printer {
|
||||
_newLocalizedLanguage := func() (language.Tag, bool) {
|
||||
environmentLang := os.Getenv("LANG")
|
||||
if environmentLang == "" {
|
||||
return language.Tag{}, false
|
||||
}
|
||||
regexp := regexp.MustCompile(`^([a-z]+)_([A-Z]+)\.[A-Z0-9-]+$`)
|
||||
results := regexp.FindAllStringSubmatch(environmentLang, -1)
|
||||
if len(results) != 1 || len(results[0]) != 3 {
|
||||
return language.Tag{}, false
|
||||
}
|
||||
|
||||
base, err := language.ParseBase(results[0][1])
|
||||
if err != nil {
|
||||
return language.Tag{}, false
|
||||
}
|
||||
region, err := language.ParseRegion(results[0][2])
|
||||
if err != nil {
|
||||
return language.Tag{}, false
|
||||
}
|
||||
result, err := language.Compose(base, region)
|
||||
return result, err == nil
|
||||
}
|
||||
|
||||
result, ok := _newLocalizedLanguage()
|
||||
if !ok {
|
||||
return message.NewPrinter(language.AmericanEnglish)
|
||||
}
|
||||
return message.NewPrinter(result)
|
||||
}()
|
||||
|
||||
// Rounded rounds a float to the number of places specified.
|
||||
func (amount Amount) Rounded(places int) Amount {
|
||||
p := math.Pow10(places)
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
module github.com/digits/WorkSample-Go
|
||||
|
||||
go 1.13
|
||||
|
||||
require golang.org/x/text v0.13.0
|
||||
|
|
|
|||
|
|
@ -0,0 +1,32 @@
|
|||
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
|
||||
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
|
||||
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
|
||||
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
|
||||
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
|
||||
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
|
||||
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
|
||||
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
|
||||
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
|
||||
golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k=
|
||||
golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
|
||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
||||
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
|
||||
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
|
||||
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
131
todo.yaml
131
todo.yaml
|
|
@ -43,6 +43,9 @@ todo:
|
|||
fmt.Println()
|
||||
p.Printf("%d", currency.ISO.Kind(currency.Cash)(currency.EUR.Amount(12.255)))
|
||||
fmt.Println()
|
||||
* $ env | grep =en
|
||||
LANG=en_US.UTF-8
|
||||
so I can try $LANG.split(.)[0].split(_) or fallback to english because gotta have some default
|
||||
- TestTransaction_String
|
||||
- TestTransaction_String/MCCOLLOMS_INC
|
||||
- TestTransaction_String/QUANTUM_ELECTRIC_INC
|
||||
|
|
@ -54,12 +57,8 @@ todo:
|
|||
- TestAnalyzer_BigSpendersReport
|
||||
- TestAnalyzer_TransactionsFromURLs
|
||||
- TestAnalyzer_TransactionsFromURLsConcurrent
|
||||
- todo: analyzer_test.go
|
||||
subtasks:
|
||||
- tdd because cicd because no qa or ops
|
||||
- fix failing `go test`
|
||||
- fix tests in order pro tip
|
||||
- analyzer_test.go is OK
|
||||
- amount.go:Rounded probably does NOT handle float precision well... it is float64
|
||||
tho...
|
||||
scheduled: []
|
||||
done:
|
||||
- todo: hello world
|
||||
|
|
@ -231,3 +230,123 @@ done:
|
|||
- TestAnalyzer_TransactionsFromURLs
|
||||
- TestAnalyzer_TransactionsFromURLsConcurrent
|
||||
ts: Sun Oct 15 10:57:33 MDT 2023
|
||||
- todo: go test
|
||||
subtasks:
|
||||
- my `go mod tidy` actually cleared `go.mod` file, probably weird localhost backwards
|
||||
compatilble stuff
|
||||
- todo: TestFormatUSD
|
||||
subtasks:
|
||||
- TestFormatUSD/zero
|
||||
- TestFormatUSD/positive
|
||||
- TestFormatUSD/negative
|
||||
- TestFormatUSD/large
|
||||
- no implementation
|
||||
- naive `-$xx,yyy.zz`
|
||||
- todo: comment emphasizes localization, which is minimum `-<always $>xx.yyy,zz`
|
||||
details: |
|
||||
BUT no indicator of what is "local". Could use system timezone/location stuff but that's weird.
|
||||
|
||||
https://phrase.com/blog/posts/number-localization/
|
||||
english => xxx,yyy.zz
|
||||
french => xxx yyy.zz
|
||||
arabic => non-digits
|
||||
azerbaijan => xxx.yyy,zz
|
||||
hindi => ww,xx,yyy.zz
|
||||
|
||||
https://dev.to/theodesp/a-step-by-step-guide-to-go-internationalization-i18n--localization-l10n-4ig2
|
||||
solved problem?
|
||||
* golang.org/x/text
|
||||
p := message.NewPrinter(language.BritishEnglish)
|
||||
p.Printf("There are %v flowers in our garden.", 1500)
|
||||
language.Greek, language.BrazilianPortuguese
|
||||
language.Make("el"), language.Parse("en-UK")
|
||||
ja, _ := language.ParseBase("ja")
|
||||
jp, _ := language.ParseRegion("JP")
|
||||
jpLngTag, _ := language.Compose(ja, jp)
|
||||
fmt.Println(jpLngTag) // prints ja-JP
|
||||
* reminds me of language of like C_LANG env variable I've seen...
|
||||
* neato all under golang.org/x/text
|
||||
p := message.NewPrinter(language.English)
|
||||
p.Printf("%d", currency.Symbol(currency.USD.Amount(0.1)))
|
||||
fmt.Println()
|
||||
p.Printf("%d", currency.NarrowSymbol(currency.JPY.Amount(1.6)))
|
||||
fmt.Println()
|
||||
p.Printf("%d", currency.ISO.Kind(currency.Cash)(currency.EUR.Amount(12.255)))
|
||||
fmt.Println()
|
||||
- TestTransaction_String
|
||||
- TestTransaction_String/MCCOLLOMS_INC
|
||||
- TestTransaction_String/QUANTUM_ELECTRIC_INC
|
||||
- TestTransaction_String/MARRIOTT_33716_NEW_ORLEAN
|
||||
- TestAnalyzer_TransctionsSum
|
||||
- TestAnalyzer_LargestTransaction
|
||||
- TestAnalyzer_DuplicatesExcluded
|
||||
- TestAnalyzer_GroupByCategory
|
||||
- TestAnalyzer_BigSpendersReport
|
||||
- TestAnalyzer_TransactionsFromURLs
|
||||
- TestAnalyzer_TransactionsFromURLsConcurrent
|
||||
ts: Sun Oct 15 10:59:12 MDT 2023
|
||||
- todo: go test
|
||||
subtasks:
|
||||
- my `go mod tidy` actually cleared `go.mod` file, probably weird localhost backwards
|
||||
compatilble stuff
|
||||
- todo: TestFormatUSD
|
||||
subtasks:
|
||||
- TestFormatUSD/zero
|
||||
- TestFormatUSD/positive
|
||||
- TestFormatUSD/negative
|
||||
- TestFormatUSD/large
|
||||
- no implementation
|
||||
- naive `-$xx,yyy.zz`
|
||||
- todo: comment emphasizes localization, which is minimum `-<always $>xx.yyy,zz`
|
||||
details: |
|
||||
BUT no indicator of what is "local". Could use system timezone/location stuff but that's weird.
|
||||
|
||||
https://phrase.com/blog/posts/number-localization/
|
||||
english => xxx,yyy.zz
|
||||
french => xxx yyy.zz
|
||||
arabic => non-digits
|
||||
azerbaijan => xxx.yyy,zz
|
||||
hindi => ww,xx,yyy.zz
|
||||
|
||||
https://dev.to/theodesp/a-step-by-step-guide-to-go-internationalization-i18n--localization-l10n-4ig2
|
||||
solved problem?
|
||||
* golang.org/x/text
|
||||
p := message.NewPrinter(language.BritishEnglish)
|
||||
p.Printf("There are %v flowers in our garden.", 1500)
|
||||
language.Greek, language.BrazilianPortuguese
|
||||
language.Make("el"), language.Parse("en-UK")
|
||||
ja, _ := language.ParseBase("ja")
|
||||
jp, _ := language.ParseRegion("JP")
|
||||
jpLngTag, _ := language.Compose(ja, jp)
|
||||
fmt.Println(jpLngTag) // prints ja-JP
|
||||
* reminds me of language of like C_LANG env variable I've seen...
|
||||
* neato all under golang.org/x/text
|
||||
p := message.NewPrinter(language.English)
|
||||
p.Printf("%d", currency.Symbol(currency.USD.Amount(0.1)))
|
||||
fmt.Println()
|
||||
p.Printf("%d", currency.NarrowSymbol(currency.JPY.Amount(1.6)))
|
||||
fmt.Println()
|
||||
p.Printf("%d", currency.ISO.Kind(currency.Cash)(currency.EUR.Amount(12.255)))
|
||||
fmt.Println()
|
||||
* $ env | grep =en
|
||||
LANG=en_US.UTF-8
|
||||
so I can try $LANG.split(.)[0].split(_) or fallback to english because gotta have some default
|
||||
- TestTransaction_String
|
||||
- TestTransaction_String/MCCOLLOMS_INC
|
||||
- TestTransaction_String/QUANTUM_ELECTRIC_INC
|
||||
- TestTransaction_String/MARRIOTT_33716_NEW_ORLEAN
|
||||
- TestAnalyzer_TransctionsSum
|
||||
- TestAnalyzer_LargestTransaction
|
||||
- TestAnalyzer_DuplicatesExcluded
|
||||
- TestAnalyzer_GroupByCategory
|
||||
- TestAnalyzer_BigSpendersReport
|
||||
- TestAnalyzer_TransactionsFromURLs
|
||||
- TestAnalyzer_TransactionsFromURLsConcurrent
|
||||
ts: Sun Oct 15 11:10:53 MDT 2023
|
||||
- todo: analyzer_test.go
|
||||
subtasks:
|
||||
- tdd because cicd because no qa or ops
|
||||
- fix failing `go test`
|
||||
- fix tests in order pro tip
|
||||
- analyzer_test.go is OK
|
||||
ts: Sun Oct 15 11:10:57 MDT 2023
|
||||
|
|
|
|||
Loading…
Reference in New Issue