import { TextDocument } from 'vscode' ; import { LanguageId } from '../utils' ; import { Framework } from './base' ; import { Config , DetectionResult } from '~/core' ; import { extractionsParsers , DefaultExtractionRules , DefaultDynamicExtractionsRules , ExtractionRule , ExtractionScore } from '~/extraction' ; class FluentVueFramework extends Framework { id = 'fluent-vue' ; display = 'fluent-vue' ; detection = { packageJSON : [ 'fluent-vue' , ] , } ; languageIds : LanguageId [ ] = [ 'vue' , 'vue-html' , 'javascript' , 'typescript' , 'ejs' , ] ; enabledParsers = [ 'ftl' ] ; usageMatchRegex = [ 'v-t:({key})[\\s\\n=]' , 'i18n[ (\\n]\\s*path=[\'"`]({key})[\'"`]' , '(?:(?:this\\.|[^\\w\\d])(?:\\$t|\\$ta)\\()\\s*[\'"`]({key})[\'"`]' , '(?:fluent\\.(?:format|formatAttrs)\\()\\s*[\'"`]({key})[\'"`]' , ] ; refactorTemplates ( keypath , args ? , document ? , detection ? ) { let params = `'${ keypath }'` ; if ( args ?. length ) params += `, [${ args . join ( ', ' ) }]` ; switch ( detection ?. source ) { case 'html-inline' : return [ `{{ $t(${ params }) }}` ] ; case 'html-attribute' : return [ `$t(${ params })` ] ; case 'js-string' : return [ `$t(${ params })` , `this.$t(${ params })` ] ; } return [ `{{ $t(${ params }) }}` , `this.$t(${ params })` , `$t(${ params })` , keypath , ] ; } supportAutoExtraction = [ 'vue' ] ; detectHardStrings ( doc ) { const text = doc . getText ( ) ; return extractionsParsers . html . detect ( text , [ new FluentVueExtractionRule ( ) , ... DefaultExtractionRules ] , DefaultDynamicExtractionsRules , Config . extractParserHTMLOptions , script => extractionsParsers . babel . detect ( script , DefaultExtractionRules , DefaultDynamicExtractionsRules , Config . extractParserBabelOptions ) ) ; } } export class FluentVueExtractionRule extends ExtractionRule { name = 'fluent-vue' ; shouldExtract ( str ) { if ( str . match ( /[\w-]+\s?=.+/gm ) ) return ExtractionScore . MustExclude ; return ExtractionScore . ShouldInclude ; } } export default FluentVueFramework ;