-
Notifications
You must be signed in to change notification settings - Fork 186
Description
While accessibility labels and ARIA support seem to exist, many statically generated sites like app landing pages or documentation sites would be vastly more accessible if they were available in multiple languages. From what I understand, it would be already possible to create a view with buttons that link to separate subfolders of articles (such as en, de-DE etc.). But there are a few others things that need to be considered for a website with multi-language support:
- A way to adjust the
languagevalue of theSitebased on the current language settings (this line) - A way to provide different values for
Stringparameters such as for a sitesdescriptionor a buttons label - A built-in way to know/determine the supported languages of a site during build
- A built-in language switcher component that automatically lists the supported languages & sets it on click
- Some kind of string table where users can provide different values for different languages
I know that this sounds like adding a dynamic layer to this "static" site generator. Localization in my opinion is for sure a "dynamic" aspect worth providing, but if implemented in a thoughtful manner, we can retain the static aspect by simply creating a duplicate of each page with only the localized texts changed. The language could be provided at root path level via the Language enums raw value, leading to URLs like https://ignitesamples.hackingwithswift.com/de/grid-examples/. We could generate variants of all pages for each supported language and place them in the respective folder (e.g. de) so everything still is static.
But we would need some JavaScript to set the language value based on the path during page load, so things like the metadata have the correct language set. For things like the usage of localizedContains, while building the different language subfolders, we would need to set a custom locale with the current language being generated for accurate results.
As for the string table, I would suggest we use a subset of String Catalogs as we don't need all features, such as "Vary by device". We could even start without pluralization support to keep things simple. I'm happy to donate my Codable struct to parse them, which I have already implemented for one of my apps. Updating that is simple enough in case Apple makes changes. It's versioned as well, so we should be able to notice easily. This year, they didn't change anything to the structure, so it should be pretty stable. But I'm happy to adjust the code whenever needed.
Using String Catalogs rather than a custom format has great advantages:
- Users can use well-known APIs like
String(localized:)to specify texts that should be localized - We can adjust existing components to accept
LocalizedStringKeyrather thanStringwhere needed - Xcode will automatically extract the localizable texts to the String Catalog on build/run
- Xcode provides an easy-to-use editor for String Catalogs built into Xcode with localization progress & more
As not everyone will need or want to localize their sites, we should keep this as opt-in and default to exactly the same behavior without any subpaths like /en. Also, a default language should be set and used as a fallback for when a domain is called without any language subpath. I'm not sure if a safe redirect is even possible on static site calls, so the default locale files should just live in the root directory.
Please let me know what you think about adding localization support and also about my suggestion. I'm happy to do provide an initial version of localization support once you've approved that what I outlined makes sense to you.