#!/bin/sh
. "${srcdir=.}/init.sh"; path_prepend_ . ../src

# Test Go support: github.com/leonelquinteros/gotext package,
# with multi-locale API, for a standalone application.

cat <<\EOF > xg-go-2.go
package main

import (
	// Documentation: https://pkg.go.dev/fmt
	"fmt"
	// Documentation: https://pkg.go.dev/github.com/leonelquinteros/gotext
	"github.com/leonelquinteros/gotext"
	// Documentation: https://pkg.go.dev/os
	"os"
)

// Returns the language in the form "ll_CC".
// Alternatives:
// - https://pkg.go.dev/github.com/Xuanwo/go-locale
// - https://pkg.go.dev/github.com/jeandeaual/go-locale
func getUserLanguage() string {
	// Look at the POSIX environment variables.
	for _, variable := range []string{"LC_ALL", "LC_MESSAGES", "LANG"} {
		if value := os.Getenv(variable); value != "" {
			return gotext.SimplifiedLocale(value)
		}
	}
	// The "C" locale is essentially the same as the en-US locale.
	return "en_US"
}

func main () {
	// Specify locale.
	//language := "fr_FR"
	language := getUserLanguage()

	// Specify localedir.
	localizer := gotext.NewLocale("@localedir@", language)
	// Specify domain.
	localizer.AddDomain("hello")

	fmt.Println(localizer.Get("Hello, world!"))
	fmt.Println(localizer.Get("This program is running as process number %d.",
	                          os.Getpid()))
}
EOF

: ${XGETTEXT=xgettext}
${XGETTEXT} --omit-header --no-location -d xg-go-2.tmp xg-go-2.go || Exit 1
LC_ALL=C tr -d '\r' < xg-go-2.tmp.po > xg-go-2.po || Exit 1

cat <<\EOF > xg-go-2.ok
msgid "Hello, world!"
msgstr ""

#, go-format
msgid "This program is running as process number %d."
msgstr ""
EOF

: ${DIFF=diff}
${DIFF} xg-go-2.ok xg-go-2.po || Exit 1

exit 0
