Skip to content

Commit c6cbbc3

Browse files
committed
Change API so variables are passed in as an argument
Although this is slightly more verbose, it gives us better opportunities for backward-compatibility if we choose to add new options to `get_translation` later. The name 'variables' is taken from the official Fluent docs https://projectfluent.org/fluent/guide/variables.html
1 parent 5b0a71c commit c6cbbc3

File tree

4 files changed

+20
-12
lines changed

4 files changed

+20
-12
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ bundle = rustfluent.Bundle(
4040
# Fetch a translation
4141
assert bundle.get_translation("hello-world") == "Hello World"
4242

43-
# Fetch a translation that takes a keyword argument
44-
assert bundle.get_translation("hello-user", user="Bob") == "Hello, \u2068Bob\u2069"
43+
# Fetch a translation that includes variables
44+
assert bundle.get_translation("hello-user", variables={"user": "Bob"}) == "Hello, \u2068Bob\u2069"
4545
```
4646

4747
The Unicode characters around "Bob" in the above example are for

src/lib.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,11 @@ impl Bundle {
4747
Ok(Self { bundle })
4848
}
4949

50-
#[pyo3(signature = (identifier, **kwargs))]
50+
#[pyo3(signature = (identifier, variables=None))]
5151
pub fn get_translation(
5252
&self,
5353
identifier: &str,
54-
kwargs: Option<&Bound<'_, PyDict>>,
54+
variables: Option<&Bound<'_, PyDict>>,
5555
) -> PyResult<String> {
5656
let msg = match self.bundle.get_message(identifier) {
5757
Some(m) => m,
@@ -71,9 +71,9 @@ impl Bundle {
7171

7272
let mut args = FluentArgs::new();
7373

74-
if let Some(kwargs) = kwargs {
75-
for kwarg in kwargs {
76-
args.set(kwarg.0.to_string(), kwarg.1.to_string());
74+
if let Some(variables) = variables {
75+
for variable in variables {
76+
args.set(variable.0.to_string(), variable.1.to_string());
7777
}
7878
}
7979

src/rustfluent.pyi

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
class Bundle:
22
def __init__(self, language: str, ftl_filenames: list[str]) -> None: ...
3-
def get_translation(self, identifier: str, **kwargs: str) -> str: ...
3+
def get_translation(self, identifier: str, variables: dict[str, str] | None = None) -> str: ...

tests/test_python_interface.py

+12-4
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ def test_en_basic_with_named_arguments():
2525

2626
def test_en_with_args():
2727
bundle = fluent.Bundle("en", [str(data_dir / "en.ftl")])
28-
assert bundle.get_translation("hello-user", user="Bob") == "Hello, \u2068Bob\u2069"
28+
assert (
29+
bundle.get_translation("hello-user", variables={"user": "Bob"}) == "Hello, \u2068Bob\u2069"
30+
)
2931

3032

3133
def test_fr_basic():
@@ -35,7 +37,10 @@ def test_fr_basic():
3537

3638
def test_fr_with_args():
3739
bundle = fluent.Bundle("fr", [str(data_dir / "fr.ftl")])
38-
assert bundle.get_translation("hello-user", user="Bob") == "Bonjour, \u2068Bob\u2069!"
40+
assert (
41+
bundle.get_translation("hello-user", variables={"user": "Bob"})
42+
== "Bonjour, \u2068Bob\u2069!"
43+
)
3944

4045

4146
def test_new_overwrites_old():
@@ -44,13 +49,16 @@ def test_new_overwrites_old():
4449
[str(data_dir / "fr.ftl"), str(data_dir / "en_hello.ftl")],
4550
)
4651
assert bundle.get_translation("hello-world") == "Hello World"
47-
assert bundle.get_translation("hello-user", user="Bob") == "Bonjour, \u2068Bob\u2069!"
52+
assert (
53+
bundle.get_translation("hello-user", variables={"user": "Bob"})
54+
== "Bonjour, \u2068Bob\u2069!"
55+
)
4856

4957

5058
def test_id_not_found():
5159
bundle = fluent.Bundle("fr", [str(data_dir / "fr.ftl")])
5260
with pytest.raises(ValueError):
53-
bundle.get_translation("missing", user="Bob")
61+
bundle.get_translation("missing", variables={"user": "Bob"})
5462

5563

5664
def test_file_not_found():

0 commit comments

Comments
 (0)