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

Allow escaping linked translations #248

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
12 changes: 12 additions & 0 deletions slang/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -709,6 +709,18 @@ If namespaces are used, then it has to be specified in the path too.

[RichTexts](#-richtext) can also contain links! But only [RichTexts](#-richtext) can link to [RichTexts](#-richtext).

Optionally, you can escape linked translations with this syntax:


```json
{
"fields": {
"nbsp": "\u00a0"
},
"message": "10@:{fields.nbsp}Days"
}
```

### ➤ Pluralization

This library uses the concept defined [here](https://www.unicode.org/cldr/charts/latest/supplemental/language_plural_rules.html).
Expand Down
2 changes: 1 addition & 1 deletion slang/lib/builder/model/node.dart
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,7 @@ _ParseLinksResult _parseLinks({
}) {
final links = <String>{};
final parsedContent = input.replaceAllMapped(RegexUtils.linkedRegex, (match) {
final linkedPath = match.group(1)!;
final linkedPath = (match.group(1) ?? match.group(2))!;
links.add(linkedPath);

if (linkParamMap == null) {
Expand Down
8 changes: 6 additions & 2 deletions slang/lib/src/builder/utils/regex_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@ class RegexUtils {
/// 2 = argument of ${argument}
static RegExp argumentsDartRegex = RegExp(r'(?<!\\)\$(?:([\w]+)|\{(.+?)\})');

/// matches @:translation.key
static RegExp linkedRegex = RegExp(r'@:(\w[\w|.]*\w|\w)');
/// matches @:translation.key or @:{translation.key}, but not \@:translation.key
/// 1 = argument of @:translation.key
/// 2 = argument of @:{translation.key}
static RegExp linkedRegex = RegExp(
r'(?<!\\)@:(?:(\w[\w|.]*\w|\w)|\{(\w[\w|.]*\w|\w)\})',
);

/// matches $hello, $ but not \$
static RegExp dollarRegex = RegExp(r'([^\\]|^)\$');
Expand Down
8 changes: 8 additions & 0 deletions slang/test/unit/model/node_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,14 @@ void main() {
expect(node.params, <String>{});
});

test('with escaped links', () {
final test = r'@:.c@:{a}@:{hi}@:wow. @:{nice.cool} @:nice.cool';
final node = textNode(test, StringInterpolation.dart);
expect(node.content,
r'@:.c${_root.a}${_root.hi}${_root.wow}. ${_root.nice.cool} ${_root.nice.cool}');
expect(node.params, <String>{});
});

test('with links and params', () {
final test = r'@:a @:b';
final node = textNode(test, StringInterpolation.dart, linkParamMap: {
Expand Down