Skip to content

Commit

Permalink
Proof of concept for projectfluent#587 (transform placeables)
Browse files Browse the repository at this point in the history
Signed-off-by: Slava Fomin II <[email protected]>
  • Loading branch information
slavafomin committed Apr 23, 2022
1 parent 5406b34 commit cb5d414
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
10 changes: 9 additions & 1 deletion fluent-bundle/src/bundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ export type TextTransform = (text: string) => string;
type NativeValue = string | number | Date;
export type FluentVariable = FluentValue | NativeValue;

const noTransform: TextTransform = (
text => text
);

/**
* Message bundles are single-language stores of translation resources. They are
* responsible for formatting message values and attributes to strings.
Expand All @@ -23,6 +27,7 @@ export class FluentBundle {
public _functions: Record<string, FluentFunction>;
public _useIsolating: boolean;
public _transform: TextTransform;
public _transformPlaceable: TextTransform;
public _intls: IntlCache;

/**
Expand Down Expand Up @@ -59,11 +64,13 @@ export class FluentBundle {
{
functions,
useIsolating = true,
transform = (v: string): string => v
transform = noTransform,
transformPlaceable = noTransform,
}: {
functions?: Record<string, FluentFunction>;
useIsolating?: boolean;
transform?: TextTransform;
transformPlaceable?: TextTransform;
} = {}
) {
this.locales = Array.isArray(locales) ? locales : [locales];
Expand All @@ -74,6 +81,7 @@ export class FluentBundle {
};
this._useIsolating = useIsolating;
this._transform = transform;
this._transformPlaceable = transformPlaceable;
this._intls = getMemoizerForLocale(locales);
}

Expand Down
6 changes: 4 additions & 2 deletions fluent-bundle/src/resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ function resolveVariableReference(
// Convert the argument to a Fluent type.
switch (typeof arg) {
case "string":
return arg;
return scope.bundle._transformPlaceable(arg);
case "number":
return new FluentNumber(arg);
case "object":
Expand Down Expand Up @@ -345,7 +345,9 @@ export function resolveComplexPattern(
result.push(FSI);
}

result.push(resolveExpression(scope, elem).toString(scope));
result.push(
resolveExpression(scope, elem).toString(scope)
);

if (useIsolating) {
result.push(PDI);
Expand Down

0 comments on commit cb5d414

Please sign in to comment.