chai2010 / gettext-go Goto Github PK
View Code? Open in Web Editor NEW:ab: GNU gettext for Go (Imported By Kubernetes)
Home Page: http://pkg.go.dev/github.com/chai2010/gettext-go
License: BSD 3-Clause "New" or "Revised" License
:ab: GNU gettext for Go (Imported By Kubernetes)
Home Page: http://pkg.go.dev/github.com/chai2010/gettext-go
License: BSD 3-Clause "New" or "Revised" License
Instasll should be Install
If the localization is provided for a fr
and there is none for fr_FR
, then the fr
files should be loaded when the language is set to fr_FR
. This would surely be better than no localization at all.
In most programs the default language is English. Should this be reflected in the formula of the nilTranslator
instead of the no-plural one?
If that is not desirable, because changing a default may have an impact on other programs, then adding an API to allow changing it would be nice.
And this in my dialogue.po
file
msgid "Choose the day of the week ;)"
msgstr "Оберіть день тижня ;)"
When I am calling the foo.DGettext("dialogue", "Choose the day of the week ;)")
english version is returned.
The normal translations are working properly, but when I try to load from other domain it just returns me what I typed in.
考虑添加JSON格式支持吗?前端用的。参考:https://github.com/php-gettext/Json
在代码中:
gettext.Gettext("Hello world!").
po文件中:
#: main.go:19
msgid "Hello world!"
msgstr "你好世界!"
这种情况将无法得到翻译字符串。
看了代码,在解析mo文件并将翻译保存到dict中时,使用了msgctxt+'\004'+msgid做为key。
在有msgctxt的情况下工作很正常,如PGettext("msgctxt", "msgid")。
在无msgctxt的情况下,Gettext("msgid")无法得到翻译字符串。在dict中实际的key是msgid的内容。而Gettext函数到dict中查找翻译字符串时,以所在函数名做为msgctxt,最后形成<func name>+'\004'+msgid做为key来查找,结果无法找到翻译字符串。
如果Gettext()函数能生成正确的key,问题就能解决,修改方法如下:
diff --git a/gettext/gettext.go b/gettext/gettext.go
index ca14065..2171c74 100644
--- a/gettext/gettext.go
+++ b/gettext/gettext.go
@@ -80,7 +80,7 @@ func Textdomain(domain string) string {
// msg := gettext.Gettext("Hello") // msgctxt is "some/package/name.Foo"
// }
func Gettext(msgid string) string {
- return PGettext(callerName(2), msgid)
+ return PGettext("", msgid)
}
// Getdata attempt to translate a resource file into the user's native language,
FileSystem.LoadMessagesMap(domain, lang string) (map[string][]string, error)
/map[domain][lang][msgid][]string
/gettext/mapfs.NewXXX()
/jsonString
/map[string]interface{}
/LoadJsonFile(domain, lang) ([]byte, error)
LoadMessagesFile("hello", "zh_CN", "json")
, Json(s) FileSystemSetLanguage BindLocale 都是用的全局defaultGettexter 并发调SetLanguage会错乱
One scenario is for serving web requests with different languages. It seems domainManager is not exported and only defaultDomainManager can be used. It would be nice to export domainManager or have some way to allow different clients/threads to have different domainManger with its own locale.
The test suite fails for me on both Go 1.5.3 and 1.6.0:
$ go test
--- FAIL: TestCallerName (0.00s)
caller_test.go:39: expect = github.com/chai2010/gettext-go/gettext.init, got = github.com/chai2010/gettext-go/gettext.init.1
FAIL
exit status 1
FAIL github.com/chai2010/gettext-go/gettext 0.226s
https://github.com/chai2010/gettext-go/blob/master/po/message.go#L134
In the case where msgstr
contains square brackets, the parsed index
value would always be 0.
Example:
msgctxt "NAME OF BUTTON MERCHANTS CAN CLICK TO DOWNLOAD ALL THEIR PRODUCTS AS AN CSV "
"FILE. INCLUDED IS THE NAME OF THE WAREHOUSE AND THE NUMBER OF ROWS THAT WILL "
"BE IN THE FILE."
msgid "Download CSV [{%2=warehouse name}] ({%1=number of products} row)"
msgid_plural "Download CSV [{%2=warehouse name}] ({%1=number of products} rows)"
msgstr [0] "Baix[1]ar CSV [{%2=warehouse name}] ({%1=number of products} linha)"
msgstr [1] "Baix[2]ar CSV [{%2=warehouse name}] ({%1=number of products} linhas)"
The expected length of Message.MsgStrPlural
should be 2 but the actual result is 1 because the message contains square brackets.
xgettext-go 通过 go install github.com/chai2010/gettext-go/cmd/xgettext-go@latest
安装,环境 go 1.18.1 Windows 10
最小重现案例
package terminal
import "github.com/chai2010/gettext-go"
func f() {
println(gettext.Gettext(`Hello world`))
}
output.pot 内容
#: github.com/nnnewb/qn/internal/terminal/t.go:6
#, go-format
msgid "`Hello world`"
msgstr ""
问题怀疑是在 cmd/xgettext-go/pkg.go 这里,没考虑到反引号包裹的字符串的情况。
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.