40 lines
1.3 KiB
Dart
40 lines
1.3 KiB
Dart
|
|
import 'package:fluent/fluent.dart';
|
||
|
|
import 'package:flutter/services.dart' show rootBundle;
|
||
|
|
import 'package:flutter/widgets.dart';
|
||
|
|
|
||
|
|
/// Loads and formats the app's Fluent messages. All user-facing strings come
|
||
|
|
/// from locales/en-US/main.ftl; lookups fall back to the message ID when a
|
||
|
|
/// message is missing so that missing translations are obvious in the UI.
|
||
|
|
class Strings {
|
||
|
|
final FluentBundle _bundle = FluentBundle('en-US');
|
||
|
|
|
||
|
|
/// Loads the messages from the app's asset bundle.
|
||
|
|
Future<void> load() async {
|
||
|
|
final source = await rootBundle.loadString('locales/en-US/main.ftl');
|
||
|
|
loadSource(source);
|
||
|
|
}
|
||
|
|
|
||
|
|
/// Loads messages from an already-read source, so tests can read the file
|
||
|
|
/// from disk instead of the asset bundle.
|
||
|
|
void loadSource(String source) {
|
||
|
|
_bundle.addMessages(source);
|
||
|
|
}
|
||
|
|
|
||
|
|
String t(String id, [Map<String, Object?> args = const {}]) {
|
||
|
|
return _bundle.format(id, args: args) ?? id;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/// Provides [Strings] to the widget tree.
|
||
|
|
class StringsScope extends InheritedWidget {
|
||
|
|
const StringsScope({required this.strings, required super.child, super.key});
|
||
|
|
|
||
|
|
final Strings strings;
|
||
|
|
|
||
|
|
static Strings of(BuildContext context) =>
|
||
|
|
context.getInheritedWidgetOfExactType<StringsScope>()!.strings;
|
||
|
|
|
||
|
|
@override
|
||
|
|
bool updateShouldNotify(StringsScope oldWidget) => false;
|
||
|
|
}
|