简体   繁体   中英

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.

Yes it is possible with localizely_sdk: package from pub.dev .

you can read more about it on https://localizely.com/flutter-over-the-air/ .

There is actually a package for that with several other features. Check out this link:

language_builder

you can achieve it by get package check out this demo.I hope this will help you.

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',
        },
      };
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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