-
Notifications
You must be signed in to change notification settings - Fork 290
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
Issue with format %s using a width in several languages #203
Comments
This is totally valid, but I don't know how to count characters properly. For this particular purpose, though, I'd suggest changing the coding style e.g. case "pt": // pt-BR portugués
return `Para visualizar este aplicativo, você deve habilitar o Javascript` |
Just in case that helps
|
That looks like C/C++ |
I tried s.length
s.split("").length
[...s].length
(new TextEncoder).encode(s).length with no success. If you know how to do it, I'm open to discuss. |
I don't know how to fix it (yet). I would like to help. I will try a couple of ideas and I'm glad to help to you. I am using your module since years. And it will be an honor if I can help you. The following (C routine ported to Javascript) doesn't work. (while (*s) len += (*s++ & 0xc0) != 0x80;)
The following using a buffer gives info about the real UTF-8 codes. But it returns basically the same of s.length
I just shared my failed tests to avoid you fall in my errors. I'll keep you informed. Best |
That was my problem as well 🙂 |
The problem is not in counting characters, Node is perfectly capable of correctly counting characters natively:
The problem is that fixed-width fonts can't really be fixed-width in some cases. It's a display issue, and I'm afraid it also depends on the font used. Here are, for example, a screenshot from GitHub, my editor, and my terminal, where you can see the Chinese string always has a different width compared to the So, I don't think it is possible to fix this. |
You can use wcwidth which recognizes wide-characters and counts the number of columns they take. |
Alexei, first of all thank you very much for your impressive work. I've been using sprintf-js since years ago.
But I found an issue that I don't know how to fix. Depending of the uff-8 characters used the width modifier in %s is not respecting the columns.
I use sprint-js instead `${expression}` because I can specify the width of a %s
I use UTF-8 characters. I attached a JSON with the string "To view this app you should enable Javascript" translated to 100 languages.
I use the following line with a %-100s width modifier
The spaces are not respected in some of the languages as you can see in the output (the comment on each line is bad adjusted
translate.json.txt
Is there any way to respect spaces properly independently of the language?
How can I solve this?
Thank you very much again.
Carlos
The text was updated successfully, but these errors were encountered: