@@ -472,15 +472,15 @@ namespace ts {
472
472
473
473
export function getTextOfPropertyName ( name : PropertyName ) : string {
474
474
switch ( name . kind ) {
475
- case SyntaxKind . Identifier :
476
- return ( < Identifier > name ) . text ;
477
- case SyntaxKind . StringLiteral :
478
- case SyntaxKind . NumericLiteral :
479
- return ( < LiteralExpression > name ) . text ;
480
- case SyntaxKind . ComputedPropertyName :
481
- if ( isStringOrNumericLiteral ( ( < ComputedPropertyName > name ) . expression ) ) {
482
- return ( < LiteralExpression > ( < ComputedPropertyName > name ) . expression ) . text ;
483
- }
475
+ case SyntaxKind . Identifier :
476
+ return ( < Identifier > name ) . text ;
477
+ case SyntaxKind . StringLiteral :
478
+ case SyntaxKind . NumericLiteral :
479
+ return ( < LiteralExpression > name ) . text ;
480
+ case SyntaxKind . ComputedPropertyName :
481
+ if ( isStringOrNumericLiteral ( ( < ComputedPropertyName > name ) . expression ) ) {
482
+ return ( < LiteralExpression > ( < ComputedPropertyName > name ) . expression ) . text ;
483
+ }
484
484
}
485
485
486
486
return undefined ;
@@ -4554,4 +4554,71 @@ namespace ts {
4554
4554
4555
4555
return flags ;
4556
4556
}
4557
+
4558
+ /**
4559
+ * Checks to see if the locale is in the appropriate format,
4560
+ * and if it is, attempts to set the appropriate language.
4561
+ */
4562
+ export function validateLocaleAndSetLanguage (
4563
+ locale : string ,
4564
+ sys : { getExecutingFilePath ( ) : string , resolvePath ( path : string ) : string , fileExists ( fileName : string ) : boolean , readFile ( fileName : string ) : string } ,
4565
+ errors ?: Diagnostic [ ] ) {
4566
+ const matchResult = / ^ ( [ a - z ] + ) ( [ _ \- ] ( [ a - z ] + ) ) ? $ / . exec ( locale . toLowerCase ( ) ) ;
4567
+
4568
+ if ( ! matchResult ) {
4569
+ if ( errors ) {
4570
+ errors . push ( createCompilerDiagnostic ( Diagnostics . Locale_must_be_of_the_form_language_or_language_territory_For_example_0_or_1 , "en" , "ja-jp" ) ) ;
4571
+ }
4572
+ return ;
4573
+ }
4574
+
4575
+ const language = matchResult [ 1 ] ;
4576
+ const territory = matchResult [ 3 ] ;
4577
+
4578
+ // First try the entire locale, then fall back to just language if that's all we have.
4579
+ // Either ways do not fail, and fallback to the English diagnostic strings.
4580
+ if ( ! trySetLanguageAndTerritory ( language , territory , errors ) ) {
4581
+ trySetLanguageAndTerritory ( language , /*territory*/ undefined , errors ) ;
4582
+ }
4583
+
4584
+ function trySetLanguageAndTerritory ( language : string , territory : string , errors ?: Diagnostic [ ] ) : boolean {
4585
+ const compilerFilePath = normalizePath ( sys . getExecutingFilePath ( ) ) ;
4586
+ const containingDirectoryPath = getDirectoryPath ( compilerFilePath ) ;
4587
+
4588
+ let filePath = combinePaths ( containingDirectoryPath , language ) ;
4589
+
4590
+ if ( territory ) {
4591
+ filePath = filePath + "-" + territory ;
4592
+ }
4593
+
4594
+ filePath = sys . resolvePath ( combinePaths ( filePath , "diagnosticMessages.generated.json" ) ) ;
4595
+
4596
+ if ( ! sys . fileExists ( filePath ) ) {
4597
+ return false ;
4598
+ }
4599
+
4600
+ // TODO: Add codePage support for readFile?
4601
+ let fileContents = "" ;
4602
+ try {
4603
+ fileContents = sys . readFile ( filePath ) ;
4604
+ }
4605
+ catch ( e ) {
4606
+ if ( errors ) {
4607
+ errors . push ( createCompilerDiagnostic ( Diagnostics . Unable_to_open_file_0 , filePath ) ) ;
4608
+ }
4609
+ return false ;
4610
+ }
4611
+ try {
4612
+ ts . localizedDiagnosticMessages = JSON . parse ( fileContents ) ;
4613
+ }
4614
+ catch ( e ) {
4615
+ if ( errors ) {
4616
+ errors . push ( createCompilerDiagnostic ( Diagnostics . Corrupted_locale_file_0 , filePath ) ) ;
4617
+ }
4618
+ return false ;
4619
+ }
4620
+
4621
+ return true ;
4622
+ }
4623
+ }
4557
4624
}
0 commit comments