Skip to content

"Plural support" example in README.md fails #14

@smcl

Description

@smcl

Using Python 3.12.1 and messageformat2==0.1.1 the final example on the README.md fails with an exception. I've taken the example and wrapped the msg.format(...) calls in a print but you can see hopefully see what's going on:

$ pip freeze | grep message
messageformat2==0.1.1
$ python --version
Python 3.12.1
$ cat app.py
from messageformat2 import Message

msg = Message("""\
.match {$count :number}
one {{You have {$count} notification.}}
*   {{You have {$count} notifications.}}
""")

print(msg.format({"count": 42}, locale='en_US')) # -> "You have 42 notifications."
print(msg.format({"count": 1}, locale='en_US')) # -> "You have 1 notification."
$ python app.py
Traceback (most recent call last):
  File "C:\Users\Sean\source\scratch\mf-test\app.py", line 3, in <module>
    msg = Message("""\
          ^^^^^^^^^^^^
  File "C:\Users\Sean\source\scratch\mf-test\venv\Lib\site-packages\messageformat2\message.py", line 67, in __init__
    self._ast = parse(msg)
                ^^^^^^^^^^
  File "C:\Users\Sean\source\scratch\mf-test\venv\Lib\site-packages\messageformat2\parser.py", line 141, in parse
    ast = parse_message(msg)
          ^^^^^^^^^^^^^^^^^^
  File "C:\Users\Sean\source\scratch\mf-test\venv\Lib\site-packages\messageformat2\parser.py", line 148, in parse_message
    message = parse_complex_message(queue) if queue.peek() == "." else parse_simple_message(queue)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Sean\source\scratch\mf-test\venv\Lib\site-packages\messageformat2\parser.py", line 180, in parse_complex_message
    body = parse_complex_body(queue)
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Sean\source\scratch\mf-test\venv\Lib\site-packages\messageformat2\parser.py", line 218, in parse_complex_body
    return parse_matcher(queue)
           ^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Sean\source\scratch\mf-test\venv\Lib\site-packages\messageformat2\parser.py", line 239, in parse_matcher
    while queue.peek_after_whitespace():
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Sean\source\scratch\mf-test\venv\Lib\site-packages\messageformat2\parser.py", line 105, in peek_after_whitespace
    return self.queue[i]
           ~~~~~~~~~~^^^
IndexError: string index out of range

Either removing the newline from the end of the multi-line """ format string or terminating it's second-last line (the "*" case) with a \ character causes this the example to successfully print the expected result:

$ cat app.py
from messageformat2 import Message

msg = Message("""\
.match {$count :number}
one {{You have {$count} notification.}}
*   {{You have {$count} notifications.}}""")

print(msg.format({"count": 42}, locale='en_US')) # -> "You have 42 notifications."
print(msg.format({"count": 1}, locale='en_US')) # -> "You have 1 notification."
$ python app.py
You have 42 notifications.
You have 1 notification.

I'm still getting started with MessageFormat 2.0 so I don't know if this is expected (and therefore the example should be adjusted) or if this requires a fix, which is why I haven't submitted a PR.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions