Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add possibility to select language by ISO string as part of this library #44

Closed
kostrahb opened this issue Aug 27, 2023 · 2 comments
Closed
Milestone

Comments

@kostrahb
Copy link

kostrahb commented Aug 27, 2023

I would really appreciate possibility to select language by ISO string as part of this library - I plan to load some configuration including ISO string from json and keeping mapping by myself is kind of pain. Something along these lines would be great:

stringToIsoCode639_1 = map[string]IsoCode639_1 {
	"AF": AF,
	...
}

func GetLanguageFromStringIsoCode639_1(code string) Language {
	for _, language := range AllLanguages() {
		if language.IsoCode639_1() == stringToIsoCode639_1[code] {
			return language
		}
	}
	return -1
}

Also, I noticed that this function is not exactly optimal. It has linear complexity with regards to number of languages. It's probably not noticeable due to relatively small number of languages, but still, it can be also optimized by lookup map:

IsoCode639_1ToLanguage = map[IsoCode639_1]Language {
	AF: Afrikaans,
	...
}

func GetLanguageFromIsoCode639_1(isoCode IsoCode639_1) Language {
	if val, ok := IsoCode639_1ToLanguage[isoCode]; ok {
		return val
	}
	return -1
}
@kostrahb
Copy link
Author

Ok, added a PR: #45

@pemistahl
Copy link
Owner

pemistahl commented Sep 5, 2023

Right, that's a good idea @kostrahb. I've decided to write the functions GetIsoCode639_1FromValue() and GetIsoCode639_3FromValue() as I don't want to have more large mappings in my code. Golang code is bloated enough already. The difference in performance is negligible imho.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants