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

Sections in YML I18N File? #1641

Closed
AlbinoGeek opened this issue Sep 22, 2020 · 4 comments
Closed

Sections in YML I18N File? #1641

AlbinoGeek opened this issue Sep 22, 2020 · 4 comments

Comments

@AlbinoGeek
Copy link

Summary

So, I translated my existing .ini internationalization file into .yml format, but none of the keys registers now.

I have confirmed the .yml file in question is being loaded as an intentional syntax error throws expected errors in iris startup.

Code

app.I18n.DefaultMessageFunc = func(langInput, langMatched, key string, args ...interface{}) string {
	app.Logger().Warnf("user language input: %s: matched as: %s: not found key: %s: args: %v", langInput, langMatched, key, args)
	return key
}

app.I18n.Subdomain = false
if err := app.I18n.Load(
	fmt.Sprintf("%s/%s", Conf.Dir.Locales, Conf.Lang.FileGlob),
	append(
		[]string{Conf.Lang.Default},
		Conf.Lang.Supported...,
	)...); err != nil {
	app.Logger().Error("failed to load languages", "error", err) // syntax error triggers this as expected
}

Error

[WARN] 2020/09/21 22:18 user language input: en-GB,en;q=0.9: matched as: en-US: not found key: nav.home: args: []
[WARN] 2020/09/21 22:18 user language input: en-GB,en;q=0.9: matched as: en-US: not found key: nav.user: args: []
[WARN] 2020/09/21 22:18 user language input: en-GB,en;q=0.9: matched as: en-US: not found key: footer.privacy: args: []
[WARN] 2020/09/21 22:18 user language input: en-GB,en;q=0.9: matched as: en-US: not found key: footer.terms: args: []
[WARN] 2020/09/21 22:18 user language input: en-GB,en;q=0.9: matched as: en-US: not found key: footer.contact: args: []

File

root:
  user: Account

nav:
  home: Home
  user: '{{tr "root.user"}}'

footer:
  contact: Contact Us
  privacy: Privacy Policy
  terms: Terms of Service
@kataras
Copy link
Owner

kataras commented Sep 22, 2020

Sections in yaml are not supported, only .ini has that term ([section]). I am working on a feature that requires the above syntax for pluralization. On yaml the above is a map. Will see what I can do to support that on the upcoming i18n commit. But it's NOT a bug. It's more like a feature request.

@kataras
Copy link
Owner

kataras commented Sep 22, 2020

OK @AlbinoGeek , I've done that locally. Here is a full example of what the upcoming commit brings:

Vars:
  - minutes:
      # possible values:
      # other
      # zero
      # one
      # two
      # few
      # many
      # "=x"  - where x is a number
      # "<x"
      # format - to customize the format, which defaults to %d .
      one: "minute"
      other: "minutes"
      format: "%d" # defaults to that.
  - dogs:
      one: "dog"
      other: "dogs"
  - houses:
      one: "house"
      other: "houses"
  - gender:
      "=1": "She" # 1 for female
      "=2": "He" # 2 for male

YouLate: "You are %[1]d ${minutes} late."
Classic: "classic"

FreeDay:
  "=3": "You have three days and %[2]d ${minutes} off." # "FreeDay" 3, 15 (plurals + variable pluralization)
  one:  "You have a day off" # "FreeDay", 1
  other: "You have %[1]d free days" # "FreeDay", 5

HeIsHome: "%s is home"
HouseCount: "${gender} (%[2]s) has %[3]d ${houses}"
# Local variables:
# HouseCount:
#    Text: "${gender} has %[2]d ${houses}"
#    Vars:
#     - gender:
#         "=1": "She" # 1 for female
#         "=2": "He" # 2 for male
#     - houses:
#         one: "house"
#         other: "houses"
root:
  user: Account

nav:
  home: Home # nav.home
  user: '{{tr "root.user"}}' # nav.user
  more:
    what: "this" # nav.more.what
    even:
      more: "yes" # nav.more.even.more
      aplural: "You are %[1]d ${minutes} late." # Tr("nav.more.even.aplural", 15)
const (
	female = iota + 1
	male
)

app.Get("/", func(ctx iris.Context) {
	classic := ctx.Tr("Classic")
	oneMin := ctx.Tr("YouLate", 1)
	twoMins := ctx.Tr("YouLate", 2)

	oneDay := ctx.Tr("FreeDay", 1)
	fiveDays := ctx.Tr("FreeDay", 5)

	threeDaysPlusMinutes := ctx.Tr("FreeDay", 3, 15)

	heIsHome := ctx.Tr("HeIsHome", "Peter")
	houseCountFemale := ctx.Tr("HouseCount", female, "Maria", 2)
	houseCountMale := ctx.Tr("HouseCount", male, "Peter", 1)

	yamlSection := ctx.Tr("nav.user") + "\n" + ctx.Tr("nav.more.what") + "\n" + ctx.Tr("nav.more.even.more")
	yamlSectionPlusPlural := ctx.Tr("nav.more.even.aplural", 15)

	ctx.Writef("%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s",
		classic, oneMin, twoMins, oneDay, fiveDays, threeDaysPlusMinutes, heIsHome, houseCountFemale, houseCountMale,
		yamlSection, yamlSectionPlusPlural)
	})
classic
You are 1 minutes late.
You are 2 minutes late.
You have a day off
You have 5 free days
You have three days and 15 minutes off.
Peter is home
She (Maria) has 2 houses
He (Peter) has 1 houses
Account
this
yes
You are 15 minutes late.

@AlbinoGeek
Copy link
Author

Ahh, so categorization of I18N only works in ini currently? Fair.

@kataras
Copy link
Owner

kataras commented Sep 22, 2020

Yes but don't change your code yet, the commit will be pushed today.

kataras added a commit that referenced this issue Sep 29, 2020
@kataras kataras added this to the v12.2.0 milestone Sep 29, 2020
@kataras kataras closed this as completed Sep 29, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants