-
-
Notifications
You must be signed in to change notification settings - Fork 64
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
Support I18n #58
Comments
That would be a great feature for this template |
This would be amazing! |
Just sharing my experience with a non sanity translation project I worked on recently. That is www.xaxameme.com The website supports about 5 languages and I had a few challenges with the localisation. There are some languages that are RTL like Arabic so I needed to support that in the code. When the locals was Arabic. Later I used the next-intl package which was not optimised for nextjs 15 which made some things difficult. Also supporting multiple sitemaps for each language was also a thing. I did research doing this for sanity and I was looking at the @sanity/document-internationalization as it's the official one. I imagine that there would just be some minor changes to the code base needed. Using an official plugin might be better than maintaining the functionality alone ? Btw I'm happy to test and feedback the languages I know when gone a bit further |
Thanks so much for your feedback @alioftech! I've just pushed a new branch Changelog
Config// src/lib/i18n.ts
export const supportedLanguages = [
{ id: 'en', title: 'English' },
{ id: 'ja', title: 'Japanese' },
// { id: 'fr', title: 'French' },
] as const satisfies Language[] // sanity.config.ts
import { supportedLanguages } from '@/lib/i18n'
import { documentInternationalization } from '@sanity/document-internationalization'
export default defineConfig({
plugins: [
// ...
documentInternationalization({
supportedLanguages,
schemaTypes: ['page', 'blog.post'],
}),
],
}) Screenshots
![]() Language Switcher component in the footer: ![]() If all looks good and I get some more feedback, if any, I prepare to be added in the next major version of Sanity (v6.5 or v7.0?) |
@nuotsu I had the pleasure of seeing your work on i18n today, and I think it is definitely very good and very big news because you listened to the voice of the community. I agree with your idea of unified page-level translation. For i18n, I have a few expectations for your reference:
https://sanitypress.dev/blog/fr/my-sample-blog-post to > https://sanitypress.dev/fr/bloguer/mon-exemple-de-blog-post In addition, for the text labels on the page, how are they translated? For example, there is a file similar to messages/en.json in next-intl. Finally, if you can set up multiple languages like this, does it mean that you only need to maintain one branch? Because it can support both one language and multiple languages. |
@wild2008 Thanks for your detailed feedback! To answer your points:
That's my intention:
(FYI the default defined as: the first item in the
I've updated the LanguageSwitcher component and the middleware to account for this. You can now set different URLs for the same page, for each language. 12b7206
My personal take: as a user I would want to stay on the same page even if the translation doesn't exist, rather than being redirected to the Homepage. I added a disabled state to the LanguageSwitcher options, for non-existent translations, so at least they know the translation is not available. d728be5 ![]()
Setting a different name for the
If you are referring to the actual translations, those are done within the actual page document. It's essentially a duplicate of the original page/blog.post document so all the existing fields/labels are there to be updated.
I can maintain this as a single branch. Everything should still work whether additional languages or zero languages are configured in the |
Looking great! |
@nuotsu I have to say that your solution is very practical. Only about point 3, a practical scenario is: A visitor enters a landing page, he finds the entrance to other languages through the language switcher, then he only needs to click to enter the website in other languages (if there is a translation of the current page, it is better, if not, he can also view other pages in that language). If the status is set to unavailable, the language switcher loses the function of the international version entrance. |
@wild2008 Thanks again for reviewing! I do recognize the advantages of your approach. I will investigate the possibility of implementing options in the middleware to accommodate both approaches. FYI, the LanguageSwitcher component sets a language cookie when an alternative language is selected. The middleware will read this cookie and display the translated page, falling back to the default language if it is not present. In the event that the user navigates to subsequent pages, the middleware will persist in attempting to display the translated page (the selected language stored in the cookie). As soon as the LanguageSwitcher is invoked and an alternative language is selected, the aforementioned process will be repeated. |
@nuotsu I mentioned earlier that there are files like message/en.json in the next-intl project. My question is: for example, text like "Page not found" has different expressions in different languages. How to achieve multi-language translation in SanityPress? |
@wild2008 I'm not sure if I can find any areas in the codebase where there is static text. All copy should reside in the Sanity Studio, including the 404 page (this is a Page document with slug That said, I likely won't add a .json based translation in the initial release (this may change in the future if it's requested by more users or if it's needed at all). @wild2008 On a separate note, I couldn't find any contact details on your profile... Would you kindly reach out to the SanityPress email ([email protected])? I would like to share some special details with you, if you may! |
@nuotsu I have sent you an email and I am happy to contact you via email. |
@wild2008 As for suggestion on translating static text, such as in the Search module etc., I will make the "suggestion" of implementing 3rd party plugins like ![]() The example with the Search module gets quite dynamic (since I use concepts like search scope [pages, blog posts, etc]) so storing this in Sanity could get a bit bloated, hence I've put in only in the Next.js frontend. Anything static that can be in Sanity, should be in Sanity for easier translations. |
I'm thrilled to announce i18n is now supported in SanityPress v7.0.0. See SanityPress Pro for details. |
I wish it could have a language changer feature
The text was updated successfully, but these errors were encountered: