首页 > 解决方案 > 我怎样才能设法使这个翻译系统工作?

问题描述

我正在做一个在 Flutter 中进行本地化的项目。但是系统现在根本运行不正常。它没有按我的计划进行。即使我更改了语言,它也没有翻译。所以我决定做一个翻译按钮,但我不知道怎么写!无论如何,这是我的代码:

主要飞镖:

import 'package:final_translating_test/taylake.dart';
import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';

import 'app_localizations.dart';
import 'dakmilprison.dart';
import 'damw.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',

     // Here if you change the 'vi','VN' to (for example) 'en' 'US', the text will change from Vietnamese to English, so the button idea came from here


      locale: Locale('vi','VN'),
      


     //--------------------------------------
      debugShowCheckedModeBanner: false,
      routes: {
        '/1': (context) => TayLake(),
        '/2': (context) => DamW(),
        '/3': (context) => DakMilPrison(),
      },
      supportedLocales: [Locale('en', 'US'), Locale('vi', 'VN')],
      localizationsDelegates: [
        // THIS CLASS WILL BE ADDED LATER
        // A class which loads the translations from JSON files
        AppLocalizations.delegate,
        // Built-in localization of basic text for Material widgets
        GlobalMaterialLocalizations.delegate,
        // Built-in localization for text direction LTR/RTL
        GlobalWidgetsLocalizations.delegate,
      ],
      localeResolutionCallback: (locale, supportedLocales) {
        for (var supportedLocale in supportedLocales) {
          if (supportedLocale.languageCode == locale!.languageCode &&
              supportedLocale.countryCode == locale.countryCode) {
            return supportedLocale;
          }
        }
        return supportedLocales.first;
      },
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({
    Key? key,
  }) : super(key: key);

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

class _MyHomePageState extends State<MyHomePage> {
  bool _isClicked = false;
  String locale = 'vn';

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Column(
        children: [
          Stack(
            children: [
              Image.network(
                'https://t4.ftcdn.net/jpg/03/36/07/85/240_F_336078503_3rIlo0PPrbrxyPbR99qXVo6VATqWsHvl.jpg',
                filterQuality: FilterQuality.high,
                fit: BoxFit.fitWidth,
                width: 500,
              ),
              ListTile(
                leading:
                    Icon(Icons.menu_outlined, color: Colors.white, size: 30),
                  
                //The translation button

                 
                trailing: IconButton(
                  onPressed: () {
                    if (!_isClicked) {
                      setState(() {
                        _isClicked = true;
                        locale = 'vn';
                      });
                    } else {
                      setState(() {
                        _isClicked = false;
                        locale = 'en';
                      });
                    }
                  },



                    
                  icon: Icon(Icons.translate_outlined ,size: 15.0),
                ),
              ),
            ],
          ),
          Expanded(
            child: PageView(
              children: <Widget>[
                placeButton(
                    context,
                    "titleHoTay",
                    "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRLRsM_lVQtJSMBRgTzeyPIrqdeMFV7M3gCzA&usqp=CAU",
                    "/1"),
                placeButton(
                    context,
                    "titleDapW",
                    "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTabeU7lO71WhEE3EY99nU087Xwlj5PAPd76g&usqp=CAU",
                    "/2"),
                placeButton(
                    context,
                    "titleNhaNgucDm",
                    "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSzl8XkPISxI0Uqcy0FR6g3cRvtng1Ghu6T9w&usqp=CAUs",
                    "/3")
              ],
            ),
          ),
        ],
      ),
    );
  }
}

PS:如果你想问什么,请随时在下面问我。另外,我遵循此页面上的本地化教程(有关一些额外信息): https ://resocoder.com/2019/06/01/flutter-localization-the-easy-way-internationalization-with-json/

标签: flutterdartlocalizationinternationalization

解决方案


推荐阅读