@inlang/sdk
v3.0.6
Published
[](https://www.npmjs.com/package/@inlang/sdk) [
});
// query the project
project.*Loading an unpacked project from Git
import { loadProjectFromDirectory } from "@inlang/sdk";
const project = await loadProjectFromDirectory({
path: "./project.inlang",
});Next steps
Go to the API reference to learn how to query messages, changes, and save the project.
Plugins
The inlang SDK supports plugins to extend its functionality.
Plugins can be used to import/export messages in different formats, add custom validation rules, and implement specialized workflows.
Available Plugins
Find available plugins on https://inlang.com/c/plugins.
Creating a Plugin
Getting started
Implement the InlangPlugin type.
Examples can be found here. Particulary the message format plugin is a good starting point.
const myPlugin: InlangPlugin = {
key: "my-plugin",
importFiles: () => {
// Import files logic
},
exportFiles: () => {
// Export files logic
},
};Deploying a plugin
[!NOTE]
Why is a CDN requires instead of using npm to use plugins?Non-JS projects (Android, iOS, etc.) wouldn't be able to use inlang, and browser-based apps like Fink couldn't load plugins.
npx @inlang/cli plugin build --entry ./src/plugin.js We recommend uploading the plugin to NPM which makes it automatically available on JSDelivr and enables users to pin the version of your plugin.
https://cdn.jsdelivr.net/npm/my-plugin@1/dist/index.jsAPI reference
Creating a new project
import { newProject } from "@inlang/sdk";
// Create a new project
const file = await newProject();
// write the file anywhere you want
await fs.writeFile("./project.inlang", file);Loading a project
import { loadProjectInMemory } from "@inlang/sdk";
const file = await fs.readFile("./project.inlang");
// Load a project from a directory
const project = await loadProjectInMemory({
blob: file
});Querying a project
// Accessing settings and plugins
const settings = await project.settings.get();
const plugins = await project.plugins.get();
// Querying messages
const messages = await project.db
.selectFrom("message")
.selectAll()
.execute();
console.log(messages);Querying changes
[!NOTE]
The inlang plugin for lix is work in progress. If you stumble on issues, please open an issue on the GitHub.
The inlang file format uses version control via lix. project.lix is the underlying Lix instance. Visit the lix documentation for more information on how to query changes.
const result = await project.lix.execute(`
SELECT created_at, schema_key, entity_pk, snapshot_content
FROM lix_change
ORDER BY created_at DESC
`);
const changes = result.rows.map((row) => row.toObject());Saving a project
const newFile = await project.toBlob();
await fs.writeFile("./project.inlang", newFile);Importing and exporting translation files
The import and export of messages depends on the installed plugins. The following example shows how to import and export messages using a plugin that supports JSON files.
const file = await fs.readFile("./en.json");
// Import files
await project.importFiles({
pluginKey: "plugin.inlang.messageFormat",
files: [
{ locale: "en", content: file },
],
});
// Export files
const files = await project.exportFiles({
pluginKey: "plugin.inlang.messageFormat"
});
await fs.writeFile("./en.json", files[0].content);Installing plugins
const settings = await project.settings.get();
settings.modules.push(
"https://cdn.jsdelivr.net/npm/@inlang/plugin-i18next@latest/dist/index.js"
)
await project.settings.set(settings)Unpacked inlang files (directories)
[!NOTE]
Unpacked inlang files are the Git-friendly representation of packed.inlangfiles.Git can store packed snapshots, but plain-file review and merge workflows work better with the unpacked directory. If you don't intend to store the inlang file in git, use the packed file.
Unpacked inlang files are not portable. They depend on plugins and do not persist version control via lix data.
import {
loadProjectFromDirectory,
saveProjectToDirectory
} from "@inlang/sdk";
const project = await loadProjectFromDirectory({
"path": "./project.inlang"
});
// modify the project
await saveProjectToDirectory({
"project": project,
"path": "./project.inlang"
});Listing on inlang.com
To list your app/plugin on inlang.com, please open a pull request to the registry.json file.
Make sure that the link you are contributing points to a marketplace-manifest.json file. An example of can be found here
