-
Notifications
You must be signed in to change notification settings - Fork 3
Add support for Fluent message attributes #34
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
Merged
LilyFirefly
merged 1 commit into
kraken-tech:main
from
kevin-hua-kraken:attribute-access
Oct 24, 2025
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| # Test file for Fluent attributes | ||
| welcome-message = Welcome! | ||
| .title = Welcome to our site | ||
| .aria-label = Welcome greeting | ||
|
|
||
| login-input = Email | ||
| .placeholder = [email protected] | ||
| .aria-label = Login input value | ||
| .title = Type your login email | ||
|
|
||
| # Message with variables in attributes | ||
| greeting = Hello | ||
| .formal = Hello, { $name } | ||
| .informal = Hi { $name }! | ||
|
|
||
| # Message with only attributes (no value) | ||
| form-button = | ||
| .submit = Submit Form | ||
| .cancel = Cancel | ||
| .reset = Reset Form | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -207,3 +207,84 @@ def test_raises_parser_error_on_file_that_contains_errors_in_strict_mode(): | |
|
|
||
| def test_parser_error_str(): | ||
| assert str(fluent.ParserError) == "<class 'rustfluent.ParserError'>" | ||
|
|
||
|
|
||
| # Attribute access tests | ||
|
|
||
|
|
||
| def test_basic_attribute_access(): | ||
| bundle = fluent.Bundle("en", [data_dir / "attributes.ftl"]) | ||
| assert bundle.get_translation("welcome-message.title") == "Welcome to our site" | ||
|
|
||
|
|
||
| def test_regular_message_still_works_with_attributes(): | ||
| """Test that accessing the main message value still works when it has attributes.""" | ||
| bundle = fluent.Bundle("en", [data_dir / "attributes.ftl"]) | ||
| assert bundle.get_translation("welcome-message") == "Welcome!" | ||
|
|
||
|
|
||
| def test_multiple_attributes_on_same_message(): | ||
| bundle = fluent.Bundle("en", [data_dir / "attributes.ftl"]) | ||
| assert bundle.get_translation("login-input.placeholder") == "[email protected]" | ||
| assert bundle.get_translation("login-input.aria-label") == "Login input value" | ||
| assert bundle.get_translation("login-input.title") == "Type your login email" | ||
|
|
||
|
|
||
| def test_attribute_with_variables(): | ||
| bundle = fluent.Bundle("en", [data_dir / "attributes.ftl"]) | ||
| result = bundle.get_translation("greeting.formal", variables={"name": "Alice"}) | ||
| assert result == f"Hello, {BIDI_OPEN}Alice{BIDI_CLOSE}" | ||
|
|
||
|
|
||
| def test_attribute_with_variables_use_isolating_off(): | ||
| bundle = fluent.Bundle("en", [data_dir / "attributes.ftl"]) | ||
| result = bundle.get_translation( | ||
| "greeting.informal", | ||
| variables={"name": "Bob"}, | ||
| use_isolating=False, | ||
| ) | ||
| assert result == "Hi Bob!" | ||
|
|
||
|
|
||
| def test_attribute_on_message_without_main_value(): | ||
| bundle = fluent.Bundle("en", [data_dir / "attributes.ftl"]) | ||
| assert bundle.get_translation("form-button.submit") == "Submit Form" | ||
| assert bundle.get_translation("form-button.cancel") == "Cancel" | ||
| assert bundle.get_translation("form-button.reset") == "Reset Form" | ||
|
|
||
|
|
||
| def test_message_without_value_raises_error(): | ||
| """Test that accessing a message without a value (only attributes) raises an error.""" | ||
| bundle = fluent.Bundle("en", [data_dir / "attributes.ftl"]) | ||
| with pytest.raises(ValueError, match="form-button - Message has no value"): | ||
| bundle.get_translation("form-button") | ||
|
|
||
|
|
||
| def test_missing_message_with_attribute_syntax_raises_error(): | ||
| bundle = fluent.Bundle("en", [data_dir / "attributes.ftl"]) | ||
| with pytest.raises(ValueError, match="nonexistent not found"): | ||
| bundle.get_translation("nonexistent.title") | ||
|
|
||
|
|
||
| def test_missing_attribute_raises_error(): | ||
| bundle = fluent.Bundle("en", [data_dir / "attributes.ftl"]) | ||
| with pytest.raises( | ||
| ValueError, | ||
| match="welcome-message.nonexistent - Attribute 'nonexistent' not found on message 'welcome-message'", | ||
| ): | ||
| bundle.get_translation("welcome-message.nonexistent") | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "identifier,expected", | ||
| ( | ||
| ("welcome-message", "Welcome!"), | ||
| ("welcome-message.title", "Welcome to our site"), | ||
| ("welcome-message.aria-label", "Welcome greeting"), | ||
| ("login-input", "Email"), | ||
| ("login-input.placeholder", "[email protected]"), | ||
| ), | ||
| ) | ||
| def test_attribute_and_message_access_parameterized(identifier, expected): | ||
| bundle = fluent.Bundle("en", [data_dir / "attributes.ftl"]) | ||
| assert bundle.get_translation(identifier) == expected | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.