From fe4b0ec203c0c4dfcd28580729239abd04818b6d Mon Sep 17 00:00:00 2001 From: Elara Musayelyan Date: Wed, 6 Oct 2021 09:41:33 -0700 Subject: [PATCH] Add init functions to transliterators --- README.md | 2 +- translit/armenian.go | 2 +- translit/chinese.go | 2 ++ translit/korean.go | 2 ++ translit/translit.go | 12 ++++++++---- 5 files changed, 14 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 9d9e920..b4dbff3 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ ### Features - Notification relay -- Notificstion transliteration +- Notification transliteration - Music control - Get info from watch (HRM, Battery level, Firmware version) - Set current time diff --git a/translit/armenian.go b/translit/armenian.go index 43eda6c..2c7acd2 100644 --- a/translit/armenian.go +++ b/translit/armenian.go @@ -120,7 +120,7 @@ var armenianMap = []string{ "ւ", "", } -func init() { +func (at *ArmenianTranslit) Init() { lower := armenianMap for i, val := range lower { if i%2 == 1 { diff --git a/translit/chinese.go b/translit/chinese.go index 252210d..41d1170 100644 --- a/translit/chinese.go +++ b/translit/chinese.go @@ -12,6 +12,8 @@ import ( // conversion library. type ChineseTranslit struct{} +func (ChineseTranslit) Init() {} + func (ct *ChineseTranslit) Transliterate(s string) string { // Create buffer for final output outBuf := &bytes.Buffer{} diff --git a/translit/korean.go b/translit/korean.go index cc47b0b..63d47d0 100644 --- a/translit/korean.go +++ b/translit/korean.go @@ -39,6 +39,8 @@ var compatJamoBlock = &unicode.RangeTable{ // This was translated to Go from the code in https://codeberg.org/Freeyourgadget/Gadgetbridge type KoreanTranslit struct{} +func (KoreanTranslit) Init() {} + // User input consisting of isolated jamo is usually mapped to the KS X 1001 compatibility // block, but jamo resulting from decomposed syllables are mapped to the modern one. This // function maps compat jamo to modern ones where possible and returns all other characters diff --git a/translit/translit.go b/translit/translit.go index 8228118..d16f188 100644 --- a/translit/translit.go +++ b/translit/translit.go @@ -9,9 +9,9 @@ func Transliterate(s string, useMaps ...string) string { // Create variable to store modified string out := s // If custom map exists - if customMap, ok := Transliterators["custom"]; ok { + if custom, ok := Transliterators["custom"]; ok { // Perform transliteration with it - out = customMap.Transliterate(out) + out = custom.Transliterate(out) } // For every map to use for _, useMap := range useMaps { @@ -20,12 +20,13 @@ func Transliterate(s string, useMaps ...string) string { continue } // Get requested map - translitMap, ok := Transliterators[useMap] + transliterator, ok := Transliterators[useMap] if !ok { continue } + transliterator.Init() // Perform transliteration - out = translitMap.Transliterate(out) + out = transliterator.Transliterate(out) } // Return result return out @@ -36,6 +37,7 @@ func Transliterate(s string, useMaps ...string) string { // and returns the resulting string. type Transliterator interface { Transliterate(string) string + Init() } // Map implements Transliterator using a slice where @@ -47,6 +49,8 @@ func (mt Map) Transliterate(s string) string { return strings.NewReplacer(mt...).Replace(s) } +func (Map) Init() {} + // Transliterators stores transliterator implementations for each supported language. // Some of these were sourced from https://codeberg.org/Freeyourgadget/Gadgetbridge var Transliterators = map[string]Transliterator{