5.1.0 • Published 7 months ago

gettext-extractor-svelte v5.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
7 months ago

gettext-extractor for svelte files

This extractor extracts also all the called functions and the function definition into a separate dictionary.

Installation

npm install gettext-extractor-svelte

Usage:

import {
    SvelteGettextExtractor, 
    callExpressionExtractor, 
    ICustomJsExtractorOptions,
    FunctionExtractorBuilder
} from 'gettext-extractor-svelte';

const extractor = new SvelteGettextExtractor();
const functionExtractor = new FunctionExtractorBuilder();
const findTranslationClassExpression = functionExtractor.variableDeclaration(
    'Foo', functionExtractor.classExpression(undefined, [
        functionExtractor.methodDeclaration('bar', true)
    ])
);

const options: ICustomJsExtractorOptions = {
    arguments: {
        text: 0,
        context: 1,
        comments: 2
    },
    comments: {
        commentString: 'comment',
        props: {
            props: ['{', '}']
        }
    },
    identifierKeys: ['text', 'context'],
    translatorFunction: {
        functionExtractor: findTranslationClassExpression,
        identifier: 'translatorFunction',
        restrictToFile: './src/translator.ts'
    }
}

extractor.createSvelteParser()
    .addExtractor(callExpressionExtractor('t', options))
    .parseFilesGlob('./src/**/*.svelte');

const messages = extractor.getMessages();
const functionDict = extractor.getFunctions();
const messageDict = extractor.getMessageDictionary();

From the following svelte file named src/App.svelte:

<script lang="ts">
    import { t } from './translator-function';
    import Component from './Component.svelte';
    export let place: string;
    let caption = t(
        'FooCaption', 
        'Context', 
        {comment: 'Comment', path: 'https://www.example.com'}
    );
</script>

<body>
    <h1>{caption}</h1>
    <p>{t('Foo')}</p>
    {#each [t('Bar'), t('Baz')] as text}
        <p>{text}</p>
        <Component label="{t('Bax', 'Context', {comment: 'Comment'})}">
            {t(
                'Hello {PLACE}', 
                'Context', 
                { 
                    comment: 'Multiline\nComment', 
                    props: {PLACE: 'The place where you are'},
                    messageformat: 'This could be a messageformat function'
                },
                {PLACE: place}
            )}
        </Component>
    {/each}
</body>

and the following translation function in file src/translator.ts:

let foo = class Bar {
    bar(translationObject: any) {
        return 'I am translated';
    }
}

we extract the following messages:

[
    {
        text: 'FooCaption',
        context: 'Context',
        comments: [
            'Comment',
            'path: https://www.example.com'
        ],
        references: [
            'src/App.svelte:5'
        ],
    },
    {
        text: 'Foo',
        references: [
            'src/App.svelte:14'
        ],
    },
    {
        text: 'Bar',
        references: [
            'src/App.svelte:15'
        ],
    },
    {
        text: 'Baz',
        references: [
            'src/App.svelte:15'
        ],
    },
    {
        text: 'Bax',
        context: 'Context',
        comments: [
            'Comment',
        ],
        references: [
            'src/App.svelte:17'
        ],
    },
    {
        text: 'Hello {PLACE}',
        context: 'Context',
        comments: [
            'Multiline',
            'Comment',
            'messageformat: This could be a messageformat function',
            '{PLACE}: The place where you are'
        ],
        references: [
            'src/App.svelte:18'
        ],
    },
]

the following functions:

{
    'src/App.svelte': [
        {
            functionString: `_(
        'FooCaption',
        'Context',
        {comment: 'Comment', path: 'https://www.example.com'}
    )`,
            functionData: {
                functionName: '_',
                functionArgs: []
            },
            identifier: '{"text":"FooCaption","context":"Context"}',
            startChar: 162,
            endChar: 273
        },                
        {
            functionString: "_('Foo')",
            functionData: {
                functionName: '_',
                functionArgs: []
            },
            identifier: '{"text":"Foo"}',
            startChar: 324,
            endChar: 332
        },
        {
            functionString: "_('Bar')",
            functionData: {
                functionName: '_',
                functionArgs: []
            },
            identifier: '{"text":"Bar"}',
            startChar: 350,
            endChar: 358
        },
        {
            functionString: "_('Baz')",
            functionData: {
                functionName: '_',
                functionArgs: []
            },
            identifier: '{"text":"Baz"}',
            startChar: 360,
            endChar: 368
        },
        {
            functionString: "_('Bax')",
            functionData: {
                functionName: '_',
                functionArgs: []
            },
            identifier: '{"text":"Bax"}',
            startChar: 428,
            endChar: 436
        },
        {
            functionString: `_(
                'Hello {PLACE}',
                'Context',
                {
                    comment: 'Multiline\\nComment',
                    props: {PLACE: 'The place where you are'},
                    messageformat: 'This could be a messageformat function'
                },
                {PLACE: place}
            )`,
            functionData: {
                functionName: '_',
                functionArgs: ['{PLACE: place}']
            },
            identifier: '{"text":"Hello {PLACE}","context":"Context"}',
            startChar: 486,
            endChar: 820
        } 
    ],
    'src/translator.ts': [
        {
            functionString:
`bar(translationObject: any) {
        return 'I am translated';
    }`,
            identifier: 'translatorFunction',
            definition: true, // marks that this entry is an extracted translation function
            startChar: 26,
            endChar: 95
        }
    ]
}

and the mapping of identifiers to text as an object

{
    '{"text":"Foo"}': 'Foo',
    '{"text":"Bar"}': 'Bar',
    '{"text":"Baz"}': 'Baz',
    '{"text":"Bax"}': 'Bax',
    '{"text":"FooCaption","context":"Context"}': 'FooCaption',
    '{"text":"Hello {PLACE}","context":"Context"}': 'Hello {PLACE}'
}

Additional methods for SvelteGettextExtractor

  createSvelteParser()

Create parser for parsing .svelte files

Return Value

SvelteParser · Can be used in the same way as parser created with createJsParser(), except the following added parse option:

NameTypeDetails
optionsobjectAdditional parse option
translatorFunctionsTTranslatorFunction | TTranslatorFunction[]Translation Function Definition to extract functions.

  getFunctions()

Gets all parsed function calls

Return Value

object · Dictionary with keys of file name and values of a list of function objects with properties as described below

NameTypeDetails
functionStringstringString of the function call in the source code
functionDataobjectObject containing keys functionName and functionArgs
 functionNamestringName of the called function
 functionArgsstring[]Arguments without arguments defining text, textPlural, context and comment
identifierstringIdentifier string constructed trough options.identifierKeys
definitiontrueWhen set marks that string is part of translation function definition
startCharnumberIndex where the function call string starts
endCharnumberIndex where the function call string ends

  getLastAddedFunctions()

Receive array of functions, which got added in the last call of parseJsString, parseSvelteString.

Return Value

array · Same as values of dictionary described in getFunctions()

  getFunctionsByFileName(fileName)

Gets all parsed function calls in a file

Parameters

NameTypeDetails
fileNamestringRequired · Name of file
Return Value

array · Same as values of dictionary described in getFunctions()

  saveFunctionJSON(fileName)

Save functions dictionary as a JSON file.

Parameters

NameTypeDetails
fileNamestringRequired · Name of file

Return Value

void

  saveFunctionJSONAsync(fileName)

Save functions dictionary as a JSON file asynchronously.

Parameters

NameTypeDetails
fileNamestringRequired · Name of file

Return Value

Promise

  getMessageDictionary()

Recieve a dictionary mapping message text to function identifiers.

Return Value

object · Dictionary with identifier strings as keys and message text as value

  getMessagesWithId()

Recieve array of messages with identifier.

Return Value
NameTypeDetails
textstringMessage string
textPluralstringPlural version of the message string
contextstringMessage context
referencesstring[]Array of file references where the message was extracted from
commentsstring[]Array of comments for this message
identifierstringIdentifier string constructed trough options.identifierKeys

  getLastAddedMessages()

Receive array of messages with identifier, which got added in the last call of parseJsString, parseSvelteString or parseHtmlString.

Return Value

array · List of message objects (same properties as for getMessagesWithId())

  getTransformedMessages<T = any>(func: (messages: IMessage[]) => T)

Transform message object with custom function.

Parameters

NameTypeDetails
func(messages: IMessage[]) => TRequired · Transformer function
Return Value

T · Depends on your function

For all other available options please look at the package gettext-extractor

JS Parser for gettext-extractor

Extract comments provided by a string or an object in the translator function.

import { callExpressionExtractor, ICustomJsExtractorOptions } 
    from 'gettext-extractor-svelte';
import { GettextExtractor } from 'gettext-extractor'; // Works also with SvelteGettextExtractor

const options: ICustomJsExtractorOptions = {
    arguments: {
        text: 0,
        textPlural: 1,
        comments: 2,
        context: 3,
    },
    comments: {
        commentString: 'comment',
        props: {
            props: ['{', '}']
        }
    }
};

const extractor = new GettextExtractor();

extractor
    .createJsParser()
    .addExtractor(callExpressionExtractor('_', options))
    .parseFilesGlob('src/**/*.@(ts|js|tsx|jsx)');

callExpressionExtractor(calleeName, options)

Parameters

NameTypeDetails
calleeNamestring orstring[]Required · Name(s) of the function(s)
optionsobjectOptions to configure the extractor function
argumentsobjectRequired · See Argument Mapping below
commentsobjectSee Comment Options below
identifierKeys<'text' | 'textPlural' | 'context'>[]Fields for constructing ids to map messages to functions, defaults to all. At least one of these keys should match each message function. Defaults to ['text', 'context']
 translatorFunctionobjectSee Translator Function Options below
contentobjectSee Content Options below
Argument Mapping
NameTypeDetails
textnumberRequired · Position of the argument containing the message text
textPluralnumberPosition of the argument containing the plural message text
contextnumberPosition of the argument containing the message context
commentsnumberPosition of the argument containing the comments string or object
Comment Options

If ommitted the comment is expected to be a string. If fallback is true, the comment has to be an object, otherwise it can be a string or an object.

NameTypeDefaultDetails
commentStringstringcommentKey for providing plain comments
propsobjectEach key under props has a value of an array with two strings. In the comment object we can provide key value pairs under each key defined under props. Each of these keys gets wrapped in between the provided two strings. Then after a semicolon the value is concatenated.
throwWhenMalformedbooleantrueIf set to true, throws an error when in the comment object any value is not a plain string
fallbackbooleanfalseIf set to true, an omitted argument fallbacks to the next argument if the next argument is of different type

If not trough commentString or props specified keys are used in the comment object, then these keys (concatenated with dots when they are nested) are added to the comments with a semicolon followed by the value of the key.

Content Options
NameTypeDefaultDetails
trimWhiteSpacebooleanfalseIf set to true, white space at the very beginning and at the end of the content will get removedNormally this behaves like .trim(), however if preseveIndentation is true, the indentation of the first line is kept as well.
preserveIndentationbooleantrueIf set to false, white space at the beginning of the line will get removed
replaceNewLinesfalse or stringfalseIf a string is provided all new lines will be replaced with it
Function Extractor Options
NameTypeDefaultDetails
functionExtractorFunctionExtractorThe function extractor describing the typescript nodes of the function to extract
identifierstringThe identifier under which the function will be added to the dict
restrictToFilestringWhen set than only the specified file will be parsed for the function
Return Value

function · An extractor function that extracts messages from call expressions.

Example

With the example settings from the usage example and the following functions

// We can provide comments as string
const string1 = _(
    'Foo',
    'Plural',
    'Comment',
    'Context'
);
// Or we can provide comments as object
const string2 = _(
    'Hello {PLACE}',
    'Plural',
    {
        comment: 'Comment',
        props: {
            PLACE: 'The place of interest'
        },
        path: 'https://www.example.com',
        nested: {
            key1: 'Key1',
            key2: 'Key2'
        }
    }
);
// When type of argument does not match declared type, then all following arguments are ignored
const string3 = _(
    'Foo2',
    {
        comment: 'Comment'
    },
    'Context'
)
// We can omit empty arguments with `null`, `undefined` or `0`
const string4 = _(
    'Foo3',
    null,
    null,
    'Context'
);

We extract the following messages

[
    {
        text: 'Foo',
        textPlural: 'Plural',
        coments: ['Comment'],
        context: 'Context'
    },
    {
        text: 'Hello {PLACE}',
        textPlural: 'Plural',
        comments: [
            'Comment',
            'path: https://www.example.com',
            '{PLACE}: The place of interest',
            'nested.key1: Key1',
            'nested.key2: Key2'
        ]
    },
    {
        text: 'Foo2'
    },
    {
        text: 'Foo3',
        context: 'Context'
    }
]

If we have the option fallback: true set:

const options: ICustomJsExtractorOptions = {
    arguments: {
        text: 0,
        textPlural: 1,
        comments: 2,
        context: 3,
    },
    comments: {
        commentString: 'comment',
        props: {
            props: ['{', '}']
        },
        fallback: true
    }
};

and the following functions

const string1 = (worldPlace: string) => _(
    'Hello {PLACE}', 
    'Plural', 
    {
        comment: 'Comment', 
        props: {
            PLACE: 'The place of interest'
        }, 
        path: 'http://www.example.com', 
        nested: {
            key1: 'Key1',
            key2: 'Key2'
        }
    },
    'Context',
    {
        PLACE: worldPlace
    }
);
// when omitting the second argument the third argument can take the place of the second argument 
// if the arguments are of different type. If there are more arguments, they also change their
// place accordingly.
const string2 = _(
    'Foo',
    {
        comment: 'No Plural here.'
    }
);
// omit comment object
const string3 = _(
    'Foo2',
    'Plural',
    'Context'
);
// skip textPlural and comment object, allowed placeholders are `null`, `undefined` or `0`
const string4 = _(
    'Foo3',
    null,
    null,
    'Context'
);
// if argument is not string or comment object than rest of arguments are ignored
const string5 = (props: {PROPS: string}) => _(
    'My {PROPS}',
    {
        props: {
            PROPS: 'Some props'
        }
    },
    props
);

we extract the following messages

[
    {
        text: 'Hello {PLACE}',
        textPlural: 'Plural',
        comments: [
            'Comment',
            'path: http://www.example.com',
            '{PLACE}: The place of interest',
            'nested.key1: Key1',
            'nested.key2: Key2'
        ],
        context: 'Context'
    },
    {
        text: 'Foo',
        comments: [
            'No Plural here.'
        ],
    },
    {
        text: 'Foo2',
        textPlural: 'Plural',
        context: 'Context'
    },
    {
        text: 'Foo3',
        context: 'Context'
    },
    {
        text: 'My {PROPS}',
        comments: [
            '{PROPS}: Some props'
        ]
    }
]

If any argument is not a string or comment object then the parsing is cut off starting from this argument. If there are other arguments in between these arguments, their position is not considered in the fallback.

JS Node Parser for gettext-extractor

Extracts nodes provided by TTranslatorFunction or by providing TTranslatorFunction[] on each .parseString or .parseSvelteString method call.

import { nodeExtractor, TTranslatorFunction, FunctionExtractorBuilder, SvelteGettextExtractor } from 'gettext-extractor-svelte';

const functionExtractor = new FunctionExtractorBuilder();
const importDeclaration = functionExtractor.importDeclaration(
    './translations',
    functionExtractor.importClause(
        't'
    ),
    true,
);

const translatorFunctions: TTranslatorFunction[] = [
    {
        identifier: 'translationFunctionImport',
        functionName: 't',
        functionExtractor: importDeclaration
    }
];

const extractor = new SvelteGettextExtractor();

extractor.createJsParser()
    .addExtractor(nodeExtractor())
    .parseString('import t from "./translations";', 'src/app.js', {translatorFunctions});

extractor.getFunctions() === {
    'src/app.js': [
        {
            functionString: 'import t from "./translations";',
            functionData: {
                functionName: 't',
                functionArgs: []
            },
            startChar: 0,
            endChar: 31,
            identifier: 'translationFunctionImport',
            definition: true
        }
    ]
}; // true

nodeExtractor(translatorFunction)

Parameters

NameTypeDetails
translatorFunctionTTranslatorFunction or TTranslatorFunction[]See Translator Function Options below
Function Extractor Options
NameTypeDefaultDetails
functionExtractorFunctionExtractorThe function extractor describing the typescript nodes of the function to extract
identifierstringThe identifier under which the function will be added to the dict
restrictToFilestringWhen set than only the specified file will be parsed for the function

Function Extractor Builder

import {
    FunctionExtractorBuilder
} from 'gettext-extractor-svelte';

const functionExtractor = new FunctionExtractorBuilder();

Methods for FunctionExtractorBuilder

  objectLiteralExpression(properties?, getPos?)

Marks object literal expressions

Parameters

NameTypeDefaultDetails
propertiesFunctionExtractor []List of function extractors defining the properties of the object
getPosbooleanfalseReturn start and end position in call expression to extract node as string
Return Value

FunctionExtractor

  variableDeclaration(variableName, initializer?, getPos?)

Marks variable declarations

Parameters

NameTypeDefaultDetails
variableNamestringRequired · Name of declared variable
initializerFunctionExtractorFunction extractor defining the initializer
getPosbooleanfalseReturn start and end position in call expression to extract node as string
Return Value

FunctionExtractor

  propertyAssignment(keyName, initializer?, getPos?)

Marks property assignment

Parameters

NameTypeDefaultDetails
keyNamestringRequired · Name of property
initializerFunctionExtractorFunction extractor defining the initializer
getPosbooleanfalseReturn start and end position in call expression to extract node as string
Return Value

FunctionExtractor

  methodDeclaration(methodName, getPos?)

Marks method declaration

Parameters

NameTypeDefaultDetails
methodNamestringRequired · Name of method
getPosbooleanfalseReturn start and end position in call expression to extract node as string
Return Value

FunctionExtractor

  arrowFunction(getPos?)

Marks arrow function

Parameters

NameTypeDefaultDetails
getPosbooleanfalseReturn start and end position in call expression to extract node as string
Return Value

FunctionExtractor

  propertyDeclaration(propertyName, initializer?, getPos?)

Marks property declaration

Parameters

NameTypeDefaultDetails
propertyNamestringRequired · Name of property
propertiesFunctionExtractorFunction extractor defining the initializer
getPosbooleanfalseReturn start and end position in call expression to extract node as string
Return Value

FunctionExtractor

  functionDeclaration(functionName, getPos?)

Marks function declaration

Parameters

NameTypeDefaultDetails
functionNamestringRequired · Name of function declaration
getPosbooleanfalseReturn start and end position in call expression to extract node as string
Return Value

FunctionExtractor

  functionExpression(functionName?, getPos?)

Marks function expression

Parameters

NameTypeDefaultDetails
functionNamestringName of function expression
getPosbooleanfalseReturn start and end position in call expression to extract node as string
Return Value

FunctionExtractor

  classDeclaration(className, members?, getPos?)

Marks class declarations

Parameters

NameTypeDefaultDetails
classNamestringRequired · Name of class
membersFunctionExtractor[]Array of function extractors defining the members of the class
getPosbooleanfalseReturn start and end position in call expression to extract node as string
Return Value

FunctionExtractor

  classExpression(className?, members?, getPos?)

Marks class expression

Parameters

NameTypeDefaultDetails
classNamestringName of class
membersFunctionExtractor[]Array of function extractors defining the members of the class
getPosbooleanfalseReturn start and end position in call expression to extract node as string
Return Value

FunctionExtractor

  getAccessor(accessorName, body?, getPos?)

Marks get accessor

Parameters

NameTypeDefaultDetails
accessorNamestringRequired · Name of accessor
bodyFunctionExtractorFunction extractor defining the body of the get accessor
getPosbooleanfalseReturn start and end position in call expression to extract node as string
Return Value

FunctionExtractor

  setAccessor(accessorName, body?, getPos?)

Marks set accessor

Parameters

NameTypeDefaultDetails
accessorNamestringRequired · Name of accessor
bodyFunctionExtractorFunction extractor defining the body of the set accessor
getPosbooleanfalseReturn start and end position in call expression to extract node as string
Return Value

FunctionExtractor

  expressionStatement(identifier, right?, getPos?)

Marks expression statement

Parameters

NameTypeDefaultDetails
identifierstringRequired · Name of identifier on the left side of expression statement
rightFunctionExtractorFunction extractor defining the right side of the expression statement
getPosbooleanfalseReturn start and end position in call expression to extract node as string
Return Value

FunctionExtractor

  labeledStatement(statement?, getPos?)

Marks labeled statement

Parameters

NameTypeDefaultDetails
statementFunctionExtractorFunction extractor for defining the right side of the labeled statement
getPosbooleanfalseReturn start and end position in call expression to extract node as string
Return Value

FunctionExtractor

  importDeclaration(moduleSpecifier, importClause?, getPos?)

Marks import declaration

Parameters

NameTypeDefaultDetails
moduleSpecifierstringRequired · Name of module imported in this declaration
importClauseFunctionExtractorFunction extractor defining the import clause
getPosbooleanfalseReturn start and end position in call expression to extract node as string
Return Value

FunctionExtractor

  importClause(name?, elements?, getPos?)

Marks import clause

Parameters

NameTypeDefaultDetails
namestringName of main import in this clause
elementsFunctionExtractor[]List of function extractors defining the imported elements
getPosbooleanfalseReturn start and end position in call expression to extract node as string
Return Value

FunctionExtractor

  importSpecifier(name, getPos?)

Marks import specifier

Parameters

NameTypeDefaultDetails
namestringName of import in this specifier
getPosbooleanfalseReturn start and end position in call expression to extract node as string
Return Value

FunctionExtractor

  variableStatment(declarations?, getPos?)

Marks variable statement

Parameters

NameTypeDefaultDetails
declarationsFunctionExtractor[]List of variable declared in this statement
getPosbooleanfalseReturn start and end position in call expression to extract node as string
Return Value

FunctionExtractor

  newExpression(name, getPos?)

Marks new expression

Parameters

NameTypeDefaultDetails
namestringName of new expression
getPosbooleanfalseReturn start and end position in call expression to extract node as string
Return Value

FunctionExtractor

5.1.0

7 months ago

5.0.0

8 months ago

4.0.1

2 years ago

4.0.0

2 years ago

4.0.3

2 years ago

4.0.2

2 years ago

3.4.0

2 years ago

3.3.0

3 years ago

3.2.0

3 years ago

3.1.3

3 years ago

3.1.2

3 years ago

3.1.4

3 years ago

3.1.1

3 years ago

3.1.0

3 years ago

3.0.4

3 years ago

3.0.3

3 years ago

3.0.2

3 years ago

3.0.5

3 years ago

3.0.1

3 years ago

3.0.0

3 years ago

2.2.2

3 years ago

2.2.1

3 years ago

2.2.0

3 years ago

2.1.1

3 years ago

2.1.0

3 years ago

2.0.0

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago