import { TranslationEntry } from "../Definitions" ; export interface File { content : string ; name : string ; } const regex = /{{ *tr *((("([^"]|\\")*")|('([^']|\\')*')|(`([^`]|\\`)+`)|([\n ]*\+[\n ]*)?)+) *\/ *}}/ ; export function extractJsRendererTranslations ( file ) { let result = [ ] ; const lines = file . content . split ( '\n' ) ; let match ; let baseIndex = 0 ; while ( match = regex . exec ( file . content . substr ( baseIndex ) ) ) { let expression = ( match . groups || { } ) [ "message_expression" ] || match [ 1 ] ; let message ; try { message = eval ( expression ) ; } catch ( error ) { console . error ( "Failed to evaluate expression:\n%s" , expression ) ; baseIndex += match . index + match [ 0 ] . length ; continue ; } let character = baseIndex + match . index ; let line ; for ( line = 0 ; line < lines . length ; line ++ ) { const length = lines [ line ] . length + 1 ; if ( length > character ) { break ; } character -= length ; } result . push ( { filename : file . name , character : character + 1 , line : line + 1 , message : message , type : "js-template" } ) ; baseIndex += match . index + match [ 0 ] . length ; } return result ; }