* Checks to see if the locale is in the appropriate format, * and if it is, attempts to set the appropriate language.
(locale, sys, errors)
| 12669 | * and if it is, attempts to set the appropriate language. |
| 12670 | */ |
| 12671 | function validateLocaleAndSetLanguage(locale, sys, errors) { |
| 12672 | var lowerCaseLocale = locale.toLowerCase(); |
| 12673 | var matchResult = /^([a-z]+)([_\-]([a-z]+))?$/.exec(lowerCaseLocale); |
| 12674 | if (!matchResult) { |
| 12675 | if (errors) { |
| 12676 | errors.push(ts.createCompilerDiagnostic(ts.Diagnostics.Locale_must_be_of_the_form_language_or_language_territory_For_example_0_or_1, "en", "ja-jp")); |
| 12677 | } |
| 12678 | return; |
| 12679 | } |
| 12680 | var language = matchResult[1]; |
| 12681 | var territory = matchResult[3]; |
| 12682 | // First try the entire locale, then fall back to just language if that's all we have. |
| 12683 | // Either ways do not fail, and fallback to the English diagnostic strings. |
| 12684 | if (ts.contains(ts.supportedLocaleDirectories, lowerCaseLocale) && !trySetLanguageAndTerritory(language, territory, errors)) { |
| 12685 | trySetLanguageAndTerritory(language, /*territory*/ undefined, errors); |
| 12686 | } |
| 12687 | // Set the UI locale for string collation |
| 12688 | ts.setUILocale(locale); |
| 12689 | function trySetLanguageAndTerritory(language, territory, errors) { |
| 12690 | var compilerFilePath = ts.normalizePath(sys.getExecutingFilePath()); |
| 12691 | var containingDirectoryPath = ts.getDirectoryPath(compilerFilePath); |
| 12692 | var filePath = ts.combinePaths(containingDirectoryPath, language); |
| 12693 | if (territory) { |
| 12694 | filePath = filePath + "-" + territory; |
| 12695 | } |
| 12696 | filePath = sys.resolvePath(ts.combinePaths(filePath, "diagnosticMessages.generated.json")); |
| 12697 | if (!sys.fileExists(filePath)) { |
| 12698 | return false; |
| 12699 | } |
| 12700 | // TODO: Add codePage support for readFile? |
| 12701 | var fileContents = ""; |
| 12702 | try { |
| 12703 | fileContents = sys.readFile(filePath); |
| 12704 | } |
| 12705 | catch (e) { |
| 12706 | if (errors) { |
| 12707 | errors.push(ts.createCompilerDiagnostic(ts.Diagnostics.Unable_to_open_file_0, filePath)); |
| 12708 | } |
| 12709 | return false; |
| 12710 | } |
| 12711 | try { |
| 12712 | // this is a global mutation (or live binding update)! |
| 12713 | ts.setLocalizedDiagnosticMessages(JSON.parse(fileContents)); |
| 12714 | } |
| 12715 | catch (_a) { |
| 12716 | if (errors) { |
| 12717 | errors.push(ts.createCompilerDiagnostic(ts.Diagnostics.Corrupted_locale_file_0, filePath)); |
| 12718 | } |
| 12719 | return false; |
| 12720 | } |
| 12721 | return true; |
| 12722 | } |
| 12723 | } |
| 12724 | ts.validateLocaleAndSetLanguage = validateLocaleAndSetLanguage; |
| 12725 | function getOriginalNode(node, nodeTest) { |
| 12726 | if (node) { |
nothing calls this directly
no test coverage detected