Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

babel-plugin-i18next-extract

Package Overview
Dependencies
Maintainers
1
Versions
37
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

babel-plugin-i18next-extract - npm Package Compare versions

Comparing version 0.1.2 to 0.2.0

52

lib/index.es.js

@@ -306,2 +306,3 @@ import i18next from 'i18next';

ns: null,
defaultValue: null,
};

@@ -328,2 +329,9 @@ const countAttr = findJSXAttributeByName(path, 'count');

}
const defaultsAttr = findJSXAttributeByName(path, 'defaults');
if (defaultsAttr) {
let value = defaultsAttr.get('value');
if (value.isJSXExpressionContainer())
value = value.get('expression');
res.defaultValue = evaluateIfConfident(value);
}
return {

@@ -492,8 +500,11 @@ ...res,

return [];
const keyEvaluation = parseTransComponentKeyFromAttributes(path) ||
parseTransComponentKeyFromChildren(path);
const keyEvaluationFromAttribute = parseTransComponentKeyFromAttributes(path);
const keyEvaluationFromChildren = parseTransComponentKeyFromChildren(path);
const parsedOptions = parseTransComponentOptions(path, commentHints);
if (parsedOptions.defaultValue === null) {
parsedOptions.defaultValue = keyEvaluationFromChildren;
}
return [
{
key: keyEvaluation,
key: keyEvaluationFromAttribute || keyEvaluationFromChildren,
parsedOptions,

@@ -532,20 +543,26 @@ sourceNodes: [path.node],

ns: null,
defaultValue: null,
};
if (!path)
return res;
// Try brutal evaluation first.
// Try brutal evaluation of defaultValue first.
const optsEvaluation = evaluateIfConfident(path);
if (optsEvaluation !== null && typeof optsEvaluation === 'object') {
res.contexts = 'context' in optsEvaluation;
res.hasCount = 'count' in optsEvaluation;
const evaluatedNamespace = optsEvaluation['ns'];
res.ns = getFirstOrNull(evaluatedNamespace);
if (typeof optsEvaluation === 'string') {
res.defaultValue = optsEvaluation;
}
else if (path.isObjectExpression()) {
// It didn't work. Let's try to parse object expression keys.
// It didn't work. Let's try to parse as object expression.
res.contexts = findKeyInObjectExpression(path, 'context') !== null;
res.hasCount = findKeyInObjectExpression(path, 'count') !== null;
const nsNode = findKeyInObjectExpression(path, 'ns');
const nsNodeEvaluation = evaluateIfConfident(nsNode);
res.ns = getFirstOrNull(nsNodeEvaluation);
if (nsNode !== null && nsNode.isObjectProperty()) {
const nsValueNode = nsNode.get('value');
const nsEvaluation = evaluateIfConfident(nsValueNode);
res.ns = getFirstOrNull(nsEvaluation);
}
const defaultValueNode = findKeyInObjectExpression(path, 'defaultValue');
if (defaultValueNode !== null && defaultValueNode.isObjectProperty()) {
const defaultValueNodeValue = defaultValueNode.get('value');
res.defaultValue = evaluateIfConfident(defaultValueNodeValue);
}
}

@@ -1130,4 +1147,5 @@ return res;

function parseConfig(opts) {
const defaultLocales = ['en'];
return {
locales: coalesce(opts.locales, ['en']),
locales: coalesce(opts.locales, defaultLocales),
defaultNS: coalesce(opts.defaultNS, 'translation'),

@@ -1146,2 +1164,3 @@ pluralSeparator: coalesce(opts.pluralSeparator, '_'),

defaultValue: coalesce(opts.defaultValue, ''),
useI18nextDefaultValue: coalesce(opts.useI18nextDefaultValue, defaultLocales),
keyAsDefaultValue: coalesce(opts.keyAsDefaultValue, false),

@@ -1264,2 +1283,9 @@ keyAsDefaultValueForDerivedKeys: coalesce(opts.keyAsDefaultValueForDerivedKeys, true),

}
const useI18nextDefaultValueEnabled = config.useI18nextDefaultValue === true ||
(Array.isArray(config.useI18nextDefaultValue) &&
config.useI18nextDefaultValue.includes(locale));
if (useI18nextDefaultValueEnabled &&
key.parsedOptions.defaultValue !== null) {
defaultValue = key.parsedOptions.defaultValue;
}
return {

@@ -1266,0 +1292,0 @@ [JSON.stringify([...key.keyPath, key.cleanKey])]: defaultValue,

@@ -310,2 +310,3 @@ 'use strict';

ns: null,
defaultValue: null,
};

@@ -332,2 +333,9 @@ const countAttr = findJSXAttributeByName(path, 'count');

}
const defaultsAttr = findJSXAttributeByName(path, 'defaults');
if (defaultsAttr) {
let value = defaultsAttr.get('value');
if (value.isJSXExpressionContainer())
value = value.get('expression');
res.defaultValue = evaluateIfConfident(value);
}
return {

@@ -496,8 +504,11 @@ ...res,

return [];
const keyEvaluation = parseTransComponentKeyFromAttributes(path) ||
parseTransComponentKeyFromChildren(path);
const keyEvaluationFromAttribute = parseTransComponentKeyFromAttributes(path);
const keyEvaluationFromChildren = parseTransComponentKeyFromChildren(path);
const parsedOptions = parseTransComponentOptions(path, commentHints);
if (parsedOptions.defaultValue === null) {
parsedOptions.defaultValue = keyEvaluationFromChildren;
}
return [
{
key: keyEvaluation,
key: keyEvaluationFromAttribute || keyEvaluationFromChildren,
parsedOptions,

@@ -536,20 +547,26 @@ sourceNodes: [path.node],

ns: null,
defaultValue: null,
};
if (!path)
return res;
// Try brutal evaluation first.
// Try brutal evaluation of defaultValue first.
const optsEvaluation = evaluateIfConfident(path);
if (optsEvaluation !== null && typeof optsEvaluation === 'object') {
res.contexts = 'context' in optsEvaluation;
res.hasCount = 'count' in optsEvaluation;
const evaluatedNamespace = optsEvaluation['ns'];
res.ns = getFirstOrNull(evaluatedNamespace);
if (typeof optsEvaluation === 'string') {
res.defaultValue = optsEvaluation;
}
else if (path.isObjectExpression()) {
// It didn't work. Let's try to parse object expression keys.
// It didn't work. Let's try to parse as object expression.
res.contexts = findKeyInObjectExpression(path, 'context') !== null;
res.hasCount = findKeyInObjectExpression(path, 'count') !== null;
const nsNode = findKeyInObjectExpression(path, 'ns');
const nsNodeEvaluation = evaluateIfConfident(nsNode);
res.ns = getFirstOrNull(nsNodeEvaluation);
if (nsNode !== null && nsNode.isObjectProperty()) {
const nsValueNode = nsNode.get('value');
const nsEvaluation = evaluateIfConfident(nsValueNode);
res.ns = getFirstOrNull(nsEvaluation);
}
const defaultValueNode = findKeyInObjectExpression(path, 'defaultValue');
if (defaultValueNode !== null && defaultValueNode.isObjectProperty()) {
const defaultValueNodeValue = defaultValueNode.get('value');
res.defaultValue = evaluateIfConfident(defaultValueNodeValue);
}
}

@@ -1134,4 +1151,5 @@ return res;

function parseConfig(opts) {
const defaultLocales = ['en'];
return {
locales: coalesce(opts.locales, ['en']),
locales: coalesce(opts.locales, defaultLocales),
defaultNS: coalesce(opts.defaultNS, 'translation'),

@@ -1150,2 +1168,3 @@ pluralSeparator: coalesce(opts.pluralSeparator, '_'),

defaultValue: coalesce(opts.defaultValue, ''),
useI18nextDefaultValue: coalesce(opts.useI18nextDefaultValue, defaultLocales),
keyAsDefaultValue: coalesce(opts.keyAsDefaultValue, false),

@@ -1268,2 +1287,9 @@ keyAsDefaultValueForDerivedKeys: coalesce(opts.keyAsDefaultValueForDerivedKeys, true),

}
const useI18nextDefaultValueEnabled = config.useI18nextDefaultValue === true ||
(Array.isArray(config.useI18nextDefaultValue) &&
config.useI18nextDefaultValue.includes(locale));
if (useI18nextDefaultValueEnabled &&
key.parsedOptions.defaultValue !== null) {
defaultValue = key.parsedOptions.defaultValue;
}
return {

@@ -1270,0 +1296,0 @@ [JSON.stringify([...key.keyPath, key.cleanKey])]: defaultValue,

@@ -13,2 +13,3 @@ export interface Config {

defaultValue: string | null;
useI18nextDefaultValue: boolean | string[];
keyAsDefaultValue: boolean | string[];

@@ -15,0 +16,0 @@ keyAsDefaultValueForDerivedKeys: boolean;

@@ -50,3 +50,3 @@ import * as BabelCore from '@babel/core';

*/
export declare function findKeyInObjectExpression(path: BabelCore.NodePath<BabelTypes.ObjectExpression>, key: string): BabelCore.NodePath | null;
export declare function findKeyInObjectExpression(path: BabelCore.NodePath<BabelTypes.ObjectExpression>, key: string): BabelCore.NodePath<BabelTypes.ObjectExpression['properties'][0]> | null;
/**

@@ -53,0 +53,0 @@ * Find a JSX attribute given its name.

@@ -7,2 +7,3 @@ import * as BabelTypes from '@babel/types';

ns: string | null;
defaultValue: string | null;
}

@@ -9,0 +10,0 @@ /**

{
"name": "babel-plugin-i18next-extract",
"version": "0.1.2",
"version": "0.2.0",
"description": "Statically extract translation keys from i18next application.",

@@ -5,0 +5,0 @@ "repository": {

@@ -94,3 +94,4 @@ # babel-plugin-i18next-extract

| defaultValue | `string` or `null` | Default value for extracted keys. | `''` (empty string) |
| keyAsDefaultValue | `boolean` or `string[]` | If true, use the extracted key as defaultValue (ignoring `defaultValue` option). You can also specify an array of locales to apply this behavior only to a specific set locales (e.g. if you keys are in plain english, you may want to set this option to `['en']`). | `false` |
| useI18nextDefaultValue | `boolean` or `string[]` | If true and a [i18next default value](https://www.i18next.com/translation-function/essentials#passing-a-default-value) is set for the key, use this default value (ignoring `defaultValue` option). You can also specify an array of locales to apply this behavior only to a specific set locales (e.g. if your i18next default values are in plain french, you may want to set this option to `['fr']`). Note: for `react-i18next` `Trans` component, the children might also be used as default value. | `['en']` |
| keyAsDefaultValue | `boolean` or `string[]` | If true, use the extracted key as defaultValue (ignoring `defaultValue` option). You can also specify an array of locales to apply this behavior only to a specific set locales (e.g. if your keys are in plain english, you may want to set this option to `['en']`). | `false` |
| keyAsDefaultValueForDerivedKeys | `boolean` | If false and `keyAsDefaultValue` is enabled, don't use derived keys (plural forms or contexts) as default value. `defaultValue` will be used instead. | `true` |

@@ -97,0 +98,0 @@ | discardOldKeys | `boolean` | When set to `true`, keys that no longer exist are removed from the JSON files. By default, new keys will be added to the JSON files and never removed. | `false` |

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc