import path from 'path' ; import { RewriteKeySource , DataProcessContext , RewriteKeyContext } from '../core/types' ; import { Framework } from './base' ; import { LanguageId } from '~/utils' ; import { Config , Global } from '~/core' ; class RubyRailsFramework extends Framework { id = 'ruby-rails' ; display = 'Ruby on Rails' ; detection = { gemfile : [ 'rails' , 'rails-i18n' , ] , } ; languageIds : LanguageId [ ] = [ 'ruby' , 'haml' , 'slim' , 'html.erb' , 'js.erb' , 'erb' , ] ; usageMatchRegex = [ '[^\\w\\d]t(?:\\(| )[\'"`]({key})[\'"`]' , '[^\\w\\d]t\\(\\:({key})\\)' , ] ; refactorTemplates ( keypath ) { return [ `t(".${ keypath }")` , keypath , ] ; } preprocessData ( data , context ) { return data [ context . locale || '' ] || { } ; } deprocessData ( data , context ) { return { [ context . locale || '' ] : data , } ; } rewriteKeys ( key , source , context = { } ) { if ( source === 'reference' && key . startsWith ( '.' ) && context . targetFile ) { const root = path . resolve ( Global . rootpath , Config . frameworksRubyRailsScopeRoot ) ; let scope = path . relative ( root , context . targetFile ) ; scope = scope . replace ( /(?:\.html\.erb|\.html\.haml|\.html\.slim|\.haml)$/ , '' ) . replace ( /(?:\/|\\)/g , '.' ) . replace ( /\._/g , '.' ) . replace ( /^_/g , '' ) ; return scope + key ; } return key ; } } export default RubyRailsFramework ;