@inlang/paraglide-js 1.7.3 → 1.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +0 -3
- package/dist/cli/index.js +2 -2
- package/dist/compiler/aliases.d.ts +9 -0
- package/dist/compiler/compile.d.ts +16 -6
- package/dist/compiler/compileMessage.d.ts +19 -2
- package/dist/compiler/messageIndex.d.ts +8 -0
- package/dist/compiler/runtime.d.ts +6 -0
- package/dist/index.js +252 -182
- package/package.json +5 -5
package/README.md
CHANGED
|
@@ -412,6 +412,3 @@ If you are working with translators or designers you will find these tools usefu
|
|
|
412
412
|
- [Fink](https://inlang.com/m/tdozzpar/app-inlang-finkLocalizationEditor) - An Online UI for editing translations. Changes made in Fink are committed to a translation branch or submitted via pull request.
|
|
413
413
|
- [Parrot](https://inlang.com/m/gkrpgoir/app-parrot-figmaPlugin) - A Figma Plugin for previewing translations right in your Figma designs. This avoids any layout issues that might occur due to different text lengths in different languages.
|
|
414
414
|
|
|
415
|
-
# Pricing
|
|
416
|
-
|
|
417
|
-
<doc-pricing></doc-pricing>
|
package/dist/cli/index.js
CHANGED
|
@@ -32234,7 +32234,7 @@ const addParaglideJsToDevDependencies = async (ctx) => {
|
|
|
32234
32234
|
const ctx1 = await updatePackageJson({
|
|
32235
32235
|
devDependencies: async (devDeps) => ({
|
|
32236
32236
|
...devDeps,
|
|
32237
|
-
"@inlang/paraglide-js": "1.
|
|
32237
|
+
"@inlang/paraglide-js": "1.8.0"
|
|
32238
32238
|
})
|
|
32239
32239
|
})(ctx);
|
|
32240
32240
|
ctx.logger.success("Added @inlang/paraglide-js to the devDependencies in package.json.");
|
|
@@ -32321,7 +32321,7 @@ const steps = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.definePropert
|
|
|
32321
32321
|
runCompiler,
|
|
32322
32322
|
updatePackageJson
|
|
32323
32323
|
}, Symbol.toStringTag, { value: "Module" }));
|
|
32324
|
-
const cli = new Command().name("paraglide-js").addCommand(compileCommand).addCommand(initCommand).showHelpAfterError().version("1.
|
|
32324
|
+
const cli = new Command().name("paraglide-js").addCommand(compileCommand).addCommand(initCommand).showHelpAfterError().version("1.8.0");
|
|
32325
32325
|
export {
|
|
32326
32326
|
defaults as Defaults,
|
|
32327
32327
|
steps as Steps,
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { Message } from '@inlang/sdk';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Returns re-export statements for each alias of a message.
|
|
5
|
+
* If no aliases are present, this function returns an empty string.
|
|
6
|
+
*
|
|
7
|
+
* @param message
|
|
8
|
+
*/
|
|
9
|
+
export declare function reexportAliases(message: Message): string;
|
|
@@ -1,5 +1,16 @@
|
|
|
1
|
-
import { ProjectSettings, Message } from '@inlang/sdk';
|
|
1
|
+
import { ProjectSettings, Message, LanguageTag } from '@inlang/sdk';
|
|
2
2
|
|
|
3
|
+
export type CompileOptions = {
|
|
4
|
+
messages: Readonly<Message[]>;
|
|
5
|
+
settings: ProjectSettings;
|
|
6
|
+
projectId?: string | undefined;
|
|
7
|
+
/**
|
|
8
|
+
* The file-structure of the compiled output.
|
|
9
|
+
*
|
|
10
|
+
* @default "regular"
|
|
11
|
+
*/
|
|
12
|
+
outputStructure?: "regular" | "message-modules";
|
|
13
|
+
};
|
|
3
14
|
/**
|
|
4
15
|
* A compile function takes a list of messages and project settings and returns
|
|
5
16
|
* a map of file names to file contents.
|
|
@@ -9,8 +20,7 @@ import { ProjectSettings, Message } from '@inlang/sdk';
|
|
|
9
20
|
* console.log(output)
|
|
10
21
|
* >> { "messages.js": "...", "runtime.js": "..." }
|
|
11
22
|
*/
|
|
12
|
-
export declare const compile: (args:
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
}) => Promise<Record<string, string>>;
|
|
23
|
+
export declare const compile: (args: CompileOptions) => Promise<Record<string, string>>;
|
|
24
|
+
export declare function getFallbackMap(languageTags: LanguageTag[], sourceLanguageTag: LanguageTag): {
|
|
25
|
+
[k: string]: string | undefined;
|
|
26
|
+
};
|
|
@@ -1,8 +1,25 @@
|
|
|
1
|
+
import { Params } from './paramsType.js';
|
|
1
2
|
import { LanguageTag, Message } from '@inlang/sdk';
|
|
2
3
|
|
|
3
4
|
type Resource = {
|
|
5
|
+
/**
|
|
6
|
+
* The original message
|
|
7
|
+
*/
|
|
8
|
+
source: Message;
|
|
9
|
+
/**
|
|
10
|
+
* The parameters needed for this message
|
|
11
|
+
*/
|
|
12
|
+
params: Params;
|
|
13
|
+
/**
|
|
14
|
+
* The index-message function for this message
|
|
15
|
+
*/
|
|
4
16
|
index: string;
|
|
5
|
-
|
|
17
|
+
/**
|
|
18
|
+
* The message-function for each language
|
|
19
|
+
*/
|
|
20
|
+
translations: {
|
|
21
|
+
[languageTag: string]: string;
|
|
22
|
+
};
|
|
6
23
|
};
|
|
7
24
|
/**
|
|
8
25
|
* Returns the compiled messages for the given message.
|
|
@@ -17,5 +34,5 @@ type Resource = {
|
|
|
17
34
|
* @param message The message to compile
|
|
18
35
|
* @param lookupTable A table that maps language tags to their fallbacks.
|
|
19
36
|
*/
|
|
20
|
-
export declare const compileMessage: (message: Message,
|
|
37
|
+
export declare const compileMessage: (message: Message, fallbackMap: Record<LanguageTag, LanguageTag | undefined>, output?: "regular" | "message-modules") => Resource;
|
|
21
38
|
export {};
|
package/dist/index.js
CHANGED
|
@@ -10260,12 +10260,36 @@ const paramsType = (params, isMessagesIndex) => {
|
|
|
10260
10260
|
}
|
|
10261
10261
|
return `@param {{ ${fieldTypes.join(", ")} }} params`;
|
|
10262
10262
|
};
|
|
10263
|
-
function
|
|
10264
|
-
|
|
10263
|
+
function reexportAliases(message) {
|
|
10264
|
+
let code = "";
|
|
10265
|
+
if (message.alias["default"] && message.id !== message.alias["default"]) {
|
|
10266
|
+
code += `
|
|
10267
|
+
/**
|
|
10268
|
+
* Change the reference from the alias \`m.${message.alias["default"]}()\` to \`m.${message.id}()\`:
|
|
10269
|
+
* \`\`\`diff
|
|
10270
|
+
* - m.${message.alias["default"]}()
|
|
10271
|
+
* + m.${message.id}()
|
|
10272
|
+
* \`\`\`
|
|
10273
|
+
* ---
|
|
10274
|
+
* \`${message.alias["default"]}\` is an alias for the message \`${message.id}\`.
|
|
10275
|
+
* Referencing aliases instead of the message ID has downsides like:
|
|
10276
|
+
*
|
|
10277
|
+
* - The alias might be renamed in the future, breaking the code.
|
|
10278
|
+
* - Constant naming convention discussions.
|
|
10279
|
+
*
|
|
10280
|
+
* Read more about aliases and their downsides here
|
|
10281
|
+
* @see inlang.com/link.
|
|
10282
|
+
* ---
|
|
10283
|
+
* @deprecated reference the message by id \`m.${message.id}()\` instead
|
|
10284
|
+
*
|
|
10285
|
+
* @param {Parameters<typeof ${message.id}>} args
|
|
10286
|
+
* @returns {ReturnType<typeof ${message.id}>}
|
|
10287
|
+
*/
|
|
10288
|
+
export const ${message.alias["default"]} = (...args) => ${message.id}(...args);
|
|
10289
|
+
`;
|
|
10290
|
+
}
|
|
10291
|
+
return code;
|
|
10265
10292
|
}
|
|
10266
|
-
const optionsType = (args) => {
|
|
10267
|
-
return `@param {{ languageTag?: ${toStringUnion(args.languageTags) ?? "undefined"} }} options`;
|
|
10268
|
-
};
|
|
10269
10293
|
function i(str) {
|
|
10270
10294
|
var _a;
|
|
10271
10295
|
str = str.replaceAll(/[^a-zA-Z0-9_]/g, "_");
|
|
@@ -10274,7 +10298,36 @@ function i(str) {
|
|
|
10274
10298
|
}
|
|
10275
10299
|
return str;
|
|
10276
10300
|
}
|
|
10277
|
-
|
|
10301
|
+
function toStringUnion(iterable) {
|
|
10302
|
+
return [...iterable].map((item) => `"${escapeForDoubleQuoteString(item)}"`).join(" | ");
|
|
10303
|
+
}
|
|
10304
|
+
const optionsType = (args) => {
|
|
10305
|
+
return `@param {{ languageTag?: ${toStringUnion(args.languageTags) ?? "undefined"} }} options`;
|
|
10306
|
+
};
|
|
10307
|
+
const messageIndexFunction = (args) => {
|
|
10308
|
+
const hasParams = Object.keys(args.params).length > 0;
|
|
10309
|
+
return `/**
|
|
10310
|
+
* This message has been compiled by [inlang paraglide](https://inlang.com/m/gerre34r/library-inlang-paraglideJs).
|
|
10311
|
+
*
|
|
10312
|
+
* - Don't edit the message's code. Use [Sherlock (VS Code extension)](https://inlang.com/m/r7kp499g/app-inlang-ideExtension),
|
|
10313
|
+
* the [web editor](https://inlang.com/m/tdozzpar/app-inlang-finkLocalizationEditor) instead, or edit the translation files manually.
|
|
10314
|
+
*
|
|
10315
|
+
* - The params are NonNullable<unknown> because the inlang SDK does not provide information on the type of a param (yet).
|
|
10316
|
+
*
|
|
10317
|
+
* ${paramsType(args.params, true)}
|
|
10318
|
+
* ${optionsType({ languageTags: args.availableLanguageTags })}
|
|
10319
|
+
* @returns {string}
|
|
10320
|
+
*/
|
|
10321
|
+
/* @__NO_SIDE_EFFECTS__ */
|
|
10322
|
+
export const ${args.message.id} = (params ${hasParams ? "" : "= {}"}, options = {}) => {
|
|
10323
|
+
return {
|
|
10324
|
+
${args.availableLanguageTags.sort((a, b) => a.localeCompare(b)).map((tag) => ` ${isValidJSIdentifier(tag) ? tag : `"${tag}"`}: ${i(tag)}.${args.message.id}`).join(",\n")}
|
|
10325
|
+
}[options.languageTag ?? languageTag()](${hasParams ? "params" : ""})
|
|
10326
|
+
}
|
|
10327
|
+
${reexportAliases(args.message)}
|
|
10328
|
+
`;
|
|
10329
|
+
};
|
|
10330
|
+
const compileMessage = (message, fallbackMap, output = "regular") => {
|
|
10278
10331
|
if (!isValidJSIdentifier(message.id)) {
|
|
10279
10332
|
throw new Error(
|
|
10280
10333
|
`Cannot compile message with ID "${message.id}".
|
|
@@ -10292,115 +10345,61 @@ To detect this issue during linting, use the valid-js-identifier lint rule: http
|
|
|
10292
10345
|
`Duplicate language tag: ${variant.languageTag}. Multiple variants for one language tag are not supported in paraglide yet. `
|
|
10293
10346
|
);
|
|
10294
10347
|
}
|
|
10295
|
-
if (!availableLanguageTags.includes(variant.languageTag)) {
|
|
10296
|
-
throw new Error(
|
|
10297
|
-
`The language tag "${variant.languageTag}" is not included in the project's language tags but contained in of your messages. Please add the language tag to your project's language tags or delete the messages with the language tag "${variant.languageTag}" to avoid unexpected type errors.`
|
|
10298
|
-
);
|
|
10299
|
-
}
|
|
10300
10348
|
const { compiled, params: variantParams } = compilePattern(variant.pattern);
|
|
10301
10349
|
params = { ...params, ...variantParams };
|
|
10302
10350
|
compiledPatterns[variant.languageTag] = compiled;
|
|
10303
10351
|
}
|
|
10304
10352
|
const resource = {
|
|
10305
|
-
|
|
10353
|
+
source: message,
|
|
10354
|
+
params,
|
|
10355
|
+
index: messageIndexFunction({
|
|
10356
|
+
message,
|
|
10357
|
+
params,
|
|
10358
|
+
availableLanguageTags: Object.keys(fallbackMap)
|
|
10359
|
+
}),
|
|
10360
|
+
translations: Object.fromEntries(
|
|
10361
|
+
Object.entries(fallbackMap).map(([languageTag, fallbackLanguage]) => {
|
|
10362
|
+
const compiledPattern = compiledPatterns[languageTag];
|
|
10363
|
+
return [
|
|
10364
|
+
languageTag,
|
|
10365
|
+
compiledPattern ? messageFunction({
|
|
10366
|
+
message,
|
|
10367
|
+
params,
|
|
10368
|
+
languageTag,
|
|
10369
|
+
compiledPattern
|
|
10370
|
+
}) : fallbackLanguage ? reexportMessage(message, fallbackLanguage, output) : messageIdFallback(message, languageTag)
|
|
10371
|
+
];
|
|
10372
|
+
})
|
|
10373
|
+
)
|
|
10306
10374
|
};
|
|
10307
|
-
for (const languageTag of availableLanguageTags) {
|
|
10308
|
-
const compiledPattern = compiledPatterns[languageTag];
|
|
10309
|
-
if (compiledPattern) {
|
|
10310
|
-
resource[languageTag] = messageFunction({ message, params, languageTag, compiledPattern });
|
|
10311
|
-
} else {
|
|
10312
|
-
const fallbackLanguage = lookup(languageTag, {
|
|
10313
|
-
languageTags: Object.keys(compiledPatterns),
|
|
10314
|
-
defaultLanguageTag: sourceLanguageTag
|
|
10315
|
-
});
|
|
10316
|
-
const compiledFallbackPattern = compiledPatterns[fallbackLanguage];
|
|
10317
|
-
resource[languageTag] = compiledFallbackPattern ? reexportMessage(message, fallbackLanguage) : messageIdFallback(message, languageTag);
|
|
10318
|
-
}
|
|
10319
|
-
}
|
|
10320
10375
|
return resource;
|
|
10321
10376
|
};
|
|
10322
|
-
const messageIndexFunction = (args) => {
|
|
10323
|
-
const hasParams = Object.keys(args.params).length > 0;
|
|
10324
|
-
return `/**
|
|
10325
|
-
* This message has been compiled by [inlang paraglide](https://inlang.com/m/gerre34r/library-inlang-paraglideJs).
|
|
10326
|
-
*
|
|
10327
|
-
* - Don't edit the message's code. Use [Sherlock (VS Code extension)](https://inlang.com/m/r7kp499g/app-inlang-ideExtension),
|
|
10328
|
-
* the [web editor](https://inlang.com/m/tdozzpar/app-inlang-finkLocalizationEditor) instead, or edit the translation files manually.
|
|
10329
|
-
*
|
|
10330
|
-
* - The params are NonNullable<unknown> because the inlang SDK does not provide information on the type of a param (yet).
|
|
10331
|
-
*
|
|
10332
|
-
* ${paramsType(args.params, true)}
|
|
10333
|
-
* ${optionsType({ languageTags: args.availableLanguageTags })}
|
|
10334
|
-
* @returns {string}
|
|
10335
|
-
*/
|
|
10336
|
-
/* @__NO_SIDE_EFFECTS__ */
|
|
10337
|
-
export const ${args.message.id} = (params ${hasParams ? "" : "= {}"}, options = {}) => {
|
|
10338
|
-
return {
|
|
10339
|
-
${args.availableLanguageTags.sort((a, b) => a.localeCompare(b)).map((tag) => ` ${isValidJSIdentifier(tag) ? tag : `"${tag}"`}: ${i(tag)}.${args.message.id}`).join(",\n")}
|
|
10340
|
-
}[options.languageTag ?? languageTag()](${hasParams ? "params" : ""})
|
|
10341
|
-
}
|
|
10342
|
-
${reexportAliases(args.message)}
|
|
10343
|
-
`;
|
|
10344
|
-
};
|
|
10345
10377
|
const messageFunction = (args) => {
|
|
10346
10378
|
const hasParams = Object.keys(args.params).length > 0;
|
|
10347
|
-
return
|
|
10348
|
-
|
|
10349
|
-
/**
|
|
10379
|
+
return `/**
|
|
10350
10380
|
* ${paramsType(args.params, false)}
|
|
10351
10381
|
* @returns {string}
|
|
10352
10382
|
*/
|
|
10353
10383
|
/* @__NO_SIDE_EFFECTS__ */
|
|
10354
10384
|
export const ${args.message.id} = (${hasParams ? "params" : ""}) => ${args.compiledPattern}
|
|
10355
|
-
${reexportAliases(args.message)}
|
|
10356
|
-
`;
|
|
10385
|
+
${reexportAliases(args.message)}`;
|
|
10357
10386
|
};
|
|
10358
|
-
function reexportMessage(message, fromLanguageTag) {
|
|
10387
|
+
function reexportMessage(message, fromLanguageTag, output) {
|
|
10359
10388
|
const exports2 = [message.id];
|
|
10360
10389
|
if (message.alias["default"] && message.id !== message.alias["default"]) {
|
|
10361
10390
|
exports2.push(message.alias["default"]);
|
|
10362
10391
|
}
|
|
10363
|
-
|
|
10392
|
+
const from = output === "message-modules" ? `../${fromLanguageTag}.js` : `./${fromLanguageTag}.js`;
|
|
10393
|
+
return `export { ${exports2.join(", ")} } from "${from}"`;
|
|
10364
10394
|
}
|
|
10365
10395
|
function messageIdFallback(message, languageTag) {
|
|
10366
10396
|
return `/**
|
|
10367
|
-
* Failed to resolve message ${message.id} for languageTag "${languageTag}".
|
|
10368
|
-
* @returns {string}
|
|
10369
|
-
*/
|
|
10397
|
+
* Failed to resolve message ${message.id} for languageTag "${languageTag}".
|
|
10398
|
+
* @returns {string}
|
|
10399
|
+
*/
|
|
10370
10400
|
/* @__NO_SIDE_EFFECTS__ */
|
|
10371
10401
|
export const ${message.id} = () => "${escapeForDoubleQuoteString(message.id)}"
|
|
10372
|
-
${reexportAliases(message)}
|
|
10373
|
-
`;
|
|
10374
|
-
}
|
|
10375
|
-
function reexportAliases(message) {
|
|
10376
|
-
let code = "";
|
|
10377
|
-
if (message.alias["default"] && message.id !== message.alias["default"]) {
|
|
10378
|
-
code += `
|
|
10379
|
-
/**
|
|
10380
|
-
* Change the reference from the alias \`m.${message.alias["default"]}()\` to \`m.${message.id}()\`:
|
|
10381
|
-
* \`\`\`diff
|
|
10382
|
-
* - m.${message.alias["default"]}()
|
|
10383
|
-
* + m.${message.id}()
|
|
10384
|
-
* \`\`\`
|
|
10385
|
-
* ---
|
|
10386
|
-
* \`${message.alias["default"]}\` is an alias for the message \`${message.id}\`.
|
|
10387
|
-
* Referencing aliases instead of the message ID has downsides like:
|
|
10388
|
-
*
|
|
10389
|
-
* - The alias might be renamed in the future, breaking the code.
|
|
10390
|
-
* - Constant naming convention discussions.
|
|
10391
|
-
*
|
|
10392
|
-
* Read more about aliases and their downsides here
|
|
10393
|
-
* @see inlang.com/link.
|
|
10394
|
-
* ---
|
|
10395
|
-
* @deprecated reference the message by id \`m.${message.id}()\` instead
|
|
10396
|
-
*
|
|
10397
|
-
* @param {Parameters<typeof ${message.id}>} args
|
|
10398
|
-
* @returns {ReturnType<typeof ${message.id}>}
|
|
10399
|
-
*/
|
|
10400
|
-
export const ${message.alias["default"]} = (...args) => ${message.id}(...args);
|
|
10401
|
-
`;
|
|
10402
|
-
}
|
|
10403
|
-
return code;
|
|
10402
|
+
${reexportAliases(message)}`;
|
|
10404
10403
|
}
|
|
10405
10404
|
var main$1 = { exports: {} };
|
|
10406
10405
|
const name = "dotenv";
|
|
@@ -14838,74 +14837,8 @@ async function getPackageJson(fs2, cwd) {
|
|
|
14838
14837
|
return void 0;
|
|
14839
14838
|
}
|
|
14840
14839
|
}
|
|
14841
|
-
|
|
14842
|
-
|
|
14843
|
-
*
|
|
14844
|
-
`;
|
|
14845
|
-
const compile = async (args) => {
|
|
14846
|
-
const compiledMessages = args.messages.map(
|
|
14847
|
-
(message) => compileMessage(message, args.settings.languageTags, args.settings.sourceLanguageTag)
|
|
14848
|
-
);
|
|
14849
|
-
const pkgJson = await getPackageJson(nodeFsPromises, process.cwd());
|
|
14850
|
-
const stack = getStackInfo(pkgJson);
|
|
14851
|
-
telemetry.capture(
|
|
14852
|
-
{
|
|
14853
|
-
event: "PARAGLIDE-JS compile executed",
|
|
14854
|
-
properties: {
|
|
14855
|
-
stack
|
|
14856
|
-
}
|
|
14857
|
-
},
|
|
14858
|
-
args.projectId
|
|
14859
|
-
);
|
|
14860
|
-
const resources = {};
|
|
14861
|
-
for (const compiledMessage of compiledMessages) {
|
|
14862
|
-
for (const languageTag of Object.keys(compiledMessage)) {
|
|
14863
|
-
if (languageTag === "index")
|
|
14864
|
-
continue;
|
|
14865
|
-
if (!resources[languageTag])
|
|
14866
|
-
resources[languageTag] = "";
|
|
14867
|
-
resources[languageTag] += "\n\n" + compiledMessage[languageTag];
|
|
14868
|
-
}
|
|
14869
|
-
}
|
|
14870
|
-
const languagesWithMessages = new Set(Object.keys(resources));
|
|
14871
|
-
const languagesWithoutMessages = args.settings.languageTags.filter(
|
|
14872
|
-
(languageTag) => !languagesWithMessages.has(languageTag)
|
|
14873
|
-
);
|
|
14874
|
-
for (const languageTag of languagesWithoutMessages) {
|
|
14875
|
-
if (!resources[languageTag])
|
|
14876
|
-
resources[languageTag] = "\n\nexport {};";
|
|
14877
|
-
}
|
|
14878
|
-
telemetry.shutdown();
|
|
14879
|
-
return {
|
|
14880
|
-
// boilerplate files
|
|
14881
|
-
".prettierignore": ignoreDirectory,
|
|
14882
|
-
".gitignore": ignoreDirectory,
|
|
14883
|
-
...Object.fromEntries(
|
|
14884
|
-
Object.entries(resources).map(([languageTag, content]) => [
|
|
14885
|
-
`messages/${languageTag}.js`,
|
|
14886
|
-
`
|
|
14887
|
-
/* eslint-disable */
|
|
14888
|
-
/**
|
|
14889
|
-
* This file contains language specific message functions for tree-shaking.
|
|
14890
|
-
*
|
|
14891
|
-
*! WARNING: Only import messages from this file if you want to manually
|
|
14892
|
-
*! optimize your bundle. Else, import from the \`messages.js\` file.
|
|
14893
|
-
*
|
|
14894
|
-
* Your bundler will (in the future) automatically replace the index function
|
|
14895
|
-
* with a language specific message function in the build step.
|
|
14896
|
-
*/` + content
|
|
14897
|
-
])
|
|
14898
|
-
),
|
|
14899
|
-
// message index file
|
|
14900
|
-
"messages.js": `
|
|
14901
|
-
/* eslint-disable */
|
|
14902
|
-
import { languageTag } from "./runtime.js"
|
|
14903
|
-
${Object.keys(resources).map((languageTag) => `import * as ${i(languageTag)} from "./messages/${languageTag}.js"`).join("\n")}
|
|
14904
|
-
|
|
14905
|
-
${compiledMessages.map((message) => message.index).join("\n\n")}
|
|
14906
|
-
`,
|
|
14907
|
-
"runtime.js": `
|
|
14908
|
-
/* eslint-disable */
|
|
14840
|
+
function createRuntime(opts) {
|
|
14841
|
+
return `/* eslint-disable */
|
|
14909
14842
|
/** @type {((tag: AvailableLanguageTag) => void) | undefined} */
|
|
14910
14843
|
let _onSetLanguageTag
|
|
14911
14844
|
|
|
@@ -14918,7 +14851,7 @@ let _onSetLanguageTag
|
|
|
14918
14851
|
* return
|
|
14919
14852
|
* }
|
|
14920
14853
|
*/
|
|
14921
|
-
export const sourceLanguageTag = "${
|
|
14854
|
+
export const sourceLanguageTag = "${opts.sourceLanguageTag}"
|
|
14922
14855
|
|
|
14923
14856
|
/**
|
|
14924
14857
|
* The project's available language tags.
|
|
@@ -14928,9 +14861,7 @@ export const sourceLanguageTag = "${args.settings.sourceLanguageTag}"
|
|
|
14928
14861
|
* throw new Error("Language tag not available")
|
|
14929
14862
|
* }
|
|
14930
14863
|
*/
|
|
14931
|
-
export const availableLanguageTags = /** @type {const} */ (${JSON.stringify(
|
|
14932
|
-
args.settings.languageTags
|
|
14933
|
-
)})
|
|
14864
|
+
export const availableLanguageTags = /** @type {const} */ (${JSON.stringify(opts.languageTags)})
|
|
14934
14865
|
|
|
14935
14866
|
/**
|
|
14936
14867
|
* Get the current language tag.
|
|
@@ -14965,15 +14896,15 @@ export let languageTag = () => sourceLanguageTag
|
|
|
14965
14896
|
* @param {AvailableLanguageTag | (() => AvailableLanguageTag)} tag
|
|
14966
14897
|
*/
|
|
14967
14898
|
export const setLanguageTag = (tag) => {
|
|
14968
|
-
|
|
14969
|
-
|
|
14970
|
-
|
|
14971
|
-
|
|
14972
|
-
|
|
14973
|
-
|
|
14974
|
-
|
|
14975
|
-
|
|
14976
|
-
|
|
14899
|
+
if (typeof tag === "function") {
|
|
14900
|
+
languageTag = enforceLanguageTag(tag)
|
|
14901
|
+
} else {
|
|
14902
|
+
languageTag = enforceLanguageTag(() => tag)
|
|
14903
|
+
}
|
|
14904
|
+
// call the callback function if it has been defined
|
|
14905
|
+
if (_onSetLanguageTag !== undefined) {
|
|
14906
|
+
_onSetLanguageTag(languageTag())
|
|
14907
|
+
}
|
|
14977
14908
|
}
|
|
14978
14909
|
|
|
14979
14910
|
/**
|
|
@@ -14982,13 +14913,13 @@ export const setLanguageTag = (tag) => {
|
|
|
14982
14913
|
* @returns {() => AvailableLanguageTag}
|
|
14983
14914
|
*/
|
|
14984
14915
|
function enforceLanguageTag(unsafeLanguageTag) {
|
|
14985
|
-
|
|
14986
|
-
|
|
14987
|
-
|
|
14988
|
-
|
|
14989
|
-
|
|
14990
|
-
|
|
14991
|
-
|
|
14916
|
+
return () => {
|
|
14917
|
+
const tag = unsafeLanguageTag()
|
|
14918
|
+
if(!isAvailableLanguageTag(tag)) {
|
|
14919
|
+
throw new Error(\`languageTag() didn't return a valid language tag. Check your setLanguageTag call\`)
|
|
14920
|
+
}
|
|
14921
|
+
return tag
|
|
14922
|
+
}
|
|
14992
14923
|
}
|
|
14993
14924
|
|
|
14994
14925
|
/**
|
|
@@ -15018,7 +14949,7 @@ function enforceLanguageTag(unsafeLanguageTag) {
|
|
|
15018
14949
|
* @param {(languageTag: AvailableLanguageTag) => void} fn
|
|
15019
14950
|
*/
|
|
15020
14951
|
export const onSetLanguageTag = (fn) => {
|
|
15021
|
-
|
|
14952
|
+
_onSetLanguageTag = fn
|
|
15022
14953
|
}
|
|
15023
14954
|
|
|
15024
14955
|
/**
|
|
@@ -15035,7 +14966,7 @@ export const onSetLanguageTag = (fn) => {
|
|
|
15035
14966
|
* @returns {thing is AvailableLanguageTag}
|
|
15036
14967
|
*/
|
|
15037
14968
|
export function isAvailableLanguageTag(thing) {
|
|
15038
|
-
|
|
14969
|
+
return availableLanguageTags.includes(thing)
|
|
15039
14970
|
}
|
|
15040
14971
|
|
|
15041
14972
|
// ------ TYPES ------
|
|
@@ -15047,10 +14978,149 @@ export function isAvailableLanguageTag(thing) {
|
|
|
15047
14978
|
* setLanguageTag(request.languageTag as AvailableLanguageTag)
|
|
15048
14979
|
*
|
|
15049
14980
|
* @typedef {typeof availableLanguageTags[number]} AvailableLanguageTag
|
|
15050
|
-
|
|
15051
|
-
|
|
14981
|
+
*/`;
|
|
14982
|
+
}
|
|
14983
|
+
const ignoreDirectory = `# ignore everything because the directory is auto-generated by inlang paraglide-js
|
|
14984
|
+
# for more info visit https://inlang.com/m/gerre34r/paraglide-js
|
|
14985
|
+
*
|
|
14986
|
+
`;
|
|
14987
|
+
const defaultCompileOptions = {
|
|
14988
|
+
projectId: void 0,
|
|
14989
|
+
outputStructure: "regular"
|
|
14990
|
+
};
|
|
14991
|
+
const compile = async (args) => {
|
|
14992
|
+
const opts = {
|
|
14993
|
+
...defaultCompileOptions,
|
|
14994
|
+
...args
|
|
14995
|
+
};
|
|
14996
|
+
const fallbackMap = getFallbackMap(
|
|
14997
|
+
opts.settings.languageTags,
|
|
14998
|
+
opts.settings.sourceLanguageTag
|
|
14999
|
+
);
|
|
15000
|
+
const compiledMessages = opts.messages.map(
|
|
15001
|
+
(message) => compileMessage(message, fallbackMap, opts.outputStructure)
|
|
15002
|
+
);
|
|
15003
|
+
const pkgJson = await getPackageJson(nodeFsPromises, process.cwd());
|
|
15004
|
+
const stack = getStackInfo(pkgJson);
|
|
15005
|
+
telemetry.capture(
|
|
15006
|
+
{
|
|
15007
|
+
event: "PARAGLIDE-JS compile executed",
|
|
15008
|
+
properties: {
|
|
15009
|
+
stack
|
|
15010
|
+
}
|
|
15011
|
+
},
|
|
15012
|
+
opts.projectId
|
|
15013
|
+
);
|
|
15014
|
+
const resources = {};
|
|
15015
|
+
for (const compiledMessage of compiledMessages) {
|
|
15016
|
+
for (const languageTag of Object.keys(compiledMessage.translations)) {
|
|
15017
|
+
if (languageTag === "index")
|
|
15018
|
+
continue;
|
|
15019
|
+
if (!resources[languageTag])
|
|
15020
|
+
resources[languageTag] = "";
|
|
15021
|
+
resources[languageTag] += "\n\n" + compiledMessage.translations[languageTag];
|
|
15022
|
+
}
|
|
15023
|
+
}
|
|
15024
|
+
const languagesWithMessages = new Set(Object.keys(resources));
|
|
15025
|
+
const languagesWithoutMessages = opts.settings.languageTags.filter(
|
|
15026
|
+
(languageTag) => !languagesWithMessages.has(languageTag)
|
|
15027
|
+
);
|
|
15028
|
+
for (const languageTag of languagesWithoutMessages) {
|
|
15029
|
+
if (!resources[languageTag])
|
|
15030
|
+
resources[languageTag] = "\n\nexport {};";
|
|
15031
|
+
}
|
|
15032
|
+
telemetry.shutdown();
|
|
15033
|
+
let output = {
|
|
15034
|
+
// boilerplate files
|
|
15035
|
+
".prettierignore": ignoreDirectory,
|
|
15036
|
+
".gitignore": ignoreDirectory,
|
|
15037
|
+
"runtime.js": createRuntime(opts.settings)
|
|
15052
15038
|
};
|
|
15039
|
+
if (opts.outputStructure === "message-modules") {
|
|
15040
|
+
for (const message of compiledMessages) {
|
|
15041
|
+
output[`messages/index/${message.source.id}.js`] = [
|
|
15042
|
+
"/* eslint-disable */",
|
|
15043
|
+
'import { languageTag } from "../../runtime.js"',
|
|
15044
|
+
opts.settings.languageTags.map(
|
|
15045
|
+
(languageTag) => `import * as ${i(languageTag)} from "../${languageTag}/${message.source.id}.js"`
|
|
15046
|
+
).join("\n"),
|
|
15047
|
+
"\n",
|
|
15048
|
+
message.index
|
|
15049
|
+
].join("\n");
|
|
15050
|
+
for (const [lang, source] of Object.entries(message.translations)) {
|
|
15051
|
+
output[`messages/${lang}/${message.source.id}.js`] = [
|
|
15052
|
+
"/* eslint-disable */",
|
|
15053
|
+
`/**
|
|
15054
|
+
* This file contains language specific message functions for tree-shaking.
|
|
15055
|
+
*
|
|
15056
|
+
*! WARNING: Only import messages from this file if you want to manually
|
|
15057
|
+
*! optimize your bundle. Else, import from the \`messages.js\` file.
|
|
15058
|
+
*
|
|
15059
|
+
* Your bundler will (in the future) automatically replace the index function
|
|
15060
|
+
* with a language specific message function in the build step.
|
|
15061
|
+
*/`,
|
|
15062
|
+
source
|
|
15063
|
+
].join("\n");
|
|
15064
|
+
}
|
|
15065
|
+
}
|
|
15066
|
+
const messageIDs = compiledMessages.map((message) => message.source.id);
|
|
15067
|
+
output["messages.js"] = [
|
|
15068
|
+
"/* eslint-disable */",
|
|
15069
|
+
...messageIDs.map((id) => `export * from "./messages/index/${id}.js"`)
|
|
15070
|
+
].join("\n");
|
|
15071
|
+
for (const languageTag of opts.settings.languageTags) {
|
|
15072
|
+
output[`messages/${languageTag}.js`] = [
|
|
15073
|
+
"/* eslint-disable */",
|
|
15074
|
+
...messageIDs.length === 0 ? ["export {}"] : messageIDs.map((id) => `export * from "./${languageTag}/${id}.js"`)
|
|
15075
|
+
].join("\n");
|
|
15076
|
+
}
|
|
15077
|
+
} else {
|
|
15078
|
+
output = {
|
|
15079
|
+
...output,
|
|
15080
|
+
// message index file
|
|
15081
|
+
"messages.js": [
|
|
15082
|
+
"/* eslint-disable */",
|
|
15083
|
+
'import { languageTag } from "./runtime.js"',
|
|
15084
|
+
opts.settings.languageTags.map((languageTag) => `import * as ${i(languageTag)} from "./messages/${languageTag}.js"`).join("\n"),
|
|
15085
|
+
"\n",
|
|
15086
|
+
compiledMessages.map((message) => message.index).join("\n\n")
|
|
15087
|
+
].join("\n"),
|
|
15088
|
+
...Object.fromEntries(
|
|
15089
|
+
Object.entries(resources).map(([languageTag, content]) => [
|
|
15090
|
+
`messages/${languageTag}.js`,
|
|
15091
|
+
[
|
|
15092
|
+
"/* eslint-disable */",
|
|
15093
|
+
`/**
|
|
15094
|
+
* This file contains language specific message functions for tree-shaking.
|
|
15095
|
+
*
|
|
15096
|
+
*! WARNING: Only import messages from this file if you want to manually
|
|
15097
|
+
*! optimize your bundle. Else, import from the \`messages.js\` file.
|
|
15098
|
+
*
|
|
15099
|
+
* Your bundler will (in the future) automatically replace the index function
|
|
15100
|
+
* with a language specific message function in the build step.
|
|
15101
|
+
*/`,
|
|
15102
|
+
content
|
|
15103
|
+
].join("\n")
|
|
15104
|
+
])
|
|
15105
|
+
)
|
|
15106
|
+
};
|
|
15107
|
+
}
|
|
15108
|
+
return output;
|
|
15053
15109
|
};
|
|
15110
|
+
function getFallbackMap(languageTags, sourceLanguageTag) {
|
|
15111
|
+
return Object.fromEntries(
|
|
15112
|
+
languageTags.map((lang) => {
|
|
15113
|
+
const fallbackLanguage = lookup(lang, {
|
|
15114
|
+
languageTags: languageTags.filter((t) => t !== lang),
|
|
15115
|
+
defaultLanguageTag: sourceLanguageTag
|
|
15116
|
+
});
|
|
15117
|
+
if (lang === fallbackLanguage)
|
|
15118
|
+
return [lang, void 0];
|
|
15119
|
+
else
|
|
15120
|
+
return [lang, fallbackLanguage];
|
|
15121
|
+
})
|
|
15122
|
+
);
|
|
15123
|
+
}
|
|
15054
15124
|
let previousOutputHash;
|
|
15055
15125
|
async function writeOutput(outputDirectory, output, fs2) {
|
|
15056
15126
|
const currentOutputHash = hashOutput(output, outputDirectory);
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@inlang/paraglide-js",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "1.
|
|
4
|
+
"version": "1.8.0",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"publishConfig": {
|
|
7
7
|
"access": "public"
|
|
@@ -62,14 +62,14 @@
|
|
|
62
62
|
"vite-plugin-dts": "^3.8.1",
|
|
63
63
|
"vite-tsconfig-paths": "^4.3.2",
|
|
64
64
|
"vitest": "0.34.3",
|
|
65
|
-
"@inlang/cross-sell-sherlock": "0.0.4",
|
|
66
65
|
"@inlang/env-variables": "0.2.0",
|
|
67
|
-
"@inlang/
|
|
68
|
-
"@inlang/
|
|
66
|
+
"@inlang/cross-sell-sherlock": "0.0.4",
|
|
67
|
+
"@inlang/language-tag": "1.5.1",
|
|
69
68
|
"@inlang/telemetry": "0.3.28",
|
|
70
69
|
"@lix-js/client": "1.4.0",
|
|
70
|
+
"@inlang/sdk": "0.34.3",
|
|
71
71
|
"@lix-js/fs": "1.0.0",
|
|
72
|
-
"@inlang/
|
|
72
|
+
"@inlang/plugin-message-format": "2.2.0"
|
|
73
73
|
},
|
|
74
74
|
"exports": {
|
|
75
75
|
".": {
|