Languages
spring.flutter.navigation.languages gives each language its own addresses. The first language is the default, without a prefix; the others put theirs under /<language>. /ja/catalog opens the /catalog screen with the app's locale set to Japanese, so context.message, screen titles and Flutter's own widgets follow.
Why
On the web a language should be part of the address: a link shared in Japanese opens in Japanese, search engines list each language's pages, and the back button returns to the language it came from. The app's own code writes its paths without a prefix and keeps the current language, so no screen needs to know about it.
Example
# lib/resources/application.yml
spring:
flutter:
navigation:
languages: [en, ja]
// The current language stays: /catalog in English, /ja/catalog in Japanese.
context.route.go('/catalog');
// A menu that shows the same screen in another language.
TextButton(
onPressed: () => context.route.switchLanguage('ja'),
child: const Text('日本語'),
);
The rules
/ja/catalogopens the/catalogscreen with the app's locale set to Japanese:context.message,@ScreenRoute(title: '#{key}')and Flutter's own widgets follow.- The app's code writes its paths without a prefix (
go('/catalog')); the current language stays and the browser's bar shows/ja/catalog. A path with a prefix (go('/ja/catalog')) changes the language. - An address from the platform without a prefix (the browser's bar, a link) is the default language's.
context.route.languageis the current language;context.route.switchLanguage('en')shows the same screen in another language. The fonts declared for the language (spring.flutter.fonts.families.<family>.languages) load before the text changes; the first address's language has them before the first frame (see Assets and fonts).- A Markdown screen (
content:) shows<file>.ja.mdnext to the file under/ja, the default file when there is no translation. - The build writes the pages for search engines in every language, with
hreflangalternates (see Pages for search engines).
LanguageChangedEvent
The navigation publishes a LanguageChangedEvent when the language switches, by the address or by switchLanguage:
// Loads the catalog of the new language.
@Component()
class CatalogLanguage {
CatalogLanguage(this.catalog);
final Catalog catalog;
@EventListener()
void changed(LanguageChangedEvent event) => catalog.reload(event.to);
}
| Field | Meaning |
|---|---|
from |
The language before; null before the first one was set |
to |
The language now |
The texts of the screens change with it; ThemeText of the Flutter starter changes them the theme's way (motion.text-change).
The property
| Key | Type | Default | Meaning |
|---|---|---|---|
spring.flutter.navigation.languages |
list of language tags | none: one language, no prefix | The languages with their own addresses, the default first |
Unlike spring.flutter.app.locales, this key is a real list: [en, ja] in YAML or en,ja in .properties. The app's locales still come from the message files or spring.flutter.app.locales (see Messages and languages).