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

Internalization support #106

Closed
rallisf1 opened this issue Nov 3, 2021 · 4 comments
Closed

Internalization support #106

rallisf1 opened this issue Nov 3, 2021 · 4 comments
Labels
bug Something isn't working released

Comments

@rallisf1
Copy link
Contributor

rallisf1 commented Nov 3, 2021

Is your feature request related to a problem? Please describe.
When using any type of internalization plugin the meta fields don't update on language change.

Describe the solution you'd like
You should use a store for props object in order to auto refresh the meta-tags with zero-configuration

Describe alternatives you've considered
My workaround for typesafe-i18n is:

import { MetaTags, MetaTagsProps } from 'svelte-meta-tags';
import LL, { locale } from '../i18n/i18n-svelte';
import { locales } from "../i18n/i18n-util";
import { afterUpdate } from 'svelte';

let metaTags: MetaTagsProps = {
	title: '',
	description: '',
	languageAlternates: []
}

afterUpdate(() => {
	[, , ...pageParts] = $page.path.split("/");
	updateMetaLang();
});

function updateMetaLang(){
	let altLangs = [];
	locales.forEach((lang, _) => {
		if(lang != $locale){
			altLangs.push({
				hrefLang: lang,
				href: `https://${$page.host}/${lang}${$page.path.slice(3)}` // it's ugly but it works
			})
		}
	});
	metaTags.languageAlternates = altLangs;
	metaTags.title = LL.site_name();
	metaTags.description = LL.site_description();
}
@samuelstroschein
Copy link

When using any type of internalization plugin the meta fields don't update on language change.

That's likely because metaTags is not reactive. You shouldn't need the function updateMetaLang. Try the following:

$: metaTags = {
    title: LL.site_name(),
    description: LL.site_description(),
    languageAlternates: locales.map((locale) => ({ 
      hrefLang: locale,
      href: `https://${$page.host}/${lang}${$page.path.slice(3)}` 
    }))
}

That should be it. The $ prefix makes metaTags reactive. See more here. In a nutshell, metaTags should "rebuild" if for example LL.site_name() changes. Small remark, LL is a reactive store by itself. I am not sure if it will rebuild without the $ prefix. If it doesn't, please try to prefix LL.site_name() with the dollar sign i.e. $LL.site_name()

@samuelstroschein
Copy link

Another small remark:
Array.push() is not reactive in Svelte. To add elements to an array in a reactive manner, you need to re-assign the array variable. An elegant workaround is myArray = [...myArray, newElement]. Read more here

@rallisf1
Copy link
Contributor Author

rallisf1 commented Nov 4, 2021

Thanks for the tips. I tried making metaTags reactive but didn't work, turns out LL needed a $ too...pfff

You may close this, I'm just noob :D

rallisf1 added a commit to rallisf1/svelte-meta-tags that referenced this issue Nov 4, 2021
@oekazuma
Copy link
Owner

oekazuma commented Nov 4, 2021

🎉 This issue has been resolved in version 2.1.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@oekazuma oekazuma closed this as completed Nov 4, 2021
@oekazuma oekazuma added bug Something isn't working released labels Nov 4, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working released
Projects
None yet
Development

No branches or pull requests

3 participants