import * as Generator from 'yeoman-generator' ; import * as lodash from 'lodash' ; import { GeneratorTeamsAppOptions } from './../app/GeneratorTeamsAppOptions' ; import { Yotilities } from './../app/Yotilities' ; import { ManifestGeneratorFactory } from '../app/manifestGeneration/ManifestGeneratorFactory' ; export class LocalizationGenerator extends Generator { options : GeneratorTeamsAppOptions ; public constructor ( args , opts ) { super ( args , opts ) ; opts . force = true ; this . options = opts . options ; this . desc ( 'Adds localization support to a Teams project.' ) ; } public prompting ( ) { if ( this . options . localization ) { const generatorPrefix = "[localization]" ; return this . prompt ( [ { type : 'input' , name : 'defaultLanguage' , message : 'What is the default language tag? (format: en-us)' , prefix : generatorPrefix , default : "en-us" , validate : ( input , answers ) => { return /^[A-Za-z0-9]{1,8}(-[A-Za-z0-9]{1,8}){0,2}$/ . test ( input ) ; } , when : ( answers ) => { return this . options . existingManifest === undefined || this . options . existingManifest . localizationInfo === undefined || this . options . existingManifest . localizationInfo && this . options . existingManifest . localizationInfo . defaultLanguageTag === undefined ; } } , { type : 'confirm' , name : 'addMoreLanguages' , message : 'Would you like to add a translation?' , prefix : generatorPrefix , default : true } , { type : 'input' , name : 'additionalLanguage' , message : "What is the language tag of the translation? (format: en-us)" , prefix : generatorPrefix , validate : ( input , answers ) => { if ( input == answers . defaultLanguage || this . options . existingManifest && this . options . existingManifest . localizationInfo && this . options . existingManifest . localizationInfo . defaultLanguageTag == input ) { return "Must be different from the default language" ; } if ( this . options . existingManifest && this . options . existingManifest . localizationInfo && this . options . existingManifest . localizationInfo . additionalLanguages && this . options . existingManifest . localizationInfo . additionalLanguages . find ( ( x ) => x . languageTag == input ) ) { return "Specified language already exists in the manifest" ; } return /^[A-Za-z0-9]{1,8}(-[A-Za-z0-9]{1,8}){0,2}$/ . test ( input ) ; } , when : ( answers ) => answers . addMoreLanguages } ] ) . then ( ( answers ) => { this . options . defaultLanguage = answers . defaultLanguage ; this . options . additionalLanguage = answers . additionalLanguage ; } ) ; } } public writing ( ) { const manifestGeneratorFactory = new ManifestGeneratorFactory ( ) ; const manifestGenerator = manifestGeneratorFactory . createManifestGenerator ( this . options . manifestVersion ) ; let manifestPath = "src/manifest/manifest.json" ; const manifest = this . fs . readJSON ( manifestPath ) ; manifestGenerator . updateLocalizationManifest ( manifest , this . options ) ; this . fs . writeJSON ( manifestPath , manifest ) ; if ( this . options . additionalLanguage ) { let localizationData = { "$schema" : "https://developer.microsoft.com/en-us/json-schemas/teams/v1.5/MicrosoftTeams.Localization.schema.json" , } ; localizationData [ "name.short" ] = manifest . name . short ; localizationData [ "name.full" ] = manifest . name . full ; localizationData [ "description.short" ] = manifest . description . short ; localizationData [ "description.full" ] = manifest . description . full ; if ( manifest . staticTabs ) { manifest . staticTabs . forEach ( ( tab , idx ) => { localizationData [ `staticTabs[${ idx }].name` ] = tab . name ; } ) ; } if ( manifest . bots ) { manifest . bots . forEach ( ( bot , idx ) => { if ( bot . commandLists ) { bot . commandLists . forEach ( ( cl , idx2 ) => { if ( cl . commands ) { cl . commands . forEach ( ( c , idx3 ) => { localizationData [ `bots[${ idx }].commandLists[${ idx2 }].commands[${ idx3 }].title` ] = c . title ; localizationData [ `bots[${ idx }].commandLists[${ idx2 }].commands[${ idx3 }].description` ] = c . description ; } ) ; } } ) ; } } ) ; } if ( manifest . composeExtensions ) { manifest . composeExtensions . forEach ( ( ce , idx ) => { if ( ce . commands ) { ce . commands . forEach ( ( c , idx2 ) => { localizationData [ `composeExtensions[${ idx }].commands[${ idx2 }].title` ] = c . title ; localizationData [ `composeExtensions[${ idx }].commands[${ idx2 }].description` ] = c . description ; if ( c . parameters ) { c . parameters . forEach ( ( p , idx3 ) => { localizationData [ `composeExtensions[${ idx }].commands[${ idx2 }].parameters[${ idx3 }].title` ] = p . title ; localizationData [ `composeExtensions[${ idx }].commands[${ idx2 }].parameters[${ idx3 }].description` ] = p . description ; localizationData [ `composeExtensions[${ idx }].commands[${ idx2 }].parameters[${ idx3 }].value` ] = p . value ; if ( p . choices ) { p . choices . forEach ( ( ch , idx4 ) => { localizationData [ `composeExtensions[${ idx }].commands[${ idx2 }].parameters[${ idx3 }].choices[${ idx4 }].title` ] = ch . title ; } ) ; } } ) ; } if ( c . taskInfo ) { localizationData [ `composeExtensions[${ idx }].commands[${ idx2 }].taskInfo.title` ] = c . taskInfo . title ; } } ) ; } } ) ; } this . fs . writeJSON ( `src/manifest/${ this . options . additionalLanguage }.json` , localizationData ) ; } } }