Skip to content

Commit

Permalink
Add functions to map iso code string to language (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
pemistahl committed Sep 5, 2023
1 parent e26421c commit 20e6689
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 4 deletions.
18 changes: 18 additions & 0 deletions isocode.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package lingua

import "strings"

// IsoCode639_1 is the type used for enumerating the ISO 639-1 code
// representations of the supported languages.
//
Expand Down Expand Up @@ -487,3 +489,19 @@ const (
// UnknownIsoCode639_3 is the ISO 639-3 code for Unknown.
UnknownIsoCode639_3
)

var stringToIsoCode639_1 = func() map[string]IsoCode639_1 {
m := make(map[string]IsoCode639_1)
for isoCode := AF; isoCode <= ZU; isoCode++ {
m[strings.ToLower(isoCode.String())] = isoCode
}
return m
}()

var stringToIsoCode639_3 = func() map[string]IsoCode639_3 {
m := make(map[string]IsoCode639_3)
for isoCode := AFR; isoCode <= ZUL; isoCode++ {
m[strings.ToLower(isoCode.String())] = isoCode
}
return m
}()
28 changes: 24 additions & 4 deletions language.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package lingua

import "strings"

// Language is the type used for enumerating the so far 75 languages which can
// be detected by Lingua.
//
Expand Down Expand Up @@ -146,25 +148,43 @@ func AllLanguagesWithLatinScript() []Language {
}

// GetLanguageFromIsoCode639_1 returns the language for the given
// ISO 639-1 code.
// ISO 639-1 code enum value.
func GetLanguageFromIsoCode639_1(isoCode IsoCode639_1) Language {
for _, language := range AllLanguages() {
if language.IsoCode639_1() == isoCode {
return language
}
}
return -1
return Unknown
}

// GetLanguageFromIsoCode639_1String returns the language for the given
// ISO 639-1 code string value.
func GetLanguageFromIsoCode639_1String(isoCode string) Language {
if isoCodeEnum, ok := stringToIsoCode639_1[strings.ToLower(isoCode)]; ok {
return GetLanguageFromIsoCode639_1(isoCodeEnum)
}
return Unknown
}

// GetLanguageFromIsoCode639_3 returns the language for the given
// ISO 639-3 code.
// ISO 639-3 code enum value.
func GetLanguageFromIsoCode639_3(isoCode IsoCode639_3) Language {
for _, language := range AllLanguages() {
if language.IsoCode639_3() == isoCode {
return language
}
}
return -1
return Unknown
}

// GetLanguageFromIsoCode639_3String returns the language for the given
// ISO 639-3 code string value.
func GetLanguageFromIsoCode639_3String(isoCode string) Language {
if isoCodeEnum, ok := stringToIsoCode639_3[strings.ToLower(isoCode)]; ok {
return GetLanguageFromIsoCode639_3(isoCodeEnum)
}
return Unknown
}

func allLanguagesWithScript(script alphabet) (languages []Language) {
Expand Down
30 changes: 30 additions & 0 deletions language_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,3 +259,33 @@ func TestAllLanguagesWithLatinScript(t *testing.T) {
},
AllLanguagesWithLatinScript())
}

func TestGetLanguageFromIsoCode639_1(t *testing.T) {
assert.Equal(t, Afrikaans, GetLanguageFromIsoCode639_1(AF))
assert.Equal(t, Zulu, GetLanguageFromIsoCode639_1(ZU))
assert.Equal(t, Unknown, GetLanguageFromIsoCode639_1(UnknownIsoCode639_1))
}

func TestGetLanguageFromIsoCode639_1String(t *testing.T) {
assert.Equal(t, Afrikaans, GetLanguageFromIsoCode639_1String("AF"))
assert.Equal(t, Afrikaans, GetLanguageFromIsoCode639_1String("af"))
assert.Equal(t, Zulu, GetLanguageFromIsoCode639_1String("ZU"))
assert.Equal(t, Zulu, GetLanguageFromIsoCode639_1String("zu"))
assert.Equal(t, Unknown, GetLanguageFromIsoCode639_1String("afr"))
assert.Equal(t, Unknown, GetLanguageFromIsoCode639_1String("xy"))
}

func TestGetLanguageFromIsoCode639_3(t *testing.T) {
assert.Equal(t, Afrikaans, GetLanguageFromIsoCode639_3(AFR))
assert.Equal(t, Zulu, GetLanguageFromIsoCode639_3(ZUL))
assert.Equal(t, Unknown, GetLanguageFromIsoCode639_3(UnknownIsoCode639_3))
}

func TestGetLanguageFromIsoCode639_3String(t *testing.T) {
assert.Equal(t, Afrikaans, GetLanguageFromIsoCode639_3String("AFR"))
assert.Equal(t, Afrikaans, GetLanguageFromIsoCode639_3String("afr"))
assert.Equal(t, Zulu, GetLanguageFromIsoCode639_3String("ZUL"))
assert.Equal(t, Zulu, GetLanguageFromIsoCode639_3String("zul"))
assert.Equal(t, Unknown, GetLanguageFromIsoCode639_3String("af"))
assert.Equal(t, Unknown, GetLanguageFromIsoCode639_3String("xyz"))
}

0 comments on commit 20e6689

Please sign in to comment.