@@ -8,36 +8,30 @@ import { FileAccess } from 'vs/base/common/network';
88import * as path from 'vs/base/common/path' ;
99
1010import * as lp from 'vs/base/node/languagePacks' ;
11- import product from 'vs/platform/product/common/product' ;
1211
1312const metaData = path . join ( FileAccess . asFileUri ( '' ) . fsPath , 'nls.metadata.json' ) ;
1413const _cache : Map < string , Promise < lp . NLSConfiguration > > = new Map ( ) ;
1514
16- function exists ( file : string ) {
17- return new Promise ( c => fs . exists ( file , c ) ) ;
18- }
19-
2015export function getNLSConfiguration ( language : string , userDataPath : string ) : Promise < lp . NLSConfiguration > {
21- return exists ( metaData ) . then ( ( fileExists ) => {
22- if ( ! fileExists || ! product . commit ) {
23- // console.log(`==> MetaData or commit unknown. Using default language.`);
24- // The OS Locale on the remote side really doesn't matter, so we return the default locale
25- return Promise . resolve ( { locale : 'en' , osLocale : 'en' , availableLanguages : { } } ) ;
26- }
27- const key = `${ language } ||${ userDataPath } ` ;
28- let result = _cache . get ( key ) ;
29- if ( ! result ) {
30- // The OS Locale on the remote side really doesn't matter, so we pass in the same language
31- result = lp . getNLSConfiguration ( product . commit , userDataPath , metaData , language , language ) . then ( value => {
32- if ( InternalNLSConfiguration . is ( value ) ) {
33- value . _languagePackSupport = true ;
34- }
35- return value ;
36- } ) ;
37- _cache . set ( key , result ) ;
38- }
39- return result ;
40- } ) ;
16+ const key = `${ language } ||${ userDataPath } ` ;
17+ let result = _cache . get ( key ) ;
18+ if ( ! result ) {
19+ // The OS Locale on the remote side really doesn't matter, so we pass in the same language
20+ result = lp . getNLSConfiguration ( "dummy_commit" , userDataPath , metaData , language , language ) . then ( value => {
21+ if ( InternalNLSConfiguration . is ( value ) ) {
22+ value . _languagePackSupport = true ;
23+ }
24+ // If the configuration has no results keep trying since code-server
25+ // doesn't restart when a language is installed so this result would
26+ // persist (the plugin might not be installed yet for example).
27+ if ( value . locale !== 'en' && value . locale !== 'en-us' && Object . keys ( value . availableLanguages ) . length === 0 ) {
28+ _cache . delete ( key ) ;
29+ }
30+ return value ;
31+ } ) ;
32+ _cache . set ( key , result ) ;
33+ }
34+ return result ;
4135}
4236
4337export namespace InternalNLSConfiguration {
@@ -46,3 +40,43 @@ export namespace InternalNLSConfiguration {
4640 return candidate && typeof candidate . _languagePackId === 'string' ;
4741 }
4842}
43+
44+ /**
45+ * The code below is copied from from src/main.js.
46+ */
47+
48+ export const getLocaleFromConfig = async ( argvResource : string ) : Promise < string > => {
49+ try {
50+ const content = stripComments ( await fs . promises . readFile ( argvResource , 'utf8' ) ) ;
51+ return JSON . parse ( content ) . locale ;
52+ } catch ( error ) {
53+ if ( error . code !== "ENOENT" ) {
54+ console . warn ( error )
55+ }
56+ return 'en' ;
57+ }
58+ } ;
59+
60+ const stripComments = ( content : string ) : string => {
61+ const regexp = / ( ' (?: [ ^ \\ ' ] * (?: \\ .) ? ) * ' ) | ( ' (?: [ ^ \\ ' ] * (?: \\ .) ? ) * ' ) | ( \/ \* (?: \r ? \n | .) * ?\* \/ ) | ( \/ { 2 , } .* ?(?: (?: \r ? \n ) | $ ) ) / g;
62+
63+ return content . replace ( regexp , ( match , _m1 , _m2 , m3 , m4 ) => {
64+ // Only one of m1, m2, m3, m4 matches
65+ if ( m3 ) {
66+ // A block comment. Replace with nothing
67+ return '' ;
68+ } else if ( m4 ) {
69+ // A line comment. If it ends in \r?\n then keep it.
70+ const length_1 = m4 . length ;
71+ if ( length_1 > 2 && m4 [ length_1 - 1 ] === '\n' ) {
72+ return m4 [ length_1 - 2 ] === '\r' ? '\r\n' : '\n' ;
73+ }
74+ else {
75+ return '' ;
76+ }
77+ } else {
78+ // We match a string
79+ return match ;
80+ }
81+ } ) ;
82+ } ;
0 commit comments