Giter VIP home page Giter VIP logo

gettext-go's Issues

Default plural formula should probably be the one for English

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.

Is DGettext working properly?

I have this file structure
image

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.

po文件中的词条如果没有msgctxt, 无法被找到。

在代码中:

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,

v1.0计划

  • 目前的API还是从 google code 网址时代继承来的, 需要重新设计简化API和包路径
  • gettext是上个世纪的产物, 全局只有一个选择, 改进支持多局部多语言共存
  • 目前的context有缺陷, 内部闭包的路径解析不够稳定, 需要重新设计
  • 命令行工具, 可以自动从代码提取要翻译的文字, 同时对有问题的用法提供告警(比如传入的是变量字符串)
  • v0.1到v1.0的API变化, 尽量保证gofmt命令行替换可以完成升级
  • domain绑定到对象, 不同的pkg可以选择不同的domain, context是domain内部的区别, caller不再自动填充
  • 自动生成多国文本(调用翻译服务的API/插件模式)
  • 增加map类型的翻译文件, 可以作为po文件替代
  • 支持单个po/mo文件,和map类型,对于一个具体的翻译 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)
  • json: LoadMessagesFile("hello", "zh_CN", "json"), Json(s) FileSystem
  • 支持walk/playground等格式的json翻译
  • 其它

https://pkg.go.dev/github.com/chai2010/gettext-go

Need multiple languages support for multiple threads

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.

Test failure

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

msgplural index parsing logic is wrong

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会提取出多余的反引号

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 这里,没考虑到反引号包裹的字符串的情况。

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.