nicksnyder / go-i18n

Translate your Go program into multiple languages.
MIT License
2.96k stars 271 forks source link

expected declaration, found messageEn #328

Closed n00bsi closed 2 months ago

n00bsi commented 2 months ago

Hi, as golang noob - just tyed to get the hello world to run... but did not work.

go Version: 1.22.4

main.go

package main
import "fmt"
import "github.com/nicksnyder/go-i18n/v2/i18n"

func main() {
    fmt.Println("\nhello world!\n")
}

messageEn := i18n.Message {  //1
  ID: "hello world!",
  Other: "Hello!",
}

messageFr := i18n.Message {  //2
  ID: "hello world!",
  Other: "Bonjour le monde",
}

bundle := i18n.NewBundle(language.English)  //1
bundle.AddMessages(language.English, &messageEn)  //2
bundle.AddMessages(language.French, &messageFr)  //3
localizer := i18n.NewLocalizer(bundle,  //4
             language.French.String(),
             language.English.String())
localizeConfig := i18n.LocalizeConfig {  //5
  MessageID: "hello world!",
}

localization, _ := localizer.Localize(&localizeConfig)  //6

got error from the Codium IDE - "expected declaration, found messageEn"

nicksnyder commented 2 months ago

You need to put your code inside the main function.

package main

import (
    "fmt"

    "github.com/nicksnyder/go-i18n/v2/i18n"
    "golang.org/x/text/language"
)

func main() {
    fmt.Println("\nhello world!\n")
    messageEn := i18n.Message{ //1
        ID:    "hello world!",
        Other: "Hello!",
    }

    messageFr := i18n.Message{ //2
        ID:    "hello world!",
        Other: "Bonjour le monde",
    }

    bundle := i18n.NewBundle(language.English)       //1
    bundle.AddMessages(language.English, &messageEn) //2
    bundle.AddMessages(language.French, &messageFr)  //3
    localizer := i18n.NewLocalizer(bundle,           //4
        language.French.String(),
        language.English.String())
    localizeConfig := i18n.LocalizeConfig{ //5
        MessageID: "hello world!",
    }

    localization, _ := localizer.Localize(&localizeConfig) //6
}
n00bsi commented 2 months ago

@nicksnyder

thanks - but...

$ go run hello_world_multilang.go 
# command-line-arguments
./hello_world_multilang.go:32:2: localization declared and not used

this is this line: calization, _ := localizer.Localize(&localizeConfig) //6

nicksnyder commented 2 months ago

I was just illustrating how to put your code inside of the main function, if your code didn't compile before, it isn't going to compile now.

There is plenty of documentation and working examples for you to look at to get started: