Skip to content
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [Unreleased]

- Annotate `ftl` file source code when reporting parse errors to allow ergonomic debugging.
- Support passing `pathlib.Path` in `ftl_filenames` (in addition to `str`) when creating a `Bundle`. This allows using non-unicode paths on unix systems.

## [0.1.0a7] - 2025-01-29

Expand Down
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,15 @@ The Unicode characters around "Bob" in the above example are for
A set of translations for a specific language.

```python
import pathlib

import rustfluent

bundle = rustfluent.Bundle(
language="en-US",
ftl_files=[
"/path/to/messages.ftl",
"/path/to/more/messages.ftl",
pathlib.Path("/path/to/more/messages.ftl"),
],
)
```
Expand All @@ -71,7 +73,7 @@ bundle = rustfluent.Bundle(
| Name | Type | Description |
|-------------|------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `language` | `str` | [Unicode Language Identifier](https://unicode.org/reports/tr35/tr35.html#Unicode_language_identifier) for the language. |
| `ftl_files` | `list[str]` | Full paths to the FTL files containing the translations. Entries in later files overwrite earlier ones. |
| `ftl_files` | `list[str | pathlib.Path]` | Full paths to the FTL files containing the translations. Entries in later files overwrite earlier ones. |
| `strict` | `bool`, optional | In strict mode, a `ParserError` will be raised if there are any errors in the file. In non-strict mode, invalid Fluent messages will be excluded from the Bundle. |

#### Raises
Expand Down
22 changes: 11 additions & 11 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ use fluent_bundle::concurrent::FluentBundle;
use miette::{LabeledSpan, miette};
use pyo3::exceptions::{PyFileNotFoundError, PyTypeError, PyValueError};
use pyo3::prelude::*;
use pyo3::types::{PyDate, PyDict, PyInt, PyList, PyString};
use pyo3::types::{PyDate, PyDict, PyInt, PyString};
use std::fs;
use std::path::PathBuf;
use unic_langid::LanguageIdentifier;

use pyo3::create_exception;
Expand All @@ -29,11 +30,7 @@ mod rustfluent {
impl Bundle {
#[new]
#[pyo3(signature = (language, ftl_filenames, strict=false))]
fn new(
language: &str,
ftl_filenames: &'_ Bound<'_, PyList>,
strict: bool,
) -> PyResult<Self> {
fn new(language: &str, ftl_filenames: Vec<PathBuf>, strict: bool) -> PyResult<Self> {
let langid: LanguageIdentifier = match language.parse() {
Ok(langid) => langid,
Err(_) => {
Expand All @@ -45,9 +42,8 @@ mod rustfluent {
let mut bundle = FluentBundle::new_concurrent(vec![langid]);

for file_path in ftl_filenames.iter() {
let path_string = file_path.to_string();
let contents = fs::read_to_string(path_string)
.map_err(|_| PyFileNotFoundError::new_err(file_path.to_string()))?;
let contents = fs::read_to_string(file_path)
.map_err(|_| PyFileNotFoundError::new_err(file_path.clone()))?;

let resource = match FluentResource::try_new(contents) {
Ok(resource) => resource,
Expand All @@ -56,8 +52,12 @@ mod rustfluent {
for error in errors {
labels.push(LabeledSpan::at(error.pos, format!("{}", error.kind)))
}
let error = miette!(labels = labels, "Error when parsing {file_path}",)
.with_source_code(resource.source().to_string());
let error = miette!(
labels = labels,
"Error when parsing {}",
file_path.to_string_lossy()
)
.with_source_code(resource.source().to_string());
return Err(ParserError::new_err(format!("{error:?}")));
}
Err((resource, _errors)) => resource,
Expand Down
5 changes: 4 additions & 1 deletion src/rustfluent.pyi
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
from datetime import date
from pathlib import Path

Variable = str | int | date

class Bundle:
def __init__(self, language: str, ftl_filenames: list[str], strict: bool = False) -> None: ...
def __init__(
self, language: str, ftl_filenames: list[str | Path], strict: bool = False
) -> None: ...
def get_translation(
self,
identifier: str,
Expand Down
33 changes: 19 additions & 14 deletions tests/test_python_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,33 @@


def test_en_basic():
bundle = fluent.Bundle("en", [data_dir / "en.ftl"])
assert bundle.get_translation("hello-world") == "Hello World"


def test_en_basic_str_path():
bundle = fluent.Bundle("en", [str(data_dir / "en.ftl")])
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we have a test that checks that strings are still supported?

assert bundle.get_translation("hello-world") == "Hello World"


def test_en_basic_with_named_arguments():
bundle = fluent.Bundle(
language="en",
ftl_filenames=[str(data_dir / "en.ftl")],
ftl_filenames=[data_dir / "en.ftl"],
)
assert bundle.get_translation("hello-world") == "Hello World"


def test_en_with_variables():
bundle = fluent.Bundle("en", [str(data_dir / "en.ftl")])
bundle = fluent.Bundle("en", [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")])
bundle = fluent.Bundle("en", [data_dir / "en.ftl"])
assert (
bundle.get_translation(
"hello-user",
Expand All @@ -61,7 +66,7 @@ def test_en_with_variables_use_isolating_off():
),
)
def test_variables_of_different_types(description, identifier, variables, expected):
bundle = fluent.Bundle("en", [str(data_dir / "en.ftl")])
bundle = fluent.Bundle("en", [data_dir / "en.ftl"])

result = bundle.get_translation(identifier, variables=variables)

Expand All @@ -84,7 +89,7 @@ def test_invalid_language():
),
)
def test_invalid_variable_keys_raise_type_error(key):
bundle = fluent.Bundle("en", [str(data_dir / "en.ftl")])
bundle = fluent.Bundle("en", [data_dir / "en.ftl"])

with pytest.raises(TypeError, match="Variable key not a str, got"):
bundle.get_translation("hello-user", variables={key: "Bob"})
Expand All @@ -99,20 +104,20 @@ def test_invalid_variable_keys_raise_type_error(key):
),
)
def test_invalid_variable_values_use_key_instead(value):
bundle = fluent.Bundle("en", [str(data_dir / "en.ftl")])
bundle = fluent.Bundle("en", [data_dir / "en.ftl"])

result = bundle.get_translation("hello-user", variables={"user": value})

assert result == f"Hello, {BIDI_OPEN}user{BIDI_CLOSE}"


def test_fr_basic():
bundle = fluent.Bundle("fr", [str(data_dir / "fr.ftl")])
bundle = fluent.Bundle("fr", [data_dir / "fr.ftl"])
assert bundle.get_translation("hello-world") == "Bonjour le monde!"


def test_fr_with_args():
bundle = fluent.Bundle("fr", [str(data_dir / "fr.ftl")])
bundle = fluent.Bundle("fr", [data_dir / "fr.ftl"])
assert (
bundle.get_translation("hello-user", variables={"user": "Bob"})
== f"Bonjour, {BIDI_OPEN}Bob{BIDI_CLOSE}!"
Expand All @@ -130,7 +135,7 @@ def test_fr_with_args():
),
)
def test_selector(number, expected):
bundle = fluent.Bundle("en", [str(data_dir / "en.ftl")])
bundle = fluent.Bundle("en", [data_dir / "en.ftl"])

result = bundle.get_translation("with-selector", variables={"number": number})

Expand All @@ -140,7 +145,7 @@ def test_selector(number, expected):
def test_new_overwrites_old():
bundle = fluent.Bundle(
"en",
[str(data_dir / "fr.ftl"), str(data_dir / "en_hello.ftl")],
[data_dir / "fr.ftl", data_dir / "en_hello.ftl"],
)
assert bundle.get_translation("hello-world") == "Hello World"
assert (
Expand All @@ -150,14 +155,14 @@ def test_new_overwrites_old():


def test_id_not_found():
bundle = fluent.Bundle("fr", [str(data_dir / "fr.ftl")])
bundle = fluent.Bundle("fr", [data_dir / "fr.ftl"])
with pytest.raises(ValueError):
bundle.get_translation("missing", variables={"user": "Bob"})


def test_file_not_found():
with pytest.raises(FileNotFoundError):
fluent.Bundle("fr", [str(data_dir / "none.ftl")])
fluent.Bundle("fr", [data_dir / "none.ftl"])


@pytest.mark.parametrize("pass_strict_argument_explicitly", (True, False))
Expand All @@ -166,14 +171,14 @@ def test_parses_other_parts_of_file_that_contains_errors_in_non_strict_mode(
):
kwargs = dict(strict=False) if pass_strict_argument_explicitly else {}

bundle = fluent.Bundle("fr", [str(data_dir / "errors.ftl")], **kwargs)
bundle = fluent.Bundle("fr", [data_dir / "errors.ftl"], **kwargs)
translation = bundle.get_translation("valid-message")

assert translation == "I'm valid."


def test_raises_parser_error_on_file_that_contains_errors_in_strict_mode():
filename = str(data_dir / "errors.ftl")
filename = data_dir / "errors.ftl"

with pytest.raises(fluent.ParserError) as exc_info:
fluent.Bundle("fr", [filename], strict=True)
Expand Down