简体   繁体   English

如何在 flutter 中通过一个按钮翻译整个应用程序

[英]How can I translate the whole app in a single button in flutter

I'm new to flutter, so basically I'm making a product selling app and the products will be inserted by the customer, so my question is how can I translate all the texts in the app in a click of a button (The text in the app itself and the texts in which the customers gonna name the product eg- book), iv gone through many packages but most translate only a string and other are hard coded in json files, Help will be much appreciated, Thank you.我是 flutter 的新手,所以基本上我正在制作一个产品销售应用程序,产品将由客户插入,所以我的问题是如何通过单击按钮翻译应用程序中的所有文本(文本在应用程序本身和客户将产品命名为 eg-book 的文本中),iv 经历了许多包,但大多数只翻译一个字符串,其他的都硬编码在 json 文件中,非常感谢帮助,谢谢。

Yes it is possible with localizely_sdk: package from pub.dev .是的,可以使用localizely_sdk: package 来自pub.dev

you can read more about it on https://localizely.com/flutter-over-the-air/ .您可以在https://localizely.com/flutter-over-the-air/上阅读更多相关信息。

There is actually a package for that with several other features.实际上有一个 package 具有其他几个功能。 Check out this link:查看此链接:

language_builder语言生成器

you can achieve it by get package check out this demo.I hope this will help you.您可以通过获取package 查看此演示来实现它。希望这对您有所帮助。

import 'package:ecommerce_web/screen/dashboard/localization.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';

Future main() async {
  SystemChrome.setSystemUIOverlayStyle(
    const SystemUiOverlayStyle(
      statusBarColor: Colors.transparent,
      statusBarIconBrightness: Brightness.dark,
    ),
  );
  runApp(const MyApp());
}

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

  @override
  Widget build(BuildContext context) {
    return GetMaterialApp(
      translations: Localization(),
      locale: Get.deviceLocale,
      fallbackLocale: const Locale('en', 'US'),
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(),
    );
  }
}
class MyHomePage extends StatelessWidget {
  const MyHomePage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Text(
              'greeting'.tr,
              style: Theme.of(context).textTheme.headline4,
            ),
            OutlinedButton(
              onPressed: () => Get.updateLocale(const Locale('hi', 'IN')),
              child: const Text('Hindi'),
            ),
          ],
        ),
      ),
    );
  }
}

localization file.本地化文件。

    import 'package:get/get.dart';

class Localization extends Translations {
  @override
  Map<String, Map<String, String>> get keys => {
        'hi_IN': {
          'greeting': 'नमस्ते',
        },
        'ja_JP': {
          'greeting': 'こんにちは',
        },
        'en_US': {
          'greeting': 'Hello',
        },
      };
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM