Skip to content

Commit

Permalink
Merge pull request #2593 from opral/nilsjacobsen/inweb-57-adjust-mani…
Browse files Browse the repository at this point in the history
…festjson-for-multi-pages

Nilsjacobsen/inweb 57 adjust manifestjson for multi pages
  • Loading branch information
NilsJacobsen authored Apr 16, 2024
2 parents 9db55e7 + 2294037 commit c226c78
Show file tree
Hide file tree
Showing 27 changed files with 193 additions and 93 deletions.
2 changes: 1 addition & 1 deletion inlang/source-code/cli/src/commands/machine/translate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,4 +184,4 @@ async function mockMachineTranslateMessage(args: {
}
}
return { data: copy }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -154,18 +154,18 @@ export type I18nConfig<T extends string> = {
export function createI18n<T extends string>(runtime: Paraglide<T>, options?: I18nUserConfig<T>) {
const translations = resolvePathTranslations(
options?.pathnames ?? {},
runtime.availableLanguageTags,
runtime.availableLanguageTags
)

if (dev) {
const issues = validatePathTranslations(
translations,
runtime.availableLanguageTags,
options?.matchers ?? {},
options?.matchers ?? {}
)
if (issues.length) {
console.warn(
`The following issues were found in your path translations. Make sure to fix them before deploying your app:`,
`The following issues were found in your path translations. Make sure to fix them before deploying your app:`
)
console.table(issues)
}
Expand Down Expand Up @@ -273,7 +273,7 @@ export function createI18n<T extends string>(runtime: Paraglide<T>, options?: I1
canonicalPath,
lang,
config.translations,
config.matchers,
config.matchers
)

return serializeRoute({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export type HandleOptions = {

export const createHandle = <T extends string>(
i18n: I18nConfig<T>,
options: HandleOptions,
options: HandleOptions
): Handle => {
const langPlaceholder = options.langPlaceholder ?? "%paraglide.lang%"
const dirPlaceholder = options.textDirectionPlaceholder ?? "%paraglide.textDirection%"
Expand Down Expand Up @@ -83,7 +83,7 @@ export const createHandle = <T extends string>(
`Please replace the \`lang\` attribute with the correct placeholder:"\n\n` +
` - <html ${SVELTEKIT_DEFAULT_LANG_ATTRIBUTE}>\n` +
` + <html lang="${langPlaceholder}" dir="${dirPlaceholder}">` +
`\n\nThis message will not be shown in production.`,
`\n\nThis message will not be shown in production.`
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import type { PathTranslations } from "../config/pathTranslations.js"
export const handleRedirects: (
runtime: Paraglide<any>,
translations: PathTranslations<string>,
matchers: Record<string, ParamMatcher>,
matchers: Record<string, ParamMatcher>
) => Handle =
(runtime, translations, matchers) =>
async ({ event, resolve }) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe("getCanonicalPath", () => {
de: "/uber-uns",
},
},
{},
{}
)
expect(translatedPath).toBe("/")
})
Expand All @@ -41,7 +41,7 @@ describe("getCanonicalPath", () => {
de: "/baz",
},
},
{},
{}
)
expect(canonicalPath).toBe("/foo")
})
Expand All @@ -61,7 +61,7 @@ describe("getCanonicalPath", () => {
de: "/baz/[id]/[slug]",
},
},
{},
{}
)
expect(canonicalPath).toBe("/foo/123")
})
Expand All @@ -81,7 +81,7 @@ describe("getCanonicalPath", () => {
de: "/baz/[id]/[slug]",
},
},
{},
{}
)
expect(canonicalPath).toBe("/foo/123")
})
Expand All @@ -96,7 +96,7 @@ describe("getCanonicalPath", () => {
de: "/baz/[...rest]",
},
},
{},
{}
)
expect(canonicalPath).toBe("/foo/123/baz")
})
Expand All @@ -111,7 +111,7 @@ describe("getCanonicalPath", () => {
de: "/baz/[...rest]",
},
},
{},
{}
)
expect(canonicalPath).toBe("/foo")
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe("getTranslatedPath", () => {
de: "/baz",
},
},
{},
{}
)
expect(translatedPath).toBe("/bar")
})
Expand All @@ -32,7 +32,7 @@ describe("getTranslatedPath", () => {
de: "/baz/[id]/cvb",
},
},
{},
{}
)
expect(translatedPath).toBe("/bar/123/cvb")
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe("translatePath", () => {
availableLanguageTags: ["de", "en"],
defaultLanguageTag: "en",
prefixDefaultLanguage: "never",
},
}
)

expect(translatedPath).toBe(base + "/de/foo/bar")
Expand All @@ -31,7 +31,7 @@ describe("translatePath", () => {
availableLanguageTags: ["de", "en"],
defaultLanguageTag: "en",
prefixDefaultLanguage: "never",
},
}
)

expect(translatedPath).toBe(base + "/de/foo/bar/")
Expand All @@ -48,7 +48,7 @@ describe("translatePath", () => {
availableLanguageTags: ["de", "en"],
defaultLanguageTag: "en",
prefixDefaultLanguage: "never",
},
}
)

expect(translatedPath).toBe(base + "/de/foo/bar")
Expand All @@ -65,7 +65,7 @@ describe("translatePath", () => {
availableLanguageTags: ["de", "en"],
defaultLanguageTag: "en",
prefixDefaultLanguage: "never",
},
}
)

expect(translatedPath).toBe(base ? base + "/" : "/")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export function translatePath(
availableLanguageTags: readonly string[]
defaultLanguageTag: string
prefixDefaultLanguage: "always" | "never"
},
}
) {
const {
path: targetedPathSource,
Expand All @@ -36,7 +36,7 @@ export function translatePath(
canonicalPath,
targetLanguage,
translations,
matchers,
matchers
)

return serializeRoute({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ export function getPathInfo(path: string, options: ParseOptions): ParseResult {
const dataSuffix = pathWithoutBase.endsWith(HTML_DATA_SUFFIX)
? HTML_DATA_SUFFIX
: pathWithoutBase.endsWith(DATA_SUFFIX)
? DATA_SUFFIX
: undefined
? DATA_SUFFIX
: undefined

if (dataSuffix) {
pathWithoutBase = pathWithoutBase.replace(dataSuffix, "")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export function paraglide(userConfig: UserConfig): any {
*/
function registerPreprocessor(
// eslint-disable-next-line @typescript-eslint/no-unused-vars
_config: Config,
_config: Config
): Plugin {
const preprocessConfig: PreprocessorConfig = {}
return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ describe.concurrent("preprocessor", () => {

const html = await renderComponent(code)
expect(html).toBe(
`<a href="/rewritten/de" hreflang="de">de</a><a href="/rewritten/en" hreflang="en">en</a>`,
`<a href="/rewritten/de" hreflang="de">de</a><a href="/rewritten/en" hreflang="en">en</a>`
)
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export function preprocessor(_config: PreprocessorConfig): PreprocessorGroup {
) {
console.warn(
"[paraglide] `<a>` tags in the same file as `<ParaglideJS>` won't be translated.\nConsider moving it into another component.\n" +
filename,
filename
)
}

Expand All @@ -69,7 +69,7 @@ export function preprocessor(_config: PreprocessorConfig): PreprocessorGroup {
[@inlang/paraglide-js-adapter-sveltekit] Failed to parse ${filename}.
This may be because you are using a signifficantly newer version of the "svelte" package.
Try forcing "@inlang/paraglide-js-adapter-sveltekit" to use your version of "svelte" using pnpm overrides`,
{ cause: error },
{ cause: error }
)
}

Expand All @@ -93,7 +93,7 @@ export function preprocessor(_config: PreprocessorConfig): PreprocessorGroup {
function modifyScriptTag(
ast: Ast,
code: MagicString,
additions: { before?: Iterable<string>; after?: Iterable<string> },
additions: { before?: Iterable<string>; after?: Iterable<string> }
) {
const before = additions.before ? [...additions.before] : []
const after = additions.after ? [...additions.after] : []
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export function shouldApply(content: string, TRANSLATIONS: TranslationDefinition
for (const [element_name, attribute_translations] of Object.entries(TRANSLATIONS)) {
const includesElement = content.includes(element_name)
const includesAttribute = attribute_translations.some((tr) =>
content.includes(tr.attribute_name),
content.includes(tr.attribute_name)
)

if ((includesSpread || includesAttribute) && (includesElement || includesSvelteElement)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export const rewrite = ({

// add a new spread attribute at the end of the element
const newSpreadAttributeString = c.spreadAttr(
`${i("handle_attributes")}(${attributeObjectSource}, ${uneval(attribute_translations)})`,
`${i("handle_attributes")}(${attributeObjectSource}, ${uneval(attribute_translations)})`
)

// remove all attributes that were replaced from the code
Expand Down Expand Up @@ -66,17 +66,17 @@ export const rewrite = ({
`${i("translateAttribute")}(
${attrubuteValuesToJSValue(
attribute.value,
originalCode,
originalCode
)},
${
langAttribute
? attrubuteValuesToJSValue(
langAttribute.value,
originalCode,
)
originalCode
)
: "undefined"
}
)`,
)`
)

//replace the attribute with the new attribute
Expand Down Expand Up @@ -106,7 +106,7 @@ export const rewrite = ({
value = c.ternary(
c.eq(thisValue, c.str(element_name)),
`${i("handle_attributes")}(${attributeObjectSource}, ${uneval(attribute_translations)})`,
value,
value
)
}

Expand Down Expand Up @@ -144,13 +144,13 @@ export const rewrite = ({
langAttribute
? attrubuteValuesToJSValue(
langAttribute.value,
originalCode,
)
originalCode
)
: "undefined"
}
)`,
attrubuteValuesToJSValue(attribute.value, originalCode),
),
attrubuteValuesToJSValue(attribute.value, originalCode)
)
)

//replace the attribute with the new attribute
Expand Down Expand Up @@ -203,7 +203,7 @@ export const rewrite = ({
}
return attrs;
}`,
}`
)

return {
Expand All @@ -216,7 +216,7 @@ export const rewrite = ({

function getAttributesObject(
element: ElementNode<string>,
originalCode: string,
originalCode: string
): {
attributes: Set<Attribute<string> | SpreadAttribute>
attributeObjectSource: string
Expand All @@ -228,7 +228,7 @@ function getAttributesObject(
switch (attribute.type) {
case "Attribute": {
attributeEntries.push(
`${c.str(attribute.name)} : ${attrubuteValuesToJSValue(attribute.value, originalCode)}`,
`${c.str(attribute.name)} : ${attrubuteValuesToJSValue(attribute.value, originalCode)}`
)
replacedAttributes.add(attribute)
break
Expand All @@ -238,7 +238,7 @@ function getAttributesObject(
//@ts-ignore
attribute.expression.start,
//@ts-ignore
attribute.expression.end,
attribute.expression.end
)

attributeEntries.push(`...(${code})`)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export type ElementNode<Name extends stirng> = {
? {
name: "svelte:element"
tag: string | Expression
}
}
: { name: Name })

type Expression = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { Ast, Attribute, ElementNode, TemplateNode } from "../types.js"

export function getElementsFromAst<Name extends string>(
ast: Ast,
elementName: Name,
elementName: Name
): ElementNode<Name>[] {
const links: ElementNode<Name>[] = []

Expand Down Expand Up @@ -30,9 +30,9 @@ export function hasSpreadAttribute(element: ElementNode<string>): boolean {

export function getAttributeByName<T extends string>(
element: ElementNode<string>,
name: T,
name: T
): Attribute<T> | undefined {
return element.attributes.find(
(attribute) => attribute.type === "Attribute" && attribute.name === name,
(attribute) => attribute.type === "Attribute" && attribute.name === name
) as Attribute<T> | undefined
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { escapeForTemplateString } from "./escape.js"
*/
export function attrubuteValuesToJSValue(
values: AttributeValue[] | boolean | string,
originalCode: string,
originalCode: string
): string {
if (typeof values === "boolean") return values.toString()
if (typeof values === "string") return "`" + escapeForTemplateString(values) + "`"
Expand Down Expand Up @@ -50,7 +50,7 @@ export function attrubuteValuesToJSValue(
}

function noTextNodes(
values: AttributeValue[],
values: AttributeValue[]
): values is Exclude<AttributeValue, { type: "Text" }>[] {
return !values.some((value) => value.type === "Text") as any
}
Loading

0 comments on commit c226c78

Please sign in to comment.