搜索插件、代码仓与 APP 分类

flutter_locales 封面
插件详情v3.0.6

flutter_locales

轻松将您的应用本地化为多种语言

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
1390
喜欢数
59
Pub 分数
130

安装插件

flutter pub add flutter_locales

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

flutter_locales 是一个 Flutter 包,通过支持多种语言来简化应用程序的本地化。它提供了一种简单的方法来管理翻译、动态切换区域设置,并处理特定语言的格式化。该包与 Flutter 的国际化系统无缝集成,允许开发者定义翻译文件并通过简单的键访问本地化字符串。它支持所有主要平台,包括 Android、iOS、Web、macOS、Windows 和 Linux,非常适合需要多语言功能的跨平台应用。

适合场景

  • 多语言应用支持
  • 动态语言切换
  • 翻译文件管理
  • 跨平台本地化
  • 基于用户偏好选择语言

主要特点

  • 简单翻译键系统
  • 运行时区域设置切换
  • 支持复数形式
  • 与 Flutter 的 i18n 集成
  • 平台无关的设计

适合对象

  • 全球移动和网页应用
  • 面向多样化用户群体的应用程序
  • 新接触本地化的开发者
  • 需要快速国际化设置的项目
  • 频繁更新语言的应用程序

使用建议

  • 需要手动创建翻译文件
  • 有限的内置翻译工具
  • 无自动字符串提取
  • 最佳搭配外部翻译服务
  • 依赖于正确的文件结构遵循

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

flutter_locales

Localize your Flutter app to multiple locales within seconds

Why Flutter Locales

✅ Easily Localize your app
✅ Change App Locale within the app
✅ Get Last Changed locale on App starts
✅ Save Locale Language After changed buy LocaleNotifier
✅ Get Translation with LocaleText('key') Widget

Example App

Look at a Simple app at GitHub.

Example App

Video Tutorial

Watch the video

1) Create locales assets

Create an assets/locales folder at the root of your project and add your locales json files. like: Example app assets/locales

  • your locale files name shall be name of the language
    • like:
      • en.json For english locales
      • ps.json for pashto locales

2) Include package and assets

Include latest dependency of flutter_locales

dependencies:
  flutter:
    sdk: flutter
  flutter_locales:

Include assets/locales/ folder

flutter:
  uses-material-design: true
  assets:
    - assets/locales/

3) Initialize app

Replace your main app with

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Locales.init(['en', 'fa', 'ps']); // get last saved language
  // remove await if you want to get app default language

  runApp(MyApp());
}
  • ['en', 'fa', 'ps'] are language codes of .json files located in located in assets/locales folder
  • You can replace these languages with your languages

Wrap your MaterialApp with LocaleBuilder then provide locale to app

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return LocaleBuilder(
      builder: (locale) => MaterialApp(
        title: 'Flutter Locales',
        localizationsDelegates: Locales.delegates,
        supportedLocales: Locales.supportedLocales,
        locale: locale,
        home: HomeScreen(),
      ),
    );
  }
}
  • LocaleBuilder rebuild the app you change the app locale by Locales.change(context, 'fa')

Locale Text

LocaleText Widget Use to translate a key

LocaleText(`welcome`);
  • LocaleText Translate a key to string
Locale String
  • To get a key translated call
Locales.string(context, 'welcome')

// with extension
context.localeString('welcome');

Change App Locale

To change app locale language

Locales.change(context, 'fa');

//with extension
context.changeLocale('fa');
  • When you change app automatically saves at Locale
Current Locale Language
  • To get current locale call
Locales.currentLocale(context);

//with extension
context.currentLocale;

Getting Started

This project is a starting point for a Dart package, a library module containing code that can be shared easily across multiple Flutter or Dart projects.

For help getting started with Flutter, view our online documentation, which offers tutorials, samples, guidance on mobile development, and a full API reference.

所属分类

依赖

依赖清单 2 项

源码仓库

使用此插件的项目

暂无相关数据。