Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update to paraglide js v2 #461

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

samuelstroschein
Copy link

@samuelstroschein samuelstroschein commented Feb 28, 2025

Paraglide JS 2.0 is going to be released on Monday (March 3). This PR updates the addon to set up and use v2 of Paraglide JS.

Pre-elimnary release notes for Paraglide JS v2 can be found here

Don't merge this before v2 got released (March 3)

Copy link

changeset-bot bot commented Feb 28, 2025

🦋 Changeset detected

Latest commit: 86d6c00

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
sv Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Comment on lines -20 to -24
'https://cdn.jsdelivr.net/npm/@inlang/message-lint-rule-empty-pattern@1/dist/index.js',
'https://cdn.jsdelivr.net/npm/@inlang/message-lint-rule-identical-pattern@1/dist/index.js',
'https://cdn.jsdelivr.net/npm/@inlang/message-lint-rule-missing-translation@1/dist/index.js',
'https://cdn.jsdelivr.net/npm/@inlang/message-lint-rule-without-source@1/dist/index.js',
'https://cdn.jsdelivr.net/npm/@inlang/message-lint-rule-valid-js-identifier@1/dist/index.js',
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lint rules have been deprecated in favor of upcoming lix validation rules that are more generalizable opral/lix-sdk#239

}
};

const options = defineAddonOptions({
availableLanguageTags: {
question: `Which languages would you like to support? ${colors.gray('(e.g. en,de-ch)')}`,
type: 'string',
default: 'en',
default: 'en, de',
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added a second language to have a better demo. i skipped through "which languages do you want to support?" several times. the demo will only have one language as a result leading to a "okay and now?" moment

const ext = typescript ? 'ts' : 'js';
if (!kit) throw new Error('SvelteKit is required');

const paraglideOutDir = 'src/lib/paraglide';

sv.dependency('@inlang/paraglide-sveltekit', '^0.15.5');
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the sveltekit adapter is not needed anymore. hallelujah

Comment on lines -118 to -134
sv.file(`src/lib/i18n.${ext}`, (content) => {
const { ast, generateCode } = parseScript(content);

imports.addNamed(ast, '@inlang/paraglide-sveltekit', { createI18n: 'createI18n' });
imports.addDefault(ast, '$lib/paraglide/runtime', '* as runtime');

const createI18nExpression = common.expressionFromString('createI18n(runtime)');
const i18n = variables.declaration(ast, 'const', 'i18n', createI18nExpression);

const existingExport = exports.namedExport(ast, 'i18n', i18n);
if (existingExport.declaration !== i18n) {
log.warn('Setting up $lib/i18n failed because it already exports an i18n function');
}

return generateCode();
});

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no more i18n.js needed in v2 🙏

Comment on lines -164 to -190
// add the <ParaglideJS> component to the layout
sv.file(`${kit.routesDirectory}/+layout.svelte`, (content) => {
const { script, template, generateCode } = parseSvelte(content, { typescript });

const paraglideComponentName = 'ParaglideJS';
imports.addNamed(script.ast, '@inlang/paraglide-sveltekit', {
[paraglideComponentName]: paraglideComponentName
});
imports.addNamed(script.ast, '$lib/i18n', { i18n: 'i18n' });

if (template.source.length === 0) {
const svelteVersion = dependencyVersion('svelte');
if (!svelteVersion) throw new Error('Failed to determine svelte version');

html.addSlot(script.ast, template.ast, svelteVersion);
}

const templateCode = new MagicString(template.generateCode());
if (!templateCode.original.includes('<ParaglideJS')) {
templateCode.indent();
templateCode.prepend('<ParaglideJS {i18n}>\n');
templateCode.append('\n</ParaglideJS>');
}

return generateCode({ script: script.generateCode(), template: templateCode.toString() });
});

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no more provider and layout needed in v2 🙏

@@ -204,8 +164,7 @@ export default defineAddon({
}
htmlNode.attribs = {
...htmlNode.attribs,
lang: '%paraglide.lang%',
dir: '%paraglide.textDirection%'
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removing textDir because users reported issues with the Intl API behaving differently in different runtimes.

it's better to have a working setup for everyone that users can improve themselves than a half broken setup

Comment on lines -248 to -261
if (!scriptCode.original.includes('function switchToLanguage')) {
scriptCode.trim();
scriptCode.append('\n\n');
scriptCode.append(dedent`
${ts('', '/**')}
${ts('', '* @param {import("$lib/paraglide/runtime").AvailableLanguageTag} newLanguage')}
${ts('', '*/')}
function switchToLanguage(newLanguage${ts(': AvailableLanguageTag')}) {
const canonicalPath = i18n.route(page.url.pathname);
const localisedPath = i18n.resolveRoute(canonicalPath, newLanguage);
goto(localisedPath);
}
`);
}
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

setLocale() just works in v2, irrespective of the chosen strategy

Comment on lines +216 to +219
templateCode.append(
'<p>\nIf you use VSCode, install the <a href="https://marketplace.visualstudio.com/items?itemName=inlang.vs-code-extension" target="_blank">Sherlock i18n extension</a> for a better i18n experience.\n</p>'
);

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved the recommendation form the CLI into the paraglide js demo with a link to directly open the vscode marketplace

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant