gpt4 book ai didi

android - 在 headless (headless)模式下获取本地化字符串

转载 作者:行者123 更新时间:2023-12-04 09:43:13 39 4
gpt4 key购买 nike

我目前正在尝试在 headless (headless)模式下运行应用程序,我定义了后台回调:

void callbackInBackground() {
// Invoked from the service
}

我正在尝试从回调中获取具有当前语言环境的本地化字符串。

因为我需要 BuildContext要做到这一点,我不知道如何检索它们。

有什么提示和技巧吗?

最佳答案

您可以使用 easy_localization插件来实现这一点。此外,您可以使用 flutter_background_fetch headless (headless)执行插件。
easy_localization 示例:

import 'dart:developer';
import 'dart:ui';

import 'lang_view.dart';
import 'package:flutter/material.dart';
import 'package:easy_localization/easy_localization.dart';
import 'package:easy_localization_loader/easy_localization_loader.dart';
import 'package:flutter_icons/flutter_icons.dart';

import 'generated/locale_keys.g.dart';

void main() {
runApp(EasyLocalization(
child: MyApp(),
supportedLocales: [
Locale('en', 'US'),
Locale('ar', 'DZ'),
Locale('de', 'DE'),
Locale('ru', 'RU')
],
path: 'resources/langs/langs.csv', //'resources/langs',
// fallbackLocale: Locale('en', 'US'),
// startLocale: Locale('de', 'DE'),
// saveLocale: false,
// useOnlyLangCode: true,
// preloaderColor: Colors.black,
// preloaderWidget: CustomPreloaderWidget(),

// optional assetLoader default used is RootBundleAssetLoader which uses flutter's assetloader
// install easy_localization_loader for enable custom loaders
// assetLoader: RootBundleAssetLoader()
// assetLoader: HttpAssetLoader()
// assetLoader: FileAssetLoader()
assetLoader: CsvAssetLoader()
// assetLoader: YamlAssetLoader() //multiple files
// assetLoader: YamlSingleAssetLoader() //single file
// assetLoader: XmlAssetLoader() //multiple files
// assetLoader: XmlSingleAssetLoader() //single file
// assetLoader: CodegenLoader()
));
}

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
log(context.locale.toString(),
name: '${this} # locale Context');
log('title'.tr().toString(), name: '${this} # locale');
return MaterialApp(
title: 'title'.tr(),
localizationsDelegates: context.localizationDelegates,
supportedLocales: context.supportedLocales,
locale: context.locale,
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Easy localization'),
);
}
}

class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);

final String title;

@override
_MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
int counter = 0;
bool _gender = true;

void incrementCounter() {
setState(() {
counter++;
});
}

void switchGender(bool val) {
setState(() {
_gender = val;
});
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(LocaleKeys.title).tr(context: context),
//Text(AppLocalizations.of(context).tr('title')),
actions: <Widget>[
FlatButton(
child: Icon(Icons.language),
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (_) => LanguageView(), fullscreenDialog: true),
);
},
),
],
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Spacer(
flex: 1,
),
Text(
LocaleKeys.gender_with_arg,
style: TextStyle(
color: Colors.grey.shade600,
fontSize: 19,
fontWeight: FontWeight.bold),
).tr(args: ['aissat'], gender: _gender ? 'female' : 'male'),
Text(
tr(LocaleKeys.gender, gender: _gender ? 'female' : 'male'),
style: TextStyle(
color: Colors.grey.shade600,
fontSize: 15,
fontWeight: FontWeight.bold),
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Icon(FontAwesome.male),
Switch(value: _gender, onChanged: switchGender),
Icon(FontAwesome.female),
],
),
Spacer(
flex: 1,
),
Text(LocaleKeys.msg).tr(args: ['aissat', 'Flutter']),
Text(LocaleKeys.msg_named).tr(namedArgs: {'lang': 'Dart'}, args: ['Easy localization']),
Text(LocaleKeys.clicked).plural(counter),
FlatButton(
onPressed: () {
incrementCounter();
},
child: Text(LocaleKeys.clickMe).tr(),
),
SizedBox(
height: 15,
),
Text(
plural(LocaleKeys.amount, counter,
format: NumberFormat.currency(
locale: Intl.defaultLocale, symbol: '€')),
style: TextStyle(
color: Colors.grey.shade900,
fontSize: 18,
fontWeight: FontWeight.bold)),
SizedBox(
height: 20,
),
RaisedButton(
onPressed: () {
context.deleteSaveLocale();
},
child: Text(LocaleKeys.reset_locale).tr(),
),
Spacer(
flex: 1,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: incrementCounter,
child: Text('+1'),
),
);
}
}

class CustomPreloaderWidget extends StatelessWidget {
const CustomPreloaderWidget({Key key}) : super(key: key);

@override
Widget build(BuildContext context) {
log('Loading custom preloder widget');
return Container(
child: Center(
child: CircularProgressIndicator()
),
);
}
}

关于android - 在 headless (headless)模式下获取本地化字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62242610/

39 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com