Skip to content

Commit 9cfff86

Browse files
committed
Created fluent.bundle as namespace package
1 parent b784927 commit 9cfff86

31 files changed

Lines changed: 158 additions & 97 deletions

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@
22
*.pyc
33
.eggs/
44
fluent.egg-info/
5+
fluent_syntax.egg-info/
6+
fluent_bundle.egg-info/

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ python:
99
- "nightly"
1010
env:
1111
- PACKAGE=fluent-syntax
12+
- PACKAGE=fluent-bundle
1213
install: pip install tox-travis
1314
script: cd $PACKAGE; tox
1415
notifications:

README.md

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -39,20 +39,20 @@ Usage
3939
-----
4040

4141
To generate translations from this Python libary, you start with the
42-
`MessageContext` class:
42+
`FluentBundle` class:
4343

44-
>>> from fluent.context import MessageContext
44+
>>> from fluent.bundle import FluentBundle
4545

4646
You pass a list of locales to the constructor - the first being the desired
4747
locale, with fallbacks after that:
4848

49-
>>> context = MessageContext(["en-US"])
49+
>>> bundle = FluentBundle(["en-US"])
5050

5151

5252
You must then add messages. These would normally come from a `.ftl` file stored
5353
on disk, here we will just add them directly:
5454

55-
>>> context.add_messages("""
55+
>>> bundle.add_messages("""
5656
... welcome = Welcome to this great app!
5757
... greet-by-name = Hello, { $name }!
5858
... """)
@@ -64,17 +64,17 @@ implementation tries hard to recover from any formatting errors and generate the
6464
most human readable representation of the value. The `format` method therefore
6565
returns a tuple containing `(translated string, errors)`, as below.
6666

67-
>>> translated, errs = context.format('welcome')
67+
>>> translated, errs = bundle.format('welcome')
6868
>>> translated
6969
"Welcome to this great app!"
7070
>>> errs
7171
[]
7272

73-
>>> translated, errs = context.format('greet-by-name', {'name': 'Jane'})
73+
>>> translated, errs = bundle.format('greet-by-name', {'name': 'Jane'})
7474
>>> translated
7575
'Hello, \u2068Jane\u2069!'
7676

77-
>>> translated, errs = context.format('greet-by-name', {})
77+
>>> translated, errs = bundle.format('greet-by-name', {})
7878
>>> translated
7979
'Hello, \u2068name\u2069!'
8080
>>> errs
@@ -85,7 +85,7 @@ are Unicode bidi isolation characters that help to ensure that the interpolated
8585
strings are handled correctly in the situation where the text direction of the
8686
substitution might not match the text direction of the localized text. These
8787
characters can be disabled if you are sure that is not possible for your app by
88-
passing `use_isolating=False` to the `MessageContext` constructor.
88+
passing `use_isolating=False` to the `FluentBundle` constructor.
8989

9090
Python 2
9191
--------
@@ -105,23 +105,23 @@ Numbers
105105
When rendering translations, Fluent passes any numeric arguments (int or float)
106106
through locale-aware formatting functions:
107107

108-
>>> context.add_messages("show-total-points = You have { $points } points.")
109-
>>> val, errs = context.format("show-total-points", {'points': 1234567})
108+
>>> bundle.add_messages("show-total-points = You have { $points } points.")
109+
>>> val, errs = bundle.format("show-total-points", {'points': 1234567})
110110
>>> val
111111
'You have 1,234,567 points.'
112112

113113

114114
You can specify your own formatting options on the arguments passed in by
115115
wrapping your numeric arguments with `fluent.types.fluent_number`:
116116

117-
>>> from fluent.types import fluent_number
117+
>>> from fluent.bundle.types import fluent_number
118118
>>> points = fluent_number(1234567, useGrouping=False)
119-
>>> context.format("show-total-points", {'points': points})[0]
119+
>>> bundle.format("show-total-points", {'points': points})[0]
120120
'You have 1234567 points.'
121121

122122
>>> amount = fluent_number(1234.56, style="currency", currency="USD")
123-
>>> context.add_messages("your-balance = Your balance is { $amount }")
124-
>>> context.format("your-balance", {'amount': amount})[0]
123+
>>> bundle.add_messages("your-balance = Your balance is { $amount }")
124+
>>> bundle.format("your-balance", {'amount': amount})[0]
125125
'Your balance is $1,234.56'
126126

127127
Thee options available are defined in the Fluent spec for
@@ -136,14 +136,14 @@ Python `dateime.datetime` and `datetime.date` objects are also passed through
136136
locale aware functions:
137137

138138
>>> from datetime import date
139-
>>> context.add_messages("today-is = Today is { $today }")
140-
>>> val, errs = context.format("today-is", {"today": date.today() })
139+
>>> bundle.add_messages("today-is = Today is { $today }")
140+
>>> val, errs = bundle.format("today-is", {"today": date.today() })
141141
>>> val
142142
'Today is Jun 16, 2018'
143143

144144
You can explicitly call the `DATETIME` builtin to specify options:
145145

146-
>>> context.add_messages('today-is = Today is { DATETIME($today, dateStyle: "short") }')
146+
>>> bundle.add_messages('today-is = Today is { DATETIME($today, dateStyle: "short") }')
147147

148148
See the [DATETIME
149149
docs](https://projectfluent.org/fluent/guide/functions.html#datetime). However,
@@ -153,12 +153,12 @@ currently the only supported options to `DATETIME` are:
153153
* `dateStyle` and `timeStyle` which are [proposed
154154
additions](https://github.com/tc39/proposal-ecma402-datetime-style) to the ECMA i18n spec.
155155

156-
To specify options from Python code, use `fluent.types.fluent_date`:
156+
To specify options from Python code, use `fluent.bundle.types.fluent_date`:
157157

158-
>>> from fluent.types import fluent_date
158+
>>> from fluent.bundle.types import fluent_date
159159
>>> today = date.today()
160160
>>> short_today = fluent_date(today, dateStyle='short')
161-
>>> val, errs = context.format("today-is", {"today": short_today })
161+
>>> val, errs = bundle.format("today-is", {"today": short_today })
162162
>>> val
163163
'Today is 6/17/18'
164164

@@ -180,8 +180,8 @@ You can also specify timezone for displaying `datetime` objects in two ways:
180180
>>> utcnow
181181
datetime.datetime(2018, 6, 17, 12, 15, 5, 677597)
182182

183-
>>> context.add_messages("now-is = Now is { $now }")
184-
>>> val, errs = context.format("now-is",
183+
>>> bundle.add_messages("now-is = Now is { $now }")
184+
>>> val, errs = bundle.format("now-is",
185185
... {"now": fluent_date(utcnow,
186186
... timeZone="Europe/Moscow",
187187
... dateStyle="medium",
@@ -194,7 +194,7 @@ Custom functions
194194
----------------
195195

196196
You can add functions to the ones available to FTL authors by passing
197-
a `functions` dictionary to the `MessageContext` constructor:
197+
a `functions` dictionary to the `FluentBundle` constructor:
198198

199199

200200
>>> import platform
@@ -204,24 +204,24 @@ a `functions` dictionary to the `MessageContext` constructor:
204204
... 'Darwin': 'mac',
205205
... 'Windows': 'windows'}.get(platform.system(), 'other')
206206

207-
>>> context = MessageContext(['en-US'], functions={'OS': os_name})
208-
>>> context.add_messages("""
207+
>>> bundle = FluentBundle(['en-US'], functions={'OS': os_name})
208+
>>> bundle.add_messages("""
209209
... welcome = { OS() ->
210210
... [linux] Welcome to Linux
211211
... [mac] Welcome to Mac
212212
... [windows] Welcome to Windows
213213
... *[other] Welcome
214214
... }
215215
... """)
216-
>>> print(context.format('welcome')[0]
216+
>>> print(bundle.format('welcome')[0]
217217
Welcome to Linux
218218

219219
These functions can accept positioal and keyword arguments (like the `NUMBER`
220220
and `DATETIME` builtins), and in this case must accept the following types of
221221
arguments:
222222

223223
* unicode strings (i.e. `unicode` on Python 2, `str` on Python 3)
224-
* `fluent.types.FluentType` subclasses, namely:
224+
* `fluent.bundle.types.FluentType` subclasses, namely:
225225
* `FluentNumber` - `int`, `float` or `Decimal` objects passed in externally,
226226
or expressed as literals, are wrapped in these. Note that these objects also
227227
subclass builtin `int`, `float` or `Decimal`, so can be used as numbers in

fluent-bundle/fluent/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
__path__ = __import__('pkgutil').extend_path(__path__, __name__)
Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,20 @@
44
import babel.numbers
55
import babel.plural
66

7+
from fluent.syntax import FluentParser
8+
from fluent.syntax.ast import Message, Term
9+
710
from .builtins import BUILTINS
811
from .resolver import resolve
9-
from .syntax import FluentParser
10-
from .syntax.ast import Message, Term
1112

1213

13-
class MessageContext(object):
14+
class FluentBundle(object):
1415
"""
1516
Message contexts are single-language stores of translations. They are
1617
responsible for parsing translation resources in the Fluent syntax and can
1718
format translation units (entities) to strings.
1819
19-
Always use `MessageContext.format` to retrieve translation units from
20+
Always use `FluentBundle.format` to retrieve translation units from
2021
a context. Translations can contain references to other entities or
2122
external arguments, conditional logic in form of select expressions, traits
2223
which describe their grammatical features, and can use Fluent builtins.
Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,13 @@
66
import attr
77
import six
88

9+
from fluent.syntax.ast import (AttributeExpression, CallExpression, Message,
10+
MessageReference, NumberLiteral, Pattern,
11+
Placeable, SelectExpression, StringLiteral, Term,
12+
TermReference, TextElement, VariableReference,
13+
VariantExpression, VariantList, Identifier)
14+
915
from .errors import FluentCyclicReferenceError, FluentReferenceError
10-
from .syntax.ast import (AttributeExpression, CallExpression, Message, MessageReference, NumberLiteral,
11-
Pattern, Placeable, SelectExpression, StringLiteral, Term, TermReference, TextElement,
12-
VariableReference, VariantExpression, VariantList, Identifier)
1316
from .types import FluentDateType, FluentNone, FluentNumber, fluent_date, fluent_number
1417
from .utils import numeric_to_native
1518

@@ -45,7 +48,7 @@ class ResolverEnvironment(object):
4548

4649
def resolve(context, message, args, errors=None):
4750
"""
48-
Given a MessageContext, a Message instance and some arguments,
51+
Given a FluentBundle, a Message instance and some arguments,
4952
resolve the message to a string.
5053
5154
This is the normal entry point for this module.

0 commit comments

Comments
 (0)