@@ -7,6 +7,10 @@ import {
7
7
TRANSLOCO_CONFIG ,
8
8
TranslocoConfig ,
9
9
} from './transloco.config' ;
10
+ import {
11
+ formatTranslocoError ,
12
+ TranslocoErrorCode ,
13
+ } from './transloco-error-code' ;
10
14
11
15
export const TRANSLOCO_TRANSPILER = new InjectionToken < TranslocoTranspiler > (
12
16
typeof ngDevMode !== 'undefined' && ngDevMode ? 'TRANSLOCO_TRANSPILER' : '' ,
@@ -164,6 +168,8 @@ export function getFunctionArgs(argsString: string): string[] {
164
168
return args ;
165
169
}
166
170
171
+ const functionalCallRegExp = / \[ \[ \s * ( \w + ) \( ( .* ?) \) \s * ] ] / g;
172
+
167
173
@Injectable ( )
168
174
export class FunctionalTranspiler
169
175
extends DefaultTranspiler
@@ -175,19 +181,27 @@ export class FunctionalTranspiler
175
181
let transpiled = value ;
176
182
if ( isString ( value ) ) {
177
183
transpiled = value . replace (
178
- / \[ \[ \s * ( \w + ) \( ( . * ? ) \) \s * ] ] / g ,
184
+ functionalCallRegExp ,
179
185
( match : string , functionName : string , args : string ) => {
180
186
try {
181
187
const func : TranslocoTranspilerFunction =
182
188
this . injector . get ( functionName ) ;
183
189
184
190
return func . transpile ( ...getFunctionArgs ( args ) ) ;
185
191
} catch ( e : unknown ) {
186
- let message = `There is an error in: '${ value } '.
192
+ let message : string ;
193
+ if ( typeof ngDevMode !== 'undefined' && ngDevMode ) {
194
+ message = `There is an error in: '${ value } '.
187
195
Check that the you used the right syntax in your translation and that the implementation of ${ functionName } is correct.` ;
188
- if ( ( e as Error ) . message . includes ( 'NullInjectorError' ) ) {
189
- message = `You are using the '${ functionName } ' function in your translation but no provider was found!` ;
196
+ if ( ( e as Error ) . message . includes ( 'NullInjectorError' ) ) {
197
+ message = `You are using the '${ functionName } ' function in your translation but no provider was found!` ;
198
+ }
199
+ } else {
200
+ message = formatTranslocoError (
201
+ TranslocoErrorCode . FunctionalTranspilerInvalidSyntax ,
202
+ ) ;
190
203
}
204
+
191
205
throw new Error ( message ) ;
192
206
}
193
207
} ,
0 commit comments