-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #187 from nyaruka/legacy_move
Move legacy stuff into its own package part 2/2
- Loading branch information
Showing
12 changed files
with
337 additions
and
322 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,80 @@ | ||
package definition | ||
|
||
import ( | ||
"encoding/json" | ||
|
||
"github.com/nyaruka/goflow/flows" | ||
"github.com/nyaruka/goflow/utils" | ||
) | ||
|
||
// itemTranslations map a key for a node to a key - say "text" to "[je suis francais!]" | ||
// the translations for a specific item, e.g. | ||
// { | ||
// "text": "Do you like cheese?" | ||
// "quick_replies": ["Yes", "No"] | ||
// } | ||
type itemTranslations map[string][]string | ||
|
||
// languageTranslations map a node uuid to item_translations - say "node1-asdf" to { "text": "je suis francais!" } | ||
// the translations for a specific language, e.g. | ||
// { | ||
// "f3368070-8db8-4549-872a-e69a9d060612": { | ||
// "text": "Do you like cheese?" | ||
// "quick_replies": ["Yes", "No"] | ||
// }, | ||
// "7a1aec43-f3e1-42f0-b967-0ee75e725e3a": { ... } | ||
// } | ||
type languageTranslations map[utils.UUID]itemTranslations | ||
|
||
func (t *languageTranslations) GetTextArray(uuid utils.UUID, key string) []string { | ||
item, found := (*t)[uuid] | ||
// GetTextArray returns the requested item translation | ||
func (t languageTranslations) GetTextArray(uuid utils.UUID, property string) []string { | ||
item, found := t[uuid] | ||
if found { | ||
translation, found := item[key] | ||
translation, found := item[property] | ||
if found { | ||
return translation | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
// flowTranslations are our top level container for all the translations for a language | ||
type flowTranslations map[utils.Language]*languageTranslations | ||
// our top level container for all the translations for all languages | ||
type localization map[utils.Language]languageTranslations | ||
|
||
func NewLocalization() flows.Localization { | ||
return make(localization) | ||
} | ||
|
||
func (t flowTranslations) Languages() utils.LanguageList { | ||
languages := make(utils.LanguageList, 0, len(t)) | ||
for lang := range t { | ||
// Languages gets the list of languages included in this localization | ||
func (l localization) Languages() utils.LanguageList { | ||
languages := make(utils.LanguageList, 0, len(l)) | ||
for lang := range l { | ||
languages = append(languages, lang) | ||
} | ||
return languages | ||
} | ||
|
||
func (t flowTranslations) GetLanguageTranslations(lang utils.Language) flows.Translations { | ||
translations, found := t[lang] | ||
if found { | ||
return translations | ||
// AddItemTranslation adds a new item translation | ||
func (l localization) AddItemTranslation(lang utils.Language, itemUUID utils.UUID, property string, translated []string) { | ||
_, found := l[lang] | ||
if !found { | ||
l[lang] = make(languageTranslations) | ||
} | ||
return nil | ||
_, found = l[lang][itemUUID] | ||
if !found { | ||
l[lang][itemUUID] = make(itemTranslations) | ||
} | ||
l[lang][itemUUID][property] = translated | ||
} | ||
|
||
// GetTranslations returns the translations for the given language | ||
func (l localization) GetTranslations(lang utils.Language) flows.Translations { | ||
return l[lang] | ||
} | ||
|
||
// ReadLocalization reads entire localization flow segment | ||
func ReadLocalization(data json.RawMessage) (flows.Localization, error) { | ||
translations := &localization{} | ||
if err := json.Unmarshal(data, translations); err != nil { | ||
return nil, err | ||
} | ||
return translations, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.