Compare commits
2 Commits
6b94030b83
...
b6e9ad6160
Author | SHA1 | Date | |
---|---|---|---|
b6e9ad6160 | |||
c56c0ae198 |
@ -58,6 +58,7 @@ Since the PineTime does not have enough space to store all unicode glyphs, it on
|
|||||||
- French
|
- French
|
||||||
- Armenian
|
- Armenian
|
||||||
- Korean
|
- Korean
|
||||||
|
- Chinese
|
||||||
- Emoji
|
- Emoji
|
||||||
|
|
||||||
Place the desired map names in an array as `notifs.translit.use`. They will be evaluated in order. You can also put custom transliterations in `notifs.translit.custom`. These take priority over any other maps. The `notifs.translit` config section should look like this:
|
Place the desired map names in an array as `notifs.translit.use`. They will be evaluated in order. You can also put custom transliterations in `notifs.translit.custom`. These take priority over any other maps. The `notifs.translit` config section should look like this:
|
||||||
|
1
go.mod
1
go.mod
@ -15,6 +15,7 @@ require (
|
|||||||
github.com/mattn/go-isatty v0.0.13 // indirect
|
github.com/mattn/go-isatty v0.0.13 // indirect
|
||||||
github.com/mattn/go-runewidth v0.0.13 // indirect
|
github.com/mattn/go-runewidth v0.0.13 // indirect
|
||||||
github.com/mitchellh/mapstructure v1.4.1
|
github.com/mitchellh/mapstructure v1.4.1
|
||||||
|
github.com/mozillazg/go-pinyin v0.18.0
|
||||||
github.com/rs/zerolog v1.23.0
|
github.com/rs/zerolog v1.23.0
|
||||||
github.com/sirupsen/logrus v1.8.1 // indirect
|
github.com/sirupsen/logrus v1.8.1 // indirect
|
||||||
github.com/spf13/cast v1.4.1 // indirect
|
github.com/spf13/cast v1.4.1 // indirect
|
||||||
|
2
go.sum
2
go.sum
@ -242,6 +242,8 @@ github.com/mitchellh/mapstructure v1.4.1/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RR
|
|||||||
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||||
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
|
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
|
||||||
github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
|
github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
|
||||||
|
github.com/mozillazg/go-pinyin v0.18.0 h1:hQompXO23/0ohH8YNjvfsAITnCQImCiR/Fny8EhIeW0=
|
||||||
|
github.com/mozillazg/go-pinyin v0.18.0/go.mod h1:iR4EnMMRXkfpFVV5FMi4FNB6wGq9NV6uDWbUuPhP4Yc=
|
||||||
github.com/muka/go-bluetooth v0.0.0-20210812063148-b6c83362e27d h1:EG/xyWjHT19rkUpwsWSkyiCCmyqNwFovr9m10rhyOxU=
|
github.com/muka/go-bluetooth v0.0.0-20210812063148-b6c83362e27d h1:EG/xyWjHT19rkUpwsWSkyiCCmyqNwFovr9m10rhyOxU=
|
||||||
github.com/muka/go-bluetooth v0.0.0-20210812063148-b6c83362e27d/go.mod h1:dMCjicU6vRBk34dqOmIZm0aod6gUwZXOXzBROqGous0=
|
github.com/muka/go-bluetooth v0.0.0-20210812063148-b6c83362e27d/go.mod h1:dMCjicU6vRBk34dqOmIZm0aod6gUwZXOXzBROqGous0=
|
||||||
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646/go.mod h1:jpp1/29i3P1S/RLdc7JQKbRpFeM1dOBd8T9ki5s+AY8=
|
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646/go.mod h1:jpp1/29i3P1S/RLdc7JQKbRpFeM1dOBd8T9ki5s+AY8=
|
||||||
|
@ -73,7 +73,7 @@ func initNotifRelay(dev *infinitime.Device) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
maps := viper.GetStringSlice("notifs.translit.use")
|
maps := viper.GetStringSlice("notifs.translit.use")
|
||||||
translit.Maps["custom"] = translit.Map(viper.GetStringSlice("notifs.translit.custom"))
|
translit.Transliterators["custom"] = translit.Map(viper.GetStringSlice("notifs.translit.custom"))
|
||||||
sender = translit.Transliterate(sender, maps...)
|
sender = translit.Transliterate(sender, maps...)
|
||||||
summary = translit.Transliterate(summary, maps...)
|
summary = translit.Transliterate(summary, maps...)
|
||||||
body = translit.Transliterate(body, maps...)
|
body = translit.Transliterate(body, maps...)
|
||||||
|
@ -171,7 +171,7 @@ func handleConnection(conn net.Conn, dev *infinitime.Device) {
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
maps := viper.GetStringSlice("notifs.translit.use")
|
maps := viper.GetStringSlice("notifs.translit.use")
|
||||||
translit.Maps["custom"] = translit.Map(viper.GetStringSlice("notifs.translit.custom"))
|
translit.Transliterators["custom"] = translit.Map(viper.GetStringSlice("notifs.translit.custom"))
|
||||||
title := translit.Transliterate(reqData.Title, maps...)
|
title := translit.Transliterate(reqData.Title, maps...)
|
||||||
body := translit.Transliterate(reqData.Body, maps...)
|
body := translit.Transliterate(reqData.Body, maps...)
|
||||||
// Send notification to watch
|
// Send notification to watch
|
||||||
|
42
translit/chinese.go
Normal file
42
translit/chinese.go
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
package translit
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"strings"
|
||||||
|
"unicode"
|
||||||
|
|
||||||
|
"github.com/mozillazg/go-pinyin"
|
||||||
|
)
|
||||||
|
|
||||||
|
// ChineseTranslit implements Transliterator using a pinyin
|
||||||
|
// conversion library.
|
||||||
|
type ChineseTranslit struct{}
|
||||||
|
|
||||||
|
func (ct *ChineseTranslit) Transliterate(s string) string {
|
||||||
|
// Create buffer for final output
|
||||||
|
outBuf := &bytes.Buffer{}
|
||||||
|
// Create buffer to temporarily store chinese characters
|
||||||
|
tmpBuf := &bytes.Buffer{}
|
||||||
|
// For every character in string
|
||||||
|
for _, char := range s {
|
||||||
|
// If character in Han range
|
||||||
|
if unicode.Is(unicode.Han, char) {
|
||||||
|
// Write character to temporary buffer
|
||||||
|
tmpBuf.WriteRune(char)
|
||||||
|
} else {
|
||||||
|
// If buffer contains characters
|
||||||
|
if tmpBuf.Len() > 0 {
|
||||||
|
// Convert to pinyin (without tones)
|
||||||
|
out := pinyin.LazyConvert(tmpBuf.String(), nil)
|
||||||
|
// Write space-separated string to output
|
||||||
|
outBuf.WriteString(strings.Join(out, " "))
|
||||||
|
// Reset temporary buffer
|
||||||
|
tmpBuf.Reset()
|
||||||
|
}
|
||||||
|
// Write character to output
|
||||||
|
outBuf.WriteRune(char)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Return output string
|
||||||
|
return outBuf.String()
|
||||||
|
}
|
@ -9,7 +9,7 @@ func Transliterate(s string, useMaps ...string) string {
|
|||||||
// Create variable to store modified string
|
// Create variable to store modified string
|
||||||
out := s
|
out := s
|
||||||
// If custom map exists
|
// If custom map exists
|
||||||
if customMap, ok := Maps["custom"]; ok {
|
if customMap, ok := Transliterators["custom"]; ok {
|
||||||
// Perform transliteration with it
|
// Perform transliteration with it
|
||||||
out = customMap.Transliterate(out)
|
out = customMap.Transliterate(out)
|
||||||
}
|
}
|
||||||
@ -20,7 +20,7 @@ func Transliterate(s string, useMaps ...string) string {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
// Get requested map
|
// Get requested map
|
||||||
translitMap, ok := Maps[useMap]
|
translitMap, ok := Transliterators[useMap]
|
||||||
if !ok {
|
if !ok {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@ -47,9 +47,9 @@ func (mt Map) Transliterate(s string) string {
|
|||||||
return strings.NewReplacer(mt...).Replace(s)
|
return strings.NewReplacer(mt...).Replace(s)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Maps stores transliteration maps as slices to preserve order.
|
// Transliterators stores transliterator implementations for each supported language.
|
||||||
// Some of these maps were sourced from https://codeberg.org/Freeyourgadget/Gadgetbridge
|
// Some of these were sourced from https://codeberg.org/Freeyourgadget/Gadgetbridge
|
||||||
var Maps = map[string]Transliterator{
|
var Transliterators = map[string]Transliterator{
|
||||||
"eASCII": Map{
|
"eASCII": Map{
|
||||||
"œ", "oe",
|
"œ", "oe",
|
||||||
"ª", "a",
|
"ª", "a",
|
||||||
@ -465,4 +465,5 @@ var Maps = map[string]Transliterator{
|
|||||||
"💤", ":zzz:",
|
"💤", ":zzz:",
|
||||||
},
|
},
|
||||||
"Korean": &KoreanTranslit{},
|
"Korean": &KoreanTranslit{},
|
||||||
|
"Chinese": &ChineseTranslit{},
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user