Skip to content

Commit

Permalink
Add functions to map iso code string to iso code enum (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
pemistahl committed Sep 5, 2023
1 parent e26421c commit a8305a1
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 4 deletions.
34 changes: 34 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,35 @@ const (
// UnknownIsoCode639_3 is the ISO 639-3 code for Unknown.
UnknownIsoCode639_3
)

// GetIsoCode639_1FromValue returns the ISO 639-1 code for the given name.
func GetIsoCode639_1FromValue(name string) IsoCode639_1 {
if isoCodeEnum, ok := stringToIsoCode639_1[strings.ToLower(name)]; ok {
return isoCodeEnum
}
return UnknownIsoCode639_1
}

// GetIsoCode639_3FromValue returns the ISO 639-3 code for the given name.
func GetIsoCode639_3FromValue(name string) IsoCode639_3 {
if isoCodeEnum, ok := stringToIsoCode639_3[strings.ToLower(name)]; ok {
return isoCodeEnum
}
return 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
}()
40 changes: 40 additions & 0 deletions isocode_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright © 2021-present Peter M. Stahl [email protected]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either expressed or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package lingua

import (
"github.com/stretchr/testify/assert"
"testing"
)

func TestGetIsoCode639_1FromValue(t *testing.T) {
assert.Equal(t, AF, GetIsoCode639_1FromValue("AF"))
assert.Equal(t, AF, GetIsoCode639_1FromValue("af"))
assert.Equal(t, ZU, GetIsoCode639_1FromValue("ZU"))
assert.Equal(t, ZU, GetIsoCode639_1FromValue("zu"))
assert.Equal(t, UnknownIsoCode639_1, GetIsoCode639_1FromValue("xy"))
assert.Equal(t, UnknownIsoCode639_1, GetIsoCode639_1FromValue(""))
}

func TestGetIsoCode639_3FromValue(t *testing.T) {
assert.Equal(t, AFR, GetIsoCode639_3FromValue("AFR"))
assert.Equal(t, AFR, GetIsoCode639_3FromValue("afr"))
assert.Equal(t, ZUL, GetIsoCode639_3FromValue("ZUL"))
assert.Equal(t, ZUL, GetIsoCode639_3FromValue("zul"))
assert.Equal(t, UnknownIsoCode639_3, GetIsoCode639_3FromValue("xyz"))
assert.Equal(t, UnknownIsoCode639_3, GetIsoCode639_3FromValue(""))
}
8 changes: 4 additions & 4 deletions language.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,25 +146,25 @@ 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_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
}

func allLanguagesWithScript(script alphabet) (languages []Language) {
Expand Down
12 changes: 12 additions & 0 deletions language_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,3 +259,15 @@ 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_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))
}

0 comments on commit a8305a1

Please sign in to comment.