Skip to content

Commit

Permalink
Merge pull request #3151 from opral/mesdk-251-importfiles-exportfiles…
Browse files Browse the repository at this point in the history
…-interface-update-to-remove-the-need

finalize plugin.importFiles api
  • Loading branch information
samuelstroschein authored Sep 26, 2024
2 parents 588e066 + 99bbeaf commit 562eef8
Show file tree
Hide file tree
Showing 21 changed files with 1,127 additions and 1,294 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,19 @@ import type { Bundle, LiteralMatch, Message, Pattern, Variant } from "@inlang/sd
import { type plugin } from "../plugin.js"
import { unflatten } from "flat"

export const exportFiles: NonNullable<(typeof plugin)["exportFiles"]> = async ({ bundles }) => {
export const exportFiles: NonNullable<(typeof plugin)["exportFiles"]> = async ({
bundles,
messages,
variants,
}) => {
const result: Record<string, Record<string, any>> = {}
const resultNamespaces: Record<string, Record<string, Record<string, any>>> = {}

const messages = bundles.flatMap((bundle) => bundle.messages)

for (const message of messages) {
const serializedMessages = serializeMessage(
bundles.find((b) => b.id === message.bundleId)!,
message,
message.variants
variants.filter((v) => v.messageId === message.id)
)

for (const message of serializedMessages) {
Expand Down
68 changes: 22 additions & 46 deletions inlang/source-code/plugins/i18next/src/import-export/importFiles.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
/* eslint-disable @typescript-eslint/no-non-null-assertion */
import type {
Bundle,
BundleNested,
Message,
Pattern,
VariableReference,
Variant,
} from "@inlang/sdk2"
import type { Bundle, Pattern, VariableReference, Variant } from "@inlang/sdk2"
import { type plugin } from "../plugin.js"
import { flatten } from "flat"
import type {
BundleImport,
MessageImport,
VariantImport,
} from "../../../../sdk2/dist/plugin/schema.js"

export const importFiles: NonNullable<(typeof plugin)["importFiles"]> = async ({ files }) => {
const result: BundleNested[] = []
const bundles: Bundle[] = []
const messages: Message[] = []
const variants: Variant[] = []
const bundles: BundleImport[] = []
const messages: MessageImport[] = []
const variants: VariantImport[] = []

for (const file of files) {
const namespace = file.toBeImportedFilesMetadata?.namespace
Expand All @@ -26,41 +23,27 @@ export const importFiles: NonNullable<(typeof plugin)["importFiles"]> = async ({

// merge the bundle declarations
const uniqueBundleIds = [...new Set(bundles.map((bundle) => bundle.id))]
const uniqueBundles: Bundle[] = uniqueBundleIds.map((id) => {
const uniqueBundles: BundleImport[] = uniqueBundleIds.map((id) => {
const _bundles = bundles.filter((bundle) => bundle.id === id)
const declarations = removeDuplicates(_bundles.flatMap((bundle) => bundle.declarations))
return { id, declarations }
})

// establishing nesting
for (const bundle of uniqueBundles) {
const bundleNested: BundleNested = { ...bundle, messages: [] }

// @ts-expect-error - casting the type here
bundleNested.messages = messages.filter((message) => message.bundleId === bundle.id)

for (const message of bundleNested.messages) {
message.variants = variants.filter((variant) => variant.messageId === message.id)
}

result.push(bundleNested)
}

return { bundles: result }
return { bundles: uniqueBundles, messages, variants }
}

function parseFile(args: { namespace?: string; locale: string; content: ArrayBuffer }): {
bundles: Bundle[]
messages: Message[]
variants: Variant[]
bundles: BundleImport[]
messages: MessageImport[]
variants: VariantImport[]
} {
const resource: Record<string, string> = flatten(
JSON.parse(new TextDecoder().decode(args.content))
)

const bundles: Bundle[] = []
const messages: Message[] = []
const variants: Variant[] = []
const bundles: BundleImport[] = []
const messages: MessageImport[] = []
const variants: VariantImport[] = []

for (const key in resource) {
const value = resource[key]!
Expand All @@ -84,7 +67,7 @@ function parseMessage(args: {
value: string
locale: string
resource: Record<string, any>
}): { bundle: Bundle; message: Message; variant: Variant } {
}): { bundle: BundleImport; message: MessageImport; variant: VariantImport } {
const pattern = parsePattern(args.value)

// i18next suffixes keys with context or plurals
Expand All @@ -104,16 +87,15 @@ function parseMessage(args: {
})),
}

const message: Message = {
id: "",
const message: MessageImport = {
bundleId: bundleId,
selectors: [],
locale: args.locale,
}

const variant: Variant = {
id: "",
messageId: "",
const variant: VariantImport = {
messageBundleId: bundleId,
messageLocale: args.locale,
matches: [],
pattern: pattern.result,
}
Expand Down Expand Up @@ -259,12 +241,6 @@ function parseMessage(args: {

bundle.declarations = removeDuplicates(bundle.declarations)

// i18next suffixes keys with context or plurals
// "friend_female_one" -> "friend"
message.id = `${bundle.id};;locale=${args.locale};;`
variant.id = `${message.id};;match=${variant.matches.join(",")};;`
variant.messageId = message.id

return { bundle, message, variant }
}

Expand Down
Loading

0 comments on commit 562eef8

Please sign in to comment.