From 602dd5eb2334403426067a7f1382066cf7f79ddc Mon Sep 17 00:00:00 2001 From: David Seddon Date: Thu, 26 Sep 2024 12:21:44 +0100 Subject: [PATCH 1/3] Add use_isolating flag --- CHANGELOG.md | 2 ++ README.md | 11 +++++++---- src/lib.rs | 8 ++++++-- src/rustfluent.pyi | 5 ++++- tests/test_python_interface.py | 14 +++++++++++++- 5 files changed, 32 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 81b889f..8e8bf27 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,8 @@ adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## [Unreleased] +- Add use_isolating flag. + ## [0.1.0a4] - 2024-09-25 - Raise TypeError if variable key is not a string. diff --git a/README.md b/README.md index 6ffa29e..afb06b3 100644 --- a/README.md +++ b/README.md @@ -84,15 +84,18 @@ bundle = rustfluent.Bundle( >>> bundle.get_translation(identifier="hello-world") "Hello, world!" >>> bundle.get_translation(identifier="hello-user", variables={"user": "Bob"}) +"Hello, \u2068Bob\u2069!" +>>> bundle.get_translation(identifier="hello-user", variables={"user": "Bob"}, use_isolating=False) "Hello, Bob!" ``` #### Parameters -| Name | Type | Description | -|--------------|------------------------------|----------------------------------------| -| `identifier` | `str` | The identifier for the Fluent message. | -| `variables` | `dict[str, str | int ]`, optional | Any [variables](https://projectfluent.org/fluent/guide/variables.html) to be passed to the Fluent message. | +| Name | Type | Description | +|----------------------|------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------| +| `identifier` | `str` | The identifier for the Fluent message. | +| `variables` | `dict[str, str | int ]`, optional | Any [variables](https://projectfluent.org/fluent/guide/variables.html) to be passed to the Fluent message. | +| `use_isolating` | `bool`, optional | Whether to insert Unicode Directionality Isolation Marks around placeables, to indicate that their direction may differ from the surrounding message. Defaults to `True`. | #### Return value diff --git a/src/lib.rs b/src/lib.rs index 41067f1..f124a8f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -59,12 +59,15 @@ impl Bundle { Ok(Self { bundle }) } - #[pyo3(signature = (identifier, variables=None))] + #[pyo3(signature = (identifier, variables=None, use_isolating=true))] pub fn get_translation( - &self, + &mut self, identifier: &str, variables: Option<&Bound<'_, PyDict>>, + use_isolating: bool, ) -> PyResult { + self.bundle.set_use_isolating(use_isolating); + let msg = match self.bundle.get_message(identifier) { Some(m) => m, None => return Err(PyValueError::new_err(format!("{} not found", identifier))), @@ -120,6 +123,7 @@ impl Bundle { } } } + let value = self .bundle .format_pattern(pattern, Some(&args), &mut errors); diff --git a/src/rustfluent.pyi b/src/rustfluent.pyi index 7f835c1..50a6d4a 100644 --- a/src/rustfluent.pyi +++ b/src/rustfluent.pyi @@ -3,5 +3,8 @@ Variable = str | int class Bundle: def __init__(self, language: str, ftl_filenames: list[str], strict: bool = False) -> None: ... def get_translation( - self, identifier: str, variables: dict[str, Variable] | None = None + self, + identifier: str, + variables: dict[str, Variable] | None = None, + use_isolating: bool = True, ) -> str: ... diff --git a/tests/test_python_interface.py b/tests/test_python_interface.py index baaedd7..eeee44c 100644 --- a/tests/test_python_interface.py +++ b/tests/test_python_interface.py @@ -27,7 +27,7 @@ def test_en_basic_with_named_arguments(): assert bundle.get_translation("hello-world") == "Hello World" -def test_en_with_args(): +def test_en_with_variables(): bundle = fluent.Bundle("en", [str(data_dir / "en.ftl")]) assert ( bundle.get_translation("hello-user", variables={"user": "Bob"}) @@ -35,6 +35,18 @@ def test_en_with_args(): ) +def test_en_with_variables_use_isolating_off(): + bundle = fluent.Bundle("en", [str(data_dir / "en.ftl")]) + assert ( + bundle.get_translation( + "hello-user", + variables={"user": "Bob"}, + use_isolating=False, + ) + == "Hello, Bob" + ) + + @pytest.mark.parametrize( "description, identifier, variables, expected", ( From 34ca43d4c0203fa48d1dae13d674f750c6b96baf Mon Sep 17 00:00:00 2001 From: David Seddon Date: Thu, 26 Sep 2024 12:23:03 +0100 Subject: [PATCH 2/3] Correct broken links in pyproject.toml --- pyproject.toml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index c262432..80b0a03 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -27,8 +27,8 @@ dependencies = [] [project.urls] # See https://daniel.feldroy.com/posts/2023-08-pypi-project-urls-cheatsheet for # additional URLs that can be included here. -repository = "https://github.com/octoenergy/python-rustfluent" -changelog = "https://github.com/octoenergy/python-rustfluent/blob/main/CHANGELOG.md" +repository = "https://github.com/kraken-tech/python-rustfluent/" +changelog = "https://github.com/kraken-tech/python-rustfluent/blob/main/CHANGELOG.md" [project.optional-dependencies] dev = [ From 9a08f9d2f950c2fc9bd33a8713673ef52e8e5c6f Mon Sep 17 00:00:00 2001 From: David Seddon Date: Thu, 26 Sep 2024 12:23:33 +0100 Subject: [PATCH 3/3] =?UTF-8?q?Bump=20version:=200.1.0a4=20=E2=86=92=200.1?= =?UTF-8?q?.0a5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 2 ++ pyproject.toml | 4 ++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8e8bf27..2f5a078 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,8 @@ adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## [Unreleased] +## [0.1.0a5] - 2024-09-26 + - Add use_isolating flag. ## [0.1.0a4] - 2024-09-25 diff --git a/pyproject.toml b/pyproject.toml index 80b0a03..defa9e2 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -21,7 +21,7 @@ classifiers = [ ] # Do not manually edit the version, use `make version_{type}` instead. # This should match the version in the [tool.bumpversion] section. -version = "0.1.0a4" +version = "0.1.0a5" dependencies = [] [project.urls] @@ -133,7 +133,7 @@ filterwarnings = "error" [tool.bumpversion] # Do not manually edit the version, use `make version_{type}` instead. # This should match the version in the [project] section. -current_version = "0.1.0a4" +current_version = "0.1.0a5" parse = "(?P\\d+)\\.(?P\\d+)\\.(?P\\d+)" serialize = ["{major}.{minor}.{patch}"] search = "{current_version}"