Skip to content
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

Use isolating flag #14

Merged
merged 3 commits into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ 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

- Raise TypeError if variable key is not a string.
Expand Down
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 4 additions & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ 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]
# 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 = [
Expand Down Expand Up @@ -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<major>\\d+)\\.(?P<minor>\\d+)\\.(?P<patch>\\d+)"
serialize = ["{major}.{minor}.{patch}"]
search = "{current_version}"
Expand Down
8 changes: 6 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> {
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))),
Expand Down Expand Up @@ -120,6 +123,7 @@ impl Bundle {
}
}
}

let value = self
.bundle
.format_pattern(pattern, Some(&args), &mut errors);
Expand Down
5 changes: 4 additions & 1 deletion src/rustfluent.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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: ...
14 changes: 13 additions & 1 deletion tests/test_python_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,26 @@ 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"})
== f"Hello, {BIDI_OPEN}Bob{BIDI_CLOSE}"
)


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",
(
Expand Down