@@ -6,6 +6,7 @@ export interface ParsedSaveData {
66 writtenQuestionIds : number [ ] ;
77 correctQuestionIds : number [ ] ;
88 views : View [ ] ;
9+ language : string ;
910}
1011
1112export interface MergeConflict {
@@ -33,6 +34,13 @@ function normalize(s: string): string {
3334}
3435
3536export function parseImportFile ( data : string ) : ParsedSaveData {
37+ // v3+: structured metadata block; v2 fallback: comment in header
38+ const structuredLangMatch = data . match (
39+ / \/ \* \s - - - B E G I N S a v e L a n g u a g e - - - \* \/ \n - - \s ( \w + ) \n \/ \* \s - - - E N D S a v e L a n g u a g e - - - \* \/ /
40+ ) ;
41+ const commentLangMatch = data . match ( / ^ - - L a n g u a g e : ( \w + ) $ / m) ;
42+ const language = structuredLangMatch ? structuredLangMatch [ 1 ] : ( commentLangMatch ? commentLangMatch [ 1 ] : "sv" ) ;
43+
3644 const rawQueriesMatch = data . match (
3745 / \/ \* \s - - - B E G I N R a w Q u e r i e s - - - \* \/ \n \/ \* \n ( [ \s \S ] * ?) \n \* \/ \n \/ \* \s - - - E N D R a w Q u e r i e s - - - \* \/ /
3846 ) ;
@@ -70,11 +78,11 @@ export function parseImportFile(data: string): ParsedSaveData {
7078 }
7179 }
7280
73- return { rawQueries, correctQueries, writtenQuestionIds, correctQuestionIds, views } ;
81+ return { rawQueries, correctQueries, writtenQuestionIds, correctQuestionIds, views, language } ;
7482}
7583
76- export function getLocalData ( langPrefix ? : string ) : ParsedSaveData {
77- const pfx = langPrefix ? `${ langPrefix } :` : "" ;
84+ export function getLocalData ( langPrefix : string = "sv" ) : ParsedSaveData {
85+ const pfx = `${ langPrefix } :` ;
7886 const writtenQuestionIds : number [ ] = JSON . parse (
7987 localStorage . getItem ( `${ pfx } writtenQuestions` ) || "[]"
8088 ) ;
@@ -96,7 +104,7 @@ export function getLocalData(langPrefix?: string): ParsedSaveData {
96104
97105 const views : View [ ] = JSON . parse ( localStorage . getItem ( `${ pfx } views` ) || "[]" ) ;
98106
99- return { rawQueries, correctQueries, writtenQuestionIds, correctQuestionIds, views } ;
107+ return { rawQueries, correctQueries, writtenQuestionIds, correctQuestionIds, views, language : langPrefix } ;
100108}
101109
102110export function detectConflicts (
@@ -273,5 +281,5 @@ export function buildMergedData(
273281 new Set ( [ ...local . correctQuestionIds , ...imported . correctQuestionIds ] )
274282 ) . filter ( ( id ) => String ( id ) in correctQueries ) ;
275283
276- return { rawQueries, correctQueries, writtenQuestionIds, correctQuestionIds, views } ;
284+ return { rawQueries, correctQueries, writtenQuestionIds, correctQuestionIds, views, language : local . language } ;
277285}
0 commit comments