Skip to content

Fix issue #3,if format not i18n key will return format self - #4

Open
lengpucheng wants to merge 1 commit into
beego:masterfrom
lengpucheng:master
Open

Fix issue #3,if format not i18n key will return format self#4
lengpucheng wants to merge 1 commit into
beego:masterfrom
lengpucheng:master

Conversation

@lengpucheng

Copy link
Copy Markdown

Fix #3 Currently, the Tr() function in this package assumes that any string containing a dot (.) is an i18n key in the format section.key. This causes unexpected behavior when passing normal user-facing strings such as:

i18n.Tr("en-US", "Go 1.20 project")

Instead of returning the original string "Go 1.20 project", it incorrectly splits the string and tries to resolve "1" as a section and "20 project" as a key, ultimately returning "20 project" or an unexpected fallback.

Fix
This PR modifies the Tr() function so that:

  1. If no translation is found using the section.key approach, it gracefully falls back to returning the original input string (format).
  2. It maintains full backward compatibility with existing section.key style lookups.
  3. It preserves support for optional parameters (args) passed to Tr() using fmt.Sprintf.

Example Behavior (After Fix)

i18n.Tr("en-US", "Go 1.20 project") // ➜ returns "Go 1.20 project"
i18n.Tr("en-US", "site.title")      // ➜ returns value from i18n messages if defined

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

Successfully merging this pull request may close these issues.

Tr() incorrectly splits plain strings like "Go 1.20 project" into "Go 1" and "20 project"

1 participant