Multiple languages

Struggling with multi-language site for a while. There are many ways to do this as I understand. I found one way using GitHub - leonelquinteros/gotext: Go (Golang) GNU gettext utilities package. Which seems to be simpler than golang.org/x/text IMHO.

I should be glad if anybody could make a review of my code and tell me if I am on the right track or not.

Live preview

1 Like

Hey,

Overall looks good to me, a few changes, I would make is removing init() and maybe using GitHub - nicksnyder/go-i18n: Translate your Go program into multiple languages. but it is highly personal, I only used hugo(it was the easiest option) for i18n in the past

one aspect I can see which will improve, you can use go:embed for your public static assets/templates with embed.FS so that your final binary can be standalone with locale/ files

you also won’t need a mutex if you are only reading from the map only, I wouldn’t do in-memory unsafe map and read/write in http handlers because it would grow eventually, probably what you need is something like a thread-safe LRU(least recently used cache if you continue(you will need eviction at some point of the map or the cache) with this library and approach GitHub - hashicorp/golang-lru: Golang LRU cache)

1 Like

Thank you for your input.

The init{} function creates a map variable in the memory amongst other things. So I do not really know how to replace this. I was confused about which solution to use from the beginning. There are several ways to go. golang/x seemed to be harder and go-i18n uses .toml which looks close to .po, but as the .po can be compiled into .mo files it may be faster? What are the benefits of go-i18n compared to leonelquinteros/gotext?

I will probably use the go:embed for the locale translation folder. But for the html stuff I am still hesitant. Mostly because it can contain tons of files in the future and the ability to minor edits of errors on-the-fly on the server will no longer be possible.

Do you say that locking/unlocking is not needed using maps?

So var translations map[string]*gotext.Po is an in-memory unsafe map?