@@ -136,9 +136,31 @@ class AI{
136136 options = { text : args [ 0 ] } ;
137137 }
138138
139- // if second argument is string, it's the language
139+ // * ai.txt2speech('Hello, world!', 'en-US')
140+ // * ai.txt2speech('Hello, world!', 'Brian')
141+ // * ai.txt2speech('Hello, world!', 'en-US', 'Brian')
142+ // * ai.txt2speech('Hello, world!', 'Brian', 'en-US')
140143 if ( args [ 1 ] && typeof args [ 1 ] === 'string' ) {
141- options . language = args [ 1 ] ;
144+ // Check if it's a language code (ISO 639-1 or with region)
145+ // Pattern matches: en, es, fr, de, en-US, es-ES, fr-FR, etc.
146+ const languageCodePattern = / ^ [ a - z ] { 2 } ( - [ A - Z ] { 2 } ) ? $ / ;
147+
148+ // * ai.txt2speech('Hello, world!', 'en-US')
149+ if ( languageCodePattern . test ( args [ 1 ] ) ) {
150+ options . language = args [ 1 ] ;
151+ }
152+ // * ai.txt2speech('Hello, world!', 'Brian')
153+ else {
154+ options . voice = args [ 1 ] ;
155+ }
156+ // * ai.txt2speech('Hello, world!', 'en-US', 'Brian')
157+ if ( args [ 2 ] && typeof args [ 2 ] === 'string' && options . language ) {
158+ options . voice = args [ 2 ] ;
159+ }
160+ // * ai.txt2speech('Hello, world!', 'Brian', 'en-US')
161+ else if ( args [ 2 ] && typeof args [ 2 ] === 'string' && options . voice ) {
162+ options . language = args [ 2 ] ;
163+ }
142164 }
143165
144166 // check input size
0 commit comments