├── example ├── analysis_options.yaml ├── ios │ ├── Flutter │ │ ├── Debug.xcconfig │ │ ├── Release.xcconfig │ │ └── AppFrameworkInfo.plist │ ├── Runner │ │ ├── Runner-Bridging-Header.h │ │ ├── Assets.xcassets │ │ │ ├── LaunchImage.imageset │ │ │ │ ├── LaunchImage.png │ │ │ │ ├── LaunchImage@2x.png │ │ │ │ ├── LaunchImage@3x.png │ │ │ │ ├── README.md │ │ │ │ └── Contents.json │ │ │ └── AppIcon.appiconset │ │ │ │ ├── Icon-App-20x20@1x.png │ │ │ │ ├── Icon-App-20x20@2x.png │ │ │ │ ├── Icon-App-20x20@3x.png │ │ │ │ ├── Icon-App-29x29@1x.png │ │ │ │ ├── Icon-App-29x29@2x.png │ │ │ │ ├── Icon-App-29x29@3x.png │ │ │ │ ├── Icon-App-40x40@1x.png │ │ │ │ ├── Icon-App-40x40@2x.png │ │ │ │ ├── Icon-App-40x40@3x.png │ │ │ │ ├── Icon-App-60x60@2x.png │ │ │ │ ├── Icon-App-60x60@3x.png │ │ │ │ ├── Icon-App-76x76@1x.png │ │ │ │ ├── Icon-App-76x76@2x.png │ │ │ │ ├── Icon-App-1024x1024@1x.png │ │ │ │ ├── Icon-App-83.5x83.5@2x.png │ │ │ │ └── Contents.json │ │ ├── AppDelegate.swift │ │ ├── Base.lproj │ │ │ ├── Main.storyboard │ │ │ └── LaunchScreen.storyboard │ │ └── Info.plist │ ├── Runner.xcodeproj │ │ ├── project.xcworkspace │ │ │ ├── contents.xcworkspacedata │ │ │ └── xcshareddata │ │ │ │ ├── WorkspaceSettings.xcsettings │ │ │ │ └── IDEWorkspaceChecks.plist │ │ ├── xcshareddata │ │ │ └── xcschemes │ │ │ │ └── Runner.xcscheme │ │ └── project.pbxproj │ ├── Runner.xcworkspace │ │ ├── contents.xcworkspacedata │ │ └── xcshareddata │ │ │ ├── WorkspaceSettings.xcsettings │ │ │ └── IDEWorkspaceChecks.plist │ └── .gitignore ├── android │ ├── gradle.properties │ ├── app │ │ ├── src │ │ │ ├── main │ │ │ │ ├── res │ │ │ │ │ ├── mipmap-hdpi │ │ │ │ │ │ └── ic_launcher.png │ │ │ │ │ ├── mipmap-mdpi │ │ │ │ │ │ └── ic_launcher.png │ │ │ │ │ ├── mipmap-xhdpi │ │ │ │ │ │ └── ic_launcher.png │ │ │ │ │ ├── mipmap-xxhdpi │ │ │ │ │ │ └── ic_launcher.png │ │ │ │ │ ├── mipmap-xxxhdpi │ │ │ │ │ │ └── ic_launcher.png │ │ │ │ │ ├── drawable │ │ │ │ │ │ └── launch_background.xml │ │ │ │ │ ├── drawable-v21 │ │ │ │ │ │ └── launch_background.xml │ │ │ │ │ ├── values │ │ │ │ │ │ └── styles.xml │ │ │ │ │ └── values-night │ │ │ │ │ │ └── styles.xml │ │ │ │ ├── kotlin │ │ │ │ │ └── com │ │ │ │ │ │ └── example │ │ │ │ │ │ └── example │ │ │ │ │ │ └── MainActivity.kt │ │ │ │ └── AndroidManifest.xml │ │ │ ├── debug │ │ │ │ └── AndroidManifest.xml │ │ │ └── profile │ │ │ │ └── AndroidManifest.xml │ │ └── build.gradle │ ├── gradle │ │ └── wrapper │ │ │ └── gradle-wrapper.properties │ ├── .gitignore │ ├── settings.gradle │ └── build.gradle ├── localizations │ ├── en-US │ │ ├── counter.json │ │ └── homepage.json │ └── pt-BR │ │ ├── counter.json │ │ └── homepage.json ├── .metadata ├── pubspec.yaml ├── lib │ ├── localizations.dart │ └── main.dart └── .gitignore ├── lib ├── src │ ├── data_sources │ │ ├── localization_data_source.dart │ │ └── asset_bundle_data_source.dart │ ├── resource_store.dart │ ├── i18next_localization_delegate.dart │ ├── translator.dart │ ├── i18next.dart │ ├── plural_resolver.dart │ └── options.dart ├── i18next.dart ├── utils.dart └── interpolator.dart ├── .metadata ├── pubspec.yaml ├── .github └── workflows │ └── build.yaml ├── .gitignore ├── test ├── utils_test.dart ├── data_sources │ ├── asset_bundle_data_source_test.mocks.dart │ └── asset_bundle_data_source_test.dart ├── i18next_test.mocks.dart ├── i18next_localization_delegate_test.mocks.dart ├── i18next_localization_delegate_test.dart ├── resource_store_test.dart ├── options_test.dart ├── interpolator_test.dart └── i18next_test.dart ├── CHANGELOG.md ├── README.md ├── analysis_options.yaml └── LICENSE /example/analysis_options.yaml: -------------------------------------------------------------------------------- 1 | include: ../analysis_options.yaml 2 | -------------------------------------------------------------------------------- /example/ios/Flutter/Debug.xcconfig: -------------------------------------------------------------------------------- 1 | #include "Generated.xcconfig" 2 | -------------------------------------------------------------------------------- /example/ios/Flutter/Release.xcconfig: -------------------------------------------------------------------------------- 1 | #include "Generated.xcconfig" 2 | -------------------------------------------------------------------------------- /example/ios/Runner/Runner-Bridging-Header.h: -------------------------------------------------------------------------------- 1 | #import "GeneratedPluginRegistrant.h" 2 | -------------------------------------------------------------------------------- /example/android/gradle.properties: -------------------------------------------------------------------------------- 1 | org.gradle.jvmargs=-Xmx1536M 2 | android.useAndroidX=true 3 | android.enableJetifier=true 4 | -------------------------------------------------------------------------------- /example/android/app/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nubank/i18next/HEAD/example/android/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /example/android/app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nubank/i18next/HEAD/example/android/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /example/android/app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nubank/i18next/HEAD/example/android/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /example/android/app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nubank/i18next/HEAD/example/android/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /example/android/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nubank/i18next/HEAD/example/android/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /lib/src/data_sources/localization_data_source.dart: -------------------------------------------------------------------------------- 1 | import 'dart:ui'; 2 | 3 | abstract class LocalizationDataSource { 4 | Future> load(Locale locale); 5 | } 6 | -------------------------------------------------------------------------------- /example/ios/Runner/Assets.xcassets/LaunchImage.imageset/LaunchImage.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nubank/i18next/HEAD/example/ios/Runner/Assets.xcassets/LaunchImage.imageset/LaunchImage.png -------------------------------------------------------------------------------- /example/ios/Runner/Assets.xcassets/LaunchImage.imageset/LaunchImage@2x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nubank/i18next/HEAD/example/ios/Runner/Assets.xcassets/LaunchImage.imageset/LaunchImage@2x.png -------------------------------------------------------------------------------- /example/ios/Runner/Assets.xcassets/LaunchImage.imageset/LaunchImage@3x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nubank/i18next/HEAD/example/ios/Runner/Assets.xcassets/LaunchImage.imageset/LaunchImage@3x.png -------------------------------------------------------------------------------- /example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-20x20@1x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nubank/i18next/HEAD/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-20x20@1x.png -------------------------------------------------------------------------------- /example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-20x20@2x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nubank/i18next/HEAD/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-20x20@2x.png -------------------------------------------------------------------------------- /example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-20x20@3x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nubank/i18next/HEAD/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-20x20@3x.png -------------------------------------------------------------------------------- /example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-29x29@1x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nubank/i18next/HEAD/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-29x29@1x.png -------------------------------------------------------------------------------- /example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-29x29@2x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nubank/i18next/HEAD/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-29x29@2x.png -------------------------------------------------------------------------------- /example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-29x29@3x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nubank/i18next/HEAD/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-29x29@3x.png -------------------------------------------------------------------------------- /example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-40x40@1x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nubank/i18next/HEAD/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-40x40@1x.png -------------------------------------------------------------------------------- /example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-40x40@2x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nubank/i18next/HEAD/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-40x40@2x.png -------------------------------------------------------------------------------- /example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-40x40@3x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nubank/i18next/HEAD/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-40x40@3x.png -------------------------------------------------------------------------------- /example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-60x60@2x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nubank/i18next/HEAD/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-60x60@2x.png -------------------------------------------------------------------------------- /example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-60x60@3x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nubank/i18next/HEAD/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-60x60@3x.png -------------------------------------------------------------------------------- /example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-76x76@1x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nubank/i18next/HEAD/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-76x76@1x.png -------------------------------------------------------------------------------- /example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-76x76@2x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nubank/i18next/HEAD/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-76x76@2x.png -------------------------------------------------------------------------------- /example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-1024x1024@1x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nubank/i18next/HEAD/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-1024x1024@1x.png -------------------------------------------------------------------------------- /example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-83.5x83.5@2x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nubank/i18next/HEAD/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-83.5x83.5@2x.png -------------------------------------------------------------------------------- /example/localizations/en-US/counter.json: -------------------------------------------------------------------------------- 1 | { 2 | "clickMe": "Click me", 3 | "clicked": "You clicked {{count}} time!", 4 | "clicked_plural": "You clicked {{count}} times!", 5 | "resetCounter": "Reset Counter" 6 | } -------------------------------------------------------------------------------- /example/android/app/src/main/kotlin/com/example/example/MainActivity.kt: -------------------------------------------------------------------------------- 1 | package com.example.example 2 | 3 | import io.flutter.embedding.android.FlutterActivity 4 | 5 | class MainActivity: FlutterActivity() { 6 | } 7 | -------------------------------------------------------------------------------- /example/localizations/pt-BR/counter.json: -------------------------------------------------------------------------------- 1 | { 2 | "clickMe": "Clique em mim", 3 | "clicked": "Você clicou {{count}} vez!", 4 | "clicked_plural": "Você clicou {{count}} vezes!", 5 | "resetCounter": "Resetar contador" 6 | } -------------------------------------------------------------------------------- /example/ios/Runner.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /example/ios/Runner.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /example/localizations/pt-BR/homepage.json: -------------------------------------------------------------------------------- 1 | { 2 | "genderMessage": "Eles", 3 | "genderMessage_female": "Ela", 4 | "genderMessage_male": "Ele", 5 | "today": "Hoje é {{date, dd/MM/yyyy}}", 6 | "helloMessage": "Olá {{name}} no {{world, uppercase}} world", 7 | "title": "Olá pt_BR" 8 | } -------------------------------------------------------------------------------- /example/localizations/en-US/homepage.json: -------------------------------------------------------------------------------- 1 | { 2 | "genderMessage": "They", 3 | "genderMessage_female": "Her", 4 | "genderMessage_male": "Him", 5 | "today": "Today is {{date, dd/MM/yyyy}}", 6 | "helloMessage": "Hello {{name}} in the {{world, uppercase}} world", 7 | "title": "Hello en_US" 8 | } -------------------------------------------------------------------------------- /example/android/gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Fri Jun 23 08:50:38 CEST 2017 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-6.7-all.zip 7 | -------------------------------------------------------------------------------- /lib/i18next.dart: -------------------------------------------------------------------------------- 1 | library i18next; 2 | 3 | export 'src/data_sources/asset_bundle_data_source.dart'; 4 | export 'src/data_sources/localization_data_source.dart'; 5 | export 'src/i18next.dart'; 6 | export 'src/i18next_localization_delegate.dart'; 7 | export 'src/options.dart'; 8 | export 'src/resource_store.dart'; 9 | -------------------------------------------------------------------------------- /example/ios/Runner.xcworkspace/xcshareddata/WorkspaceSettings.xcsettings: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | PreviewsEnabled 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /example/ios/Runner.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | IDEDidComputeMac32BitWarning 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /example/android/.gitignore: -------------------------------------------------------------------------------- 1 | gradle-wrapper.jar 2 | /.gradle 3 | /captures/ 4 | /gradlew 5 | /gradlew.bat 6 | /local.properties 7 | GeneratedPluginRegistrant.java 8 | 9 | # Remember to never publicly share your keystore. 10 | # See https://flutter.dev/docs/deployment/android#reference-the-keystore-from-the-app 11 | key.properties 12 | -------------------------------------------------------------------------------- /example/ios/Runner.xcodeproj/project.xcworkspace/xcshareddata/WorkspaceSettings.xcsettings: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | PreviewsEnabled 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /example/ios/Runner.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | IDEDidComputeMac32BitWarning 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /.metadata: -------------------------------------------------------------------------------- 1 | # This file tracks properties of this Flutter project. 2 | # Used by Flutter tool to assess capabilities and perform upgrades etc. 3 | # 4 | # This file should be version controlled and should not be manually edited. 5 | 6 | version: 7 | revision: c382b8e990b6976f610764179f94e0416d82c057 8 | channel: unknown 9 | 10 | project_type: package 11 | -------------------------------------------------------------------------------- /example/.metadata: -------------------------------------------------------------------------------- 1 | # This file tracks properties of this Flutter project. 2 | # Used by Flutter tool to assess capabilities and perform upgrades etc. 3 | # 4 | # This file should be version controlled and should not be manually edited. 5 | 6 | version: 7 | revision: c382b8e990b6976f610764179f94e0416d82c057 8 | channel: unknown 9 | 10 | project_type: app 11 | -------------------------------------------------------------------------------- /example/android/app/src/debug/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 3 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /example/android/app/src/profile/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 3 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /example/ios/Runner/Assets.xcassets/LaunchImage.imageset/README.md: -------------------------------------------------------------------------------- 1 | # Launch Screen Assets 2 | 3 | You can customize the launch screen with your own desired assets by replacing the image files in this directory. 4 | 5 | You can also do it by opening your Flutter project's Xcode project with `open ios/Runner.xcworkspace`, selecting `Runner/Assets.xcassets` in the Project Navigator and dropping in the desired images. -------------------------------------------------------------------------------- /example/ios/Runner/AppDelegate.swift: -------------------------------------------------------------------------------- 1 | import UIKit 2 | import Flutter 3 | 4 | @UIApplicationMain 5 | @objc class AppDelegate: FlutterAppDelegate { 6 | override func application( 7 | _ application: UIApplication, 8 | didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? 9 | ) -> Bool { 10 | GeneratedPluginRegistrant.register(with: self) 11 | return super.application(application, didFinishLaunchingWithOptions: launchOptions) 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /example/android/settings.gradle: -------------------------------------------------------------------------------- 1 | include ':app' 2 | 3 | def localPropertiesFile = new File(rootProject.projectDir, "local.properties") 4 | def properties = new Properties() 5 | 6 | assert localPropertiesFile.exists() 7 | localPropertiesFile.withReader("UTF-8") { reader -> properties.load(reader) } 8 | 9 | def flutterSdkPath = properties.getProperty("flutter.sdk") 10 | assert flutterSdkPath != null, "flutter.sdk not set in local.properties" 11 | apply from: "$flutterSdkPath/packages/flutter_tools/gradle/app_plugin_loader.gradle" 12 | -------------------------------------------------------------------------------- /example/android/app/src/main/res/drawable/launch_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 12 | 13 | -------------------------------------------------------------------------------- /example/android/app/src/main/res/drawable-v21/launch_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 12 | 13 | -------------------------------------------------------------------------------- /pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: i18next 2 | description: A localization formatter based on the i18next standard. It is not yet a fully i18n tool only the formatting itself. 3 | version: 0.5.2 4 | repository: https://github.com/nubank/i18next 5 | homepage: https://github.com/nubank/i18next 6 | 7 | environment: 8 | sdk: ">=2.12.0 <3.0.0" 9 | 10 | dependencies: 11 | flutter: 12 | sdk: flutter 13 | path: ^1.8.0 14 | 15 | dev_dependencies: 16 | build_runner: ^1.11.0 17 | flutter_test: 18 | sdk: flutter 19 | mockito: ^5.0.3 20 | 21 | flutter: 22 | -------------------------------------------------------------------------------- /example/ios/Runner/Assets.xcassets/LaunchImage.imageset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "universal", 5 | "filename" : "LaunchImage.png", 6 | "scale" : "1x" 7 | }, 8 | { 9 | "idiom" : "universal", 10 | "filename" : "LaunchImage@2x.png", 11 | "scale" : "2x" 12 | }, 13 | { 14 | "idiom" : "universal", 15 | "filename" : "LaunchImage@3x.png", 16 | "scale" : "3x" 17 | } 18 | ], 19 | "info" : { 20 | "version" : 1, 21 | "author" : "xcode" 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /lib/utils.dart: -------------------------------------------------------------------------------- 1 | // This is a collection of utility methods that are not explicitly 2 | // exported by the package, but could be used by the dependants. 3 | 4 | /// Given a [path] list, this method navigates through [data] and returns 5 | /// the last path, or null otherwise. 6 | /// 7 | /// e.g.: 8 | /// ['my', 'key'] + {'my': {'key': 'Value!'}} = 'Value!' 9 | Object? evaluate(Iterable path, Map? data) { 10 | Object? object = data; 11 | for (final current in path) { 12 | if (object is Map && object.containsKey(current)) { 13 | object = object[current]; 14 | } else { 15 | object = null; 16 | break; 17 | } 18 | } 19 | return object; 20 | } 21 | -------------------------------------------------------------------------------- /example/ios/.gitignore: -------------------------------------------------------------------------------- 1 | *.mode1v3 2 | *.mode2v3 3 | *.moved-aside 4 | *.pbxuser 5 | *.perspectivev3 6 | **/*sync/ 7 | .sconsign.dblite 8 | .tags* 9 | **/.vagrant/ 10 | **/DerivedData/ 11 | Icon? 12 | **/Pods/ 13 | **/.symlinks/ 14 | profile 15 | xcuserdata 16 | **/.generated/ 17 | Flutter/App.framework 18 | Flutter/Flutter.framework 19 | Flutter/Flutter.podspec 20 | Flutter/Generated.xcconfig 21 | Flutter/app.flx 22 | Flutter/app.zip 23 | Flutter/flutter_assets/ 24 | Flutter/flutter_export_environment.sh 25 | ServiceDefinitions.json 26 | Runner/GeneratedPluginRegistrant.* 27 | 28 | # Exceptions to above rules. 29 | !default.mode1v3 30 | !default.mode2v3 31 | !default.pbxuser 32 | !default.perspectivev3 33 | -------------------------------------------------------------------------------- /example/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: example 2 | description: I18next example project 3 | 4 | version: 0.1.0 5 | publish_to: none 6 | 7 | environment: 8 | sdk: ">=2.12.0 <3.0.0" 9 | 10 | dependencies: 11 | flutter: 12 | sdk: flutter 13 | flutter_localizations: 14 | sdk: flutter 15 | i18next: 16 | path: ../ 17 | 18 | dev_dependencies: 19 | flutter_test: 20 | sdk: flutter 21 | 22 | flutter: 23 | uses-material-design: true 24 | assets: 25 | # flutter asset generator/resolver isn't able to deal with wildcards yet 26 | # due to how asset resolution works (1x, 2x, 3x, ...) 27 | # for now, we'll have to add them manually 28 | - localizations/en-US/ 29 | - localizations/pt-BR/ 30 | -------------------------------------------------------------------------------- /example/android/build.gradle: -------------------------------------------------------------------------------- 1 | buildscript { 2 | ext.kotlin_version = '1.3.50' 3 | repositories { 4 | google() 5 | jcenter() 6 | } 7 | 8 | dependencies { 9 | classpath 'com.android.tools.build:gradle:4.1.0' 10 | classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" 11 | } 12 | } 13 | 14 | allprojects { 15 | repositories { 16 | google() 17 | jcenter() 18 | } 19 | } 20 | 21 | rootProject.buildDir = '../build' 22 | subprojects { 23 | project.buildDir = "${rootProject.buildDir}/${project.name}" 24 | } 25 | subprojects { 26 | project.evaluationDependsOn(':app') 27 | } 28 | 29 | task clean(type: Delete) { 30 | delete rootProject.buildDir 31 | } 32 | -------------------------------------------------------------------------------- /.github/workflows/build.yaml: -------------------------------------------------------------------------------- 1 | name: build 2 | 3 | on: 4 | push: 5 | branches: 6 | - master 7 | pull_request: 8 | branches: 9 | - master 10 | 11 | jobs: 12 | build: 13 | runs-on: ubuntu-latest 14 | steps: 15 | - uses: actions/checkout@v2 16 | - uses: subosito/flutter-action@v1.4.0 17 | with: 18 | flutter-version: '2.0.3' 19 | - name: Install Dependencies 20 | run: flutter packages get 21 | - name: Format 22 | run: flutter format --set-exit-if-changed lib test 23 | - name: Analyze 24 | run: flutter analyze lib test 25 | - name: Run tests 26 | run: flutter test --no-pub --coverage --test-randomize-ordering-seed random 27 | - name: Upload coverage to Codecov 28 | uses: codecov/codecov-action@v1 29 | -------------------------------------------------------------------------------- /example/ios/Flutter/AppFrameworkInfo.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | App 9 | CFBundleIdentifier 10 | io.flutter.flutter.app 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | App 15 | CFBundlePackageType 16 | FMWK 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleSignature 20 | ???? 21 | CFBundleVersion 22 | 1.0 23 | MinimumOSVersion 24 | 8.0 25 | 26 | 27 | -------------------------------------------------------------------------------- /example/android/app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 9 | 15 | 18 | 19 | -------------------------------------------------------------------------------- /example/android/app/src/main/res/values-night/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 9 | 15 | 18 | 19 | -------------------------------------------------------------------------------- /example/lib/localizations.dart: -------------------------------------------------------------------------------- 1 | import 'package:flutter/widgets.dart'; 2 | import 'package:i18next/i18next.dart'; 3 | 4 | class HomePageL10n { 5 | const HomePageL10n(this.i18next); 6 | 7 | final I18Next i18next; 8 | 9 | static HomePageL10n of(BuildContext context) => 10 | HomePageL10n(I18Next.of(context)!); 11 | 12 | String get title => i18next.t('homepage:title'); 13 | 14 | String today(DateTime date) => 15 | i18next.t('homepage:today', variables: {'date': date}); 16 | 17 | String hello({required String name, required String world}) => i18next.t( 18 | 'homepage:helloMessage', 19 | variables: {'name': name, 'world': world}, 20 | ); 21 | 22 | String gendered(String gender) => 23 | i18next.t('homepage:genderMessage', context: gender); 24 | } 25 | 26 | class CounterL10n { 27 | const CounterL10n(this.i18next); 28 | 29 | final I18Next i18next; 30 | 31 | static CounterL10n of(BuildContext context) => 32 | CounterL10n(I18Next.of(context)!); 33 | 34 | String clicked(int count) => i18next.t('counter:clicked', count: count); 35 | 36 | String get clickMe => i18next.t('counter:clickMe'); 37 | 38 | String get resetCounter => i18next.t('counter:resetCounter'); 39 | } 40 | -------------------------------------------------------------------------------- /example/ios/Runner/Base.lproj/Main.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /example/ios/Runner/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | $(DEVELOPMENT_LANGUAGE) 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | example 15 | CFBundlePackageType 16 | APPL 17 | CFBundleShortVersionString 18 | $(FLUTTER_BUILD_NAME) 19 | CFBundleSignature 20 | ???? 21 | CFBundleVersion 22 | $(FLUTTER_BUILD_NUMBER) 23 | LSRequiresIPhoneOS 24 | 25 | UILaunchStoryboardName 26 | LaunchScreen 27 | UIMainStoryboardFile 28 | Main 29 | UISupportedInterfaceOrientations 30 | 31 | UIInterfaceOrientationPortrait 32 | UIInterfaceOrientationLandscapeLeft 33 | UIInterfaceOrientationLandscapeRight 34 | 35 | UISupportedInterfaceOrientations~ipad 36 | 37 | UIInterfaceOrientationPortrait 38 | UIInterfaceOrientationPortraitUpsideDown 39 | UIInterfaceOrientationLandscapeLeft 40 | UIInterfaceOrientationLandscapeRight 41 | 42 | UIViewControllerBasedStatusBarAppearance 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /example/.gitignore: -------------------------------------------------------------------------------- 1 | # Miscellaneous 2 | *.class 3 | *.log 4 | *.pyc 5 | *.swp 6 | .DS_Store 7 | .atom/ 8 | .buildlog/ 9 | .history 10 | .svn/ 11 | 12 | # IntelliJ related 13 | *.iml 14 | *.ipr 15 | *.iws 16 | .idea/ 17 | 18 | # The .vscode folder contains launch configuration and tasks you configure in 19 | # VS Code which you may wish to be included in version control, so this line 20 | # is commented out by default. 21 | #.vscode/ 22 | 23 | # Flutter/Dart/Pub related 24 | **/doc/api/ 25 | .dart_tool/ 26 | .flutter-plugins 27 | .packages 28 | .pub-cache/ 29 | .pub/ 30 | /build/ 31 | 32 | # Android related 33 | **/android/**/gradle-wrapper.jar 34 | **/android/.gradle 35 | **/android/captures/ 36 | **/android/gradlew 37 | **/android/gradlew.bat 38 | **/android/local.properties 39 | **/android/**/GeneratedPluginRegistrant.java 40 | 41 | # iOS/XCode related 42 | **/ios/**/*.mode1v3 43 | **/ios/**/*.mode2v3 44 | **/ios/**/*.moved-aside 45 | **/ios/**/*.pbxuser 46 | **/ios/**/*.perspectivev3 47 | **/ios/**/*sync/ 48 | **/ios/**/.sconsign.dblite 49 | **/ios/**/.tags* 50 | **/ios/**/.vagrant/ 51 | **/ios/**/DerivedData/ 52 | **/ios/**/Icon? 53 | **/ios/**/Pods/ 54 | **/ios/**/.symlinks/ 55 | **/ios/**/profile 56 | **/ios/**/xcuserdata 57 | **/ios/.generated/ 58 | **/ios/Flutter/App.framework 59 | **/ios/Flutter/Flutter.framework 60 | **/ios/Flutter/Generated.xcconfig 61 | **/ios/Flutter/app.flx 62 | **/ios/Flutter/app.zip 63 | **/ios/Flutter/flutter_assets/ 64 | **/ios/Flutter/flutter_export_environment.sh 65 | **/ios/ServiceDefinitions.json 66 | **/ios/Runner/GeneratedPluginRegistrant.* 67 | 68 | # Exceptions to above rules. 69 | !**/ios/**/default.mode1v3 70 | !**/ios/**/default.mode2v3 71 | !**/ios/**/default.pbxuser 72 | !**/ios/**/default.perspectivev3 73 | !/packages/flutter_tools/test/data/dart_dependencies_test/**/.packages 74 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Miscellaneous 2 | *.class 3 | *.log 4 | *.pyc 5 | *.swp 6 | .DS_Store 7 | .atom/ 8 | .buildlog/ 9 | .history 10 | .svn/ 11 | 12 | # IntelliJ related 13 | *.iml 14 | *.ipr 15 | *.iws 16 | .idea/ 17 | 18 | # The .vscode folder contains launch configuration and tasks you configure in 19 | # VS Code which you may wish to be included in version control, so this line 20 | # is commented out by default. 21 | #.vscode/ 22 | 23 | # Flutter/Dart/Pub related 24 | **/doc/api/ 25 | .dart_tool/ 26 | .flutter-plugins 27 | .packages 28 | .pub-cache/ 29 | .pub/ 30 | build/ 31 | pubspec.lock 32 | 33 | # Android related 34 | **/android/**/gradle-wrapper.jar 35 | **/android/.gradle 36 | **/android/captures/ 37 | **/android/gradlew 38 | **/android/gradlew.bat 39 | **/android/local.properties 40 | **/android/**/GeneratedPluginRegistrant.java 41 | 42 | # iOS/XCode related 43 | **/ios/**/*.mode1v3 44 | **/ios/**/*.mode2v3 45 | **/ios/**/*.moved-aside 46 | **/ios/**/*.pbxuser 47 | **/ios/**/*.perspectivev3 48 | **/ios/**/*sync/ 49 | **/ios/**/.sconsign.dblite 50 | **/ios/**/.tags* 51 | **/ios/**/.vagrant/ 52 | **/ios/**/DerivedData/ 53 | **/ios/**/Icon? 54 | **/ios/**/Pods/ 55 | **/ios/**/.symlinks/ 56 | **/ios/**/profile 57 | **/ios/**/xcuserdata 58 | **/ios/.generated/ 59 | **/ios/Flutter/App.framework 60 | **/ios/Flutter/Flutter.framework 61 | **/ios/Flutter/Generated.xcconfig 62 | **/ios/Flutter/app.flx 63 | **/ios/Flutter/app.zip 64 | **/ios/Flutter/flutter_assets/ 65 | **/ios/Flutter/flutter_export_environment.sh 66 | **/ios/ServiceDefinitions.json 67 | **/ios/Runner/GeneratedPluginRegistrant.* 68 | 69 | # Exceptions to above rules. 70 | !**/ios/**/default.mode1v3 71 | !**/ios/**/default.mode2v3 72 | !**/ios/**/default.pbxuser 73 | !**/ios/**/default.perspectivev3 74 | !/packages/flutter_tools/test/data/dart_dependencies_test/**/.packages 75 | -------------------------------------------------------------------------------- /test/utils_test.dart: -------------------------------------------------------------------------------- 1 | import 'dart:ui'; 2 | 3 | import 'package:flutter_test/flutter_test.dart'; 4 | import 'package:i18next/utils.dart'; 5 | 6 | void main() { 7 | group('evaluate', () { 8 | const locale = Locale('any'); 9 | 10 | final level2 = { 11 | 'key': 'Second level leaf', 12 | }; 13 | final level1 = { 14 | 'key': 'First level leaf', 15 | 'nested': level2, 16 | }; 17 | final data = { 18 | 'key': 'Zero level leaf', 19 | locale: level1, 20 | }; 21 | 22 | test('given empty path', () { 23 | expect(evaluate([], data), data); 24 | }); 25 | 26 | test('given null data', () { 27 | expect(evaluate([], null), isNull); 28 | expect(evaluate(['key'], null), isNull); 29 | expect(evaluate(['key', 'nested'], null), isNull); 30 | }); 31 | 32 | test('given a non matching path', () { 33 | expect(evaluate(['invalid'], data), isNull); 34 | expect(evaluate(['key', 'invalid'], data), isNull); 35 | expect(evaluate(['key', 'invalid', 'another'], data), isNull); 36 | }); 37 | 38 | test('given leaf matching path', () { 39 | expect(evaluate(['key'], data), 'Zero level leaf'); 40 | expect(evaluate([locale, 'key'], data), 'First level leaf'); 41 | expect(evaluate([locale, 'nested', 'key'], data), 'Second level leaf'); 42 | }); 43 | 44 | test('given under matching path', () { 45 | expect(evaluate([locale], data), level1); 46 | expect(evaluate([locale, 'nested'], data), level2); 47 | }); 48 | 49 | test('given over matching path', () { 50 | expect(evaluate(['key', 'another'], data), isNull); 51 | expect(evaluate([locale, 'key', 'another'], data), isNull); 52 | expect(evaluate([locale, 'nested', 'key', 'another'], data), isNull); 53 | }); 54 | }); 55 | } 56 | -------------------------------------------------------------------------------- /test/data_sources/asset_bundle_data_source_test.mocks.dart: -------------------------------------------------------------------------------- 1 | // Mocks generated by Mockito 5.0.3 from annotations 2 | // in i18next/test/data_sources/asset_bundle_data_source_test.dart. 3 | // Do not manually edit this file. 4 | 5 | import 'dart:async' as _i4; 6 | import 'dart:typed_data' as _i2; 7 | 8 | import 'package:flutter/src/services/asset_bundle.dart' as _i3; 9 | import 'package:mockito/mockito.dart' as _i1; 10 | 11 | // ignore_for_file: comment_references 12 | // ignore_for_file: unnecessary_parenthesis 13 | 14 | class _FakeByteData extends _i1.Fake {} 15 | 16 | /// A class which mocks [AssetBundle]. 17 | /// 18 | /// See the documentation for Mockito's code generation for more information. 19 | class MockAssetBundle extends _i1.Mock implements _i3.AssetBundle { 20 | MockAssetBundle() { 21 | _i1.throwOnMissingStub(this); 22 | } 23 | 24 | @override 25 | _i4.Future<_i2.ByteData> load(String? key) => (super.noSuchMethod( 26 | Invocation.method(#load, [key]), 27 | returnValue: Future.value(_FakeByteData())) as _i4.Future<_i2.ByteData>); 28 | @override 29 | _i4.Future loadString(String? key, {bool? cache = true}) => (super 30 | .noSuchMethod(Invocation.method(#loadString, [key], {#cache: cache}), 31 | returnValue: Future.value('')) as _i4.Future); 32 | @override 33 | _i4.Future loadStructuredData( 34 | String? key, _i4.Future Function(String)? parser) => 35 | (super.noSuchMethod(Invocation.method(#loadStructuredData, [key, parser]), 36 | returnValue: Future.value(null)) as _i4.Future); 37 | @override 38 | void evict(String? key) => 39 | super.noSuchMethod(Invocation.method(#evict, [key]), 40 | returnValueForMissingStub: null); 41 | @override 42 | String toString() => 43 | (super.noSuchMethod(Invocation.method(#toString, []), returnValue: '') 44 | as String); 45 | } 46 | -------------------------------------------------------------------------------- /example/android/app/build.gradle: -------------------------------------------------------------------------------- 1 | def localProperties = new Properties() 2 | def localPropertiesFile = rootProject.file('local.properties') 3 | if (localPropertiesFile.exists()) { 4 | localPropertiesFile.withReader('UTF-8') { reader -> 5 | localProperties.load(reader) 6 | } 7 | } 8 | 9 | def flutterRoot = localProperties.getProperty('flutter.sdk') 10 | if (flutterRoot == null) { 11 | throw new GradleException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.") 12 | } 13 | 14 | def flutterVersionCode = localProperties.getProperty('flutter.versionCode') 15 | if (flutterVersionCode == null) { 16 | flutterVersionCode = '1' 17 | } 18 | 19 | def flutterVersionName = localProperties.getProperty('flutter.versionName') 20 | if (flutterVersionName == null) { 21 | flutterVersionName = '1.0' 22 | } 23 | 24 | apply plugin: 'com.android.application' 25 | apply plugin: 'kotlin-android' 26 | apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle" 27 | 28 | android { 29 | compileSdkVersion 30 30 | 31 | sourceSets { 32 | main.java.srcDirs += 'src/main/kotlin' 33 | } 34 | 35 | defaultConfig { 36 | // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html). 37 | applicationId "com.example.example" 38 | minSdkVersion 16 39 | targetSdkVersion 30 40 | versionCode flutterVersionCode.toInteger() 41 | versionName flutterVersionName 42 | } 43 | 44 | buildTypes { 45 | release { 46 | // TODO: Add your own signing config for the release build. 47 | // Signing with the debug keys for now, so `flutter run --release` works. 48 | signingConfig signingConfigs.debug 49 | } 50 | } 51 | } 52 | 53 | flutter { 54 | source '../..' 55 | } 56 | 57 | dependencies { 58 | implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" 59 | } 60 | -------------------------------------------------------------------------------- /example/android/app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 3 | 6 | 13 | 17 | 21 | 26 | 30 | 31 | 32 | 33 | 34 | 35 | 37 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /example/ios/Runner/Base.lproj/LaunchScreen.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | -------------------------------------------------------------------------------- /lib/src/resource_store.dart: -------------------------------------------------------------------------------- 1 | import 'package:flutter/foundation.dart'; 2 | import 'package:flutter/widgets.dart'; 3 | 4 | import '../utils.dart'; 5 | import 'options.dart'; 6 | 7 | /// This store handles the access to a specific resource (e.g. String) or a 8 | /// bundle (e.g. namespaces) depending on the levels transversed. 9 | /// 10 | /// The access is done by [Locale], Namespace, and key in that order. 11 | class ResourceStore { 12 | ResourceStore({Map>? data}) 13 | : _data = data ?? {}, 14 | super(); 15 | 16 | final Map> _data; 17 | 18 | /// Registers the [namespace] to the store for the given [locale]. 19 | /// 20 | /// [locale], [namespace], and [data] cannot be null. 21 | void addNamespace( 22 | Locale locale, 23 | String namespace, 24 | Map data, 25 | ) { 26 | _data[locale] ??= {}; 27 | _data[locale]?[namespace] = data; 28 | } 29 | 30 | /// Removes [namespace] given [locale] from the store. 31 | void removeNamespace(Locale locale, String namespace) { 32 | _data[locale]?.remove(namespace); 33 | } 34 | 35 | /// Unregisters the [locale] from the store and from the [cache]. 36 | Future removeLocale(Locale locale) async { 37 | _data.remove(locale); 38 | } 39 | 40 | /// Unregisters all locales from the store and from the [cache]. 41 | Future removeAll() async { 42 | _data.clear(); 43 | } 44 | 45 | /// Whether [locale] and [namespace] are registered in this store. 46 | bool isNamespaceRegistered(Locale locale, String namespace) => 47 | isLocaleRegistered(locale) && _data[locale]?[namespace] != null; 48 | 49 | /// Whether [locale] is registered in this store. 50 | bool isLocaleRegistered(Locale locale) => _data[locale] != null; 51 | 52 | /// Attempts to retrieve a value given [Locale] in [options], [namespace], 53 | /// and [key]. 54 | /// 55 | /// - [key] cannot be null and it is split by [I18NextOptions.keySeparator] 56 | /// when creating a navigation path. 57 | /// 58 | /// Returns null if not found. 59 | String? retrieve( 60 | Locale locale, 61 | String namespace, 62 | String key, 63 | I18NextOptions options, 64 | ) { 65 | final keySeparator = options.keySeparator ?? '.'; 66 | final path = [ 67 | locale, 68 | namespace, 69 | ...key.split(keySeparator), 70 | ]; 71 | final value = evaluate(path, _data); 72 | return value is String ? value : null; 73 | } 74 | 75 | @override 76 | bool operator ==(Object other) => 77 | identical(this, other) || 78 | other is ResourceStore && 79 | runtimeType == other.runtimeType && 80 | mapEquals(_data, other._data); 81 | 82 | @override 83 | int get hashCode => _data.hashCode; 84 | } 85 | -------------------------------------------------------------------------------- /test/i18next_test.mocks.dart: -------------------------------------------------------------------------------- 1 | // Mocks generated by Mockito 5.0.3 from annotations 2 | // in i18next/test/i18next_test.dart. 3 | // Do not manually edit this file. 4 | 5 | import 'dart:async' as _i4; 6 | import 'dart:ui' as _i3; 7 | 8 | import 'package:i18next/src/data_sources/localization_data_source.dart' as _i6; 9 | import 'package:i18next/src/options.dart' as _i5; 10 | import 'package:i18next/src/resource_store.dart' as _i2; 11 | import 'package:mockito/mockito.dart' as _i1; 12 | 13 | // ignore_for_file: comment_references 14 | // ignore_for_file: unnecessary_parenthesis 15 | 16 | /// A class which mocks [ResourceStore]. 17 | /// 18 | /// See the documentation for Mockito's code generation for more information. 19 | class MockResourceStore extends _i1.Mock implements _i2.ResourceStore { 20 | MockResourceStore() { 21 | _i1.throwOnMissingStub(this); 22 | } 23 | 24 | @override 25 | void addNamespace( 26 | _i3.Locale? locale, String? namespace, Map? data) => 27 | super.noSuchMethod( 28 | Invocation.method(#addNamespace, [locale, namespace, data]), 29 | returnValueForMissingStub: null); 30 | @override 31 | void removeNamespace(_i3.Locale? locale, String? namespace) => super 32 | .noSuchMethod(Invocation.method(#removeNamespace, [locale, namespace]), 33 | returnValueForMissingStub: null); 34 | @override 35 | _i4.Future removeLocale(_i3.Locale? locale) => 36 | (super.noSuchMethod(Invocation.method(#removeLocale, [locale]), 37 | returnValue: Future.value(null), 38 | returnValueForMissingStub: Future.value()) as _i4.Future); 39 | @override 40 | _i4.Future removeAll() => 41 | (super.noSuchMethod(Invocation.method(#removeAll, []), 42 | returnValue: Future.value(null), 43 | returnValueForMissingStub: Future.value()) as _i4.Future); 44 | @override 45 | bool isNamespaceRegistered(_i3.Locale? locale, String? namespace) => 46 | (super.noSuchMethod( 47 | Invocation.method(#isNamespaceRegistered, [locale, namespace]), 48 | returnValue: false) as bool); 49 | @override 50 | bool isLocaleRegistered(_i3.Locale? locale) => 51 | (super.noSuchMethod(Invocation.method(#isLocaleRegistered, [locale]), 52 | returnValue: false) as bool); 53 | @override 54 | String? retrieve(_i3.Locale? locale, String? namespace, String? key, 55 | _i5.I18NextOptions? options) => 56 | (super.noSuchMethod( 57 | Invocation.method(#retrieve, [locale, namespace, key, options])) 58 | as String?); 59 | } 60 | 61 | /// A class which mocks [LocalizationDataSource]. 62 | /// 63 | /// See the documentation for Mockito's code generation for more information. 64 | class MockLocalizationDataSource extends _i1.Mock 65 | implements _i6.LocalizationDataSource { 66 | MockLocalizationDataSource() { 67 | _i1.throwOnMissingStub(this); 68 | } 69 | 70 | @override 71 | _i4.Future> load(_i3.Locale? locale) => 72 | (super.noSuchMethod(Invocation.method(#load, [locale]), 73 | returnValue: Future.value({})) 74 | as _i4.Future>); 75 | } 76 | -------------------------------------------------------------------------------- /example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "size" : "20x20", 5 | "idiom" : "iphone", 6 | "filename" : "Icon-App-20x20@2x.png", 7 | "scale" : "2x" 8 | }, 9 | { 10 | "size" : "20x20", 11 | "idiom" : "iphone", 12 | "filename" : "Icon-App-20x20@3x.png", 13 | "scale" : "3x" 14 | }, 15 | { 16 | "size" : "29x29", 17 | "idiom" : "iphone", 18 | "filename" : "Icon-App-29x29@1x.png", 19 | "scale" : "1x" 20 | }, 21 | { 22 | "size" : "29x29", 23 | "idiom" : "iphone", 24 | "filename" : "Icon-App-29x29@2x.png", 25 | "scale" : "2x" 26 | }, 27 | { 28 | "size" : "29x29", 29 | "idiom" : "iphone", 30 | "filename" : "Icon-App-29x29@3x.png", 31 | "scale" : "3x" 32 | }, 33 | { 34 | "size" : "40x40", 35 | "idiom" : "iphone", 36 | "filename" : "Icon-App-40x40@2x.png", 37 | "scale" : "2x" 38 | }, 39 | { 40 | "size" : "40x40", 41 | "idiom" : "iphone", 42 | "filename" : "Icon-App-40x40@3x.png", 43 | "scale" : "3x" 44 | }, 45 | { 46 | "size" : "60x60", 47 | "idiom" : "iphone", 48 | "filename" : "Icon-App-60x60@2x.png", 49 | "scale" : "2x" 50 | }, 51 | { 52 | "size" : "60x60", 53 | "idiom" : "iphone", 54 | "filename" : "Icon-App-60x60@3x.png", 55 | "scale" : "3x" 56 | }, 57 | { 58 | "size" : "20x20", 59 | "idiom" : "ipad", 60 | "filename" : "Icon-App-20x20@1x.png", 61 | "scale" : "1x" 62 | }, 63 | { 64 | "size" : "20x20", 65 | "idiom" : "ipad", 66 | "filename" : "Icon-App-20x20@2x.png", 67 | "scale" : "2x" 68 | }, 69 | { 70 | "size" : "29x29", 71 | "idiom" : "ipad", 72 | "filename" : "Icon-App-29x29@1x.png", 73 | "scale" : "1x" 74 | }, 75 | { 76 | "size" : "29x29", 77 | "idiom" : "ipad", 78 | "filename" : "Icon-App-29x29@2x.png", 79 | "scale" : "2x" 80 | }, 81 | { 82 | "size" : "40x40", 83 | "idiom" : "ipad", 84 | "filename" : "Icon-App-40x40@1x.png", 85 | "scale" : "1x" 86 | }, 87 | { 88 | "size" : "40x40", 89 | "idiom" : "ipad", 90 | "filename" : "Icon-App-40x40@2x.png", 91 | "scale" : "2x" 92 | }, 93 | { 94 | "size" : "76x76", 95 | "idiom" : "ipad", 96 | "filename" : "Icon-App-76x76@1x.png", 97 | "scale" : "1x" 98 | }, 99 | { 100 | "size" : "76x76", 101 | "idiom" : "ipad", 102 | "filename" : "Icon-App-76x76@2x.png", 103 | "scale" : "2x" 104 | }, 105 | { 106 | "size" : "83.5x83.5", 107 | "idiom" : "ipad", 108 | "filename" : "Icon-App-83.5x83.5@2x.png", 109 | "scale" : "2x" 110 | }, 111 | { 112 | "size" : "1024x1024", 113 | "idiom" : "ios-marketing", 114 | "filename" : "Icon-App-1024x1024@1x.png", 115 | "scale" : "1x" 116 | } 117 | ], 118 | "info" : { 119 | "version" : 1, 120 | "author" : "xcode" 121 | } 122 | } 123 | -------------------------------------------------------------------------------- /lib/src/i18next_localization_delegate.dart: -------------------------------------------------------------------------------- 1 | import 'package:flutter/widgets.dart'; 2 | import 'package:flutter/foundation.dart'; 3 | 4 | import 'data_sources/localization_data_source.dart'; 5 | import 'i18next.dart'; 6 | import 'options.dart'; 7 | import 'resource_store.dart'; 8 | 9 | /// A factory for the localized resource [I18Next] that is loaded by a 10 | /// [Localizations] widget. 11 | /// 12 | /// See [LocalizationsDelegate] for the base lifecycle calls. 13 | class I18NextLocalizationDelegate extends LocalizationsDelegate { 14 | I18NextLocalizationDelegate({ 15 | required this.locales, 16 | required this.dataSource, 17 | ResourceStore? resourceStore, 18 | this.options, 19 | }) : resourceStore = resourceStore ?? ResourceStore(), 20 | super(); 21 | 22 | /// The list of supported locales by this delegate. 23 | /// 24 | /// A supported locale example: 25 | /// If `en_US` is given, then both `en` and `en_US` are supported by this 26 | /// delegate, and it will load the normalized locale accordingly. 27 | /// Same if `pt` is given, it will support both `pt` and `pt_BR` locales. 28 | /// 29 | /// Supported | Given | Loaded 30 | /// :--|:--|:-- 31 | /// `en_US` | `en` | `en_US` 32 | /// `en_US` | `en_US` | `en_US` 33 | /// `pt` | `pt` | `pt` 34 | /// `pt` | `pt_BR` | `pt` 35 | /// 'jp' | 'pt' | errors 36 | final List locales; 37 | 38 | /// The data source that provides the localization data to this delegate. 39 | final LocalizationDataSource dataSource; 40 | 41 | /// Where the resources are kept and managed. 42 | final ResourceStore resourceStore; 43 | 44 | /// The options given to the [I18Next] instance. 45 | final I18NextOptions? options; 46 | 47 | @override 48 | bool isSupported(Locale locale) => 49 | locales.contains(locale) || 50 | locales.any((l) => l.languageCode == locale.languageCode); 51 | 52 | @override 53 | bool shouldReload(I18NextLocalizationDelegate old) { 54 | return !listEquals(locales, old.locales) || 55 | resourceStore != old.resourceStore || 56 | dataSource != old.dataSource || 57 | options != old.options; 58 | } 59 | 60 | /// Normalizes [locale] in case it is not fully supported, but a shorter 61 | /// or specific one might be. 62 | /// 63 | /// e.g. if this delegate supports ['en_US', 'pt']: 64 | /// 65 | /// - Both 'en_US' and 'en' => 'en_US' 66 | /// - Both 'pt_BR' and 'pt' => 'pt' 67 | Locale normalizeLocale(Locale locale) { 68 | if (!locales.contains(locale)) { 69 | locale = locales.firstWhere( 70 | (l) => l.languageCode == locale.languageCode, 71 | orElse: () => throw Exception('Unsupported locale $locale'), 72 | ); 73 | } 74 | return locale; 75 | } 76 | 77 | @override 78 | Future load(Locale locale) async { 79 | locale = normalizeLocale(locale); 80 | 81 | final namespaces = await dataSource.load(locale); 82 | // TODO: should delete previous locales/namespaces from resource store? 83 | for (final entry in namespaces.entries) { 84 | resourceStore.addNamespace(locale, entry.key, entry.value); 85 | } 86 | return I18Next(locale, resourceStore, options: options); 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /test/i18next_localization_delegate_test.mocks.dart: -------------------------------------------------------------------------------- 1 | // Mocks generated by Mockito 5.0.3 from annotations 2 | // in i18next/test/i18next_localization_delegate_test.dart. 3 | // Do not manually edit this file. 4 | 5 | import 'dart:async' as _i3; 6 | import 'dart:ui' as _i4; 7 | 8 | import 'package:i18next/src/data_sources/localization_data_source.dart' as _i2; 9 | import 'package:i18next/src/options.dart' as _i6; 10 | import 'package:i18next/src/resource_store.dart' as _i5; 11 | import 'package:mockito/mockito.dart' as _i1; 12 | 13 | // ignore_for_file: comment_references 14 | // ignore_for_file: unnecessary_parenthesis 15 | 16 | /// A class which mocks [LocalizationDataSource]. 17 | /// 18 | /// See the documentation for Mockito's code generation for more information. 19 | class MockLocalizationDataSource extends _i1.Mock 20 | implements _i2.LocalizationDataSource { 21 | MockLocalizationDataSource() { 22 | _i1.throwOnMissingStub(this); 23 | } 24 | 25 | @override 26 | _i3.Future> load(_i4.Locale? locale) => 27 | (super.noSuchMethod(Invocation.method(#load, [locale]), 28 | returnValue: Future.value({})) 29 | as _i3.Future>); 30 | } 31 | 32 | /// A class which mocks [ResourceStore]. 33 | /// 34 | /// See the documentation for Mockito's code generation for more information. 35 | class MockResourceStore extends _i1.Mock implements _i5.ResourceStore { 36 | MockResourceStore() { 37 | _i1.throwOnMissingStub(this); 38 | } 39 | 40 | @override 41 | void addNamespace( 42 | _i4.Locale? locale, String? namespace, Map? data) => 43 | super.noSuchMethod( 44 | Invocation.method(#addNamespace, [locale, namespace, data]), 45 | returnValueForMissingStub: null); 46 | @override 47 | void removeNamespace(_i4.Locale? locale, String? namespace) => super 48 | .noSuchMethod(Invocation.method(#removeNamespace, [locale, namespace]), 49 | returnValueForMissingStub: null); 50 | @override 51 | _i3.Future removeLocale(_i4.Locale? locale) => 52 | (super.noSuchMethod(Invocation.method(#removeLocale, [locale]), 53 | returnValue: Future.value(null), 54 | returnValueForMissingStub: Future.value()) as _i3.Future); 55 | @override 56 | _i3.Future removeAll() => 57 | (super.noSuchMethod(Invocation.method(#removeAll, []), 58 | returnValue: Future.value(null), 59 | returnValueForMissingStub: Future.value()) as _i3.Future); 60 | @override 61 | bool isNamespaceRegistered(_i4.Locale? locale, String? namespace) => 62 | (super.noSuchMethod( 63 | Invocation.method(#isNamespaceRegistered, [locale, namespace]), 64 | returnValue: false) as bool); 65 | @override 66 | bool isLocaleRegistered(_i4.Locale? locale) => 67 | (super.noSuchMethod(Invocation.method(#isLocaleRegistered, [locale]), 68 | returnValue: false) as bool); 69 | @override 70 | String? retrieve(_i4.Locale? locale, String? namespace, String? key, 71 | _i6.I18NextOptions? options) => 72 | (super.noSuchMethod( 73 | Invocation.method(#retrieve, [locale, namespace, key, options])) 74 | as String?); 75 | } 76 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | ## [0.5.2] 2 | 3 | * Fix: Unnecessary reloads of the localizationDataSource 4 | 5 | ## [0.5.1] 6 | 7 | * Fix: Asset path (rely on Flutter asset specifications) 8 | 9 | ## [0.5.0] 10 | 11 | * Adds support for multiple fallback namespaces 12 | 13 | ## [0.4.1] 14 | 15 | * Officializes the null-safety migration 16 | 17 | ## [0.4.0-nullsafety.0] 18 | 19 | * Migrates the codebase to flutter stable 2.0.3 + null-safety 20 | Renames `I18NextOptions.apply -> merge` 21 | 22 | ## [0.3.1] 23 | 24 | * Renames `utils.dart -> definitions.dart` 25 | * Adds and moves `evaluate` to `lib/utils.dart` as a part of the package, but without explicitly exporting it. 26 | * Allows interpolations to access grouped variables like so: 27 | `'An example with {{grouped.key}}' + {'grouped': {'key': 'grouped keys'}} = 'An example with grouped keys'` 28 | * Moves `lib/src/interpolator.dart` to `lib/interpolator.dart` 29 | To allow the interpolator usage as a separate package import 30 | 31 | ## [0.3.0] 32 | 33 | * Bumps to flutter stable 1.20 34 | 35 | ## [0.2.0] 36 | 37 | * Updates README bitrise badge 38 | * Adds pluralization to non-english locales (Fixes #6) @lynn 39 | 40 | ## [0.1.0] 41 | 42 | * Bumps to match flutter version 1.17 43 | 44 | ## [0.0.1+8] 45 | 46 | * Bumps analysis options #9 47 | * Adds fallback namespace #10 48 | * Refactors Translator to a callable class #10 49 | * Refactors interpolator class to global pure functions #10 50 | 51 | ## [0.0.1+7] 52 | 53 | * Change the namespaces type from `Map> -> Map` 54 | * Adds I18Next.of(BuildContext) from Localizations 55 | * Adds `I18NextLocalizationDelegate` 56 | * Adds convenience methods to `ResourceStore` for adding, removing, and verifiying locales and namespaces 57 | * Adds asset bundle data source and the LocalizationDataSource interface 58 | * Changes links to nubank/i18next 59 | * Adds example app 60 | 61 | ## [0.0.1+6] 62 | 63 | * Migrated repository to `williamhjcho/i18next` 64 | * Reduce description size 65 | 66 | ## [0.0.1+5] 67 | 68 | * Adds plural separator in I18NextOptions 69 | * Adds key separator in I18NextOptions 70 | * Adds and replaces LocalizationDataSource for ResourceStore 71 | * Makes `I18Next.t`'s parameters supersede the options parameter 72 | * Removes `Map` extension from `I18NextOptions` 73 | * Makes `I18NextOptions` `Diagnosticable` 74 | * Improves and adds more cases on `Interpolator` 75 | 76 | ## [0.0.1+4] 77 | 78 | * Renames arguments to variables 79 | * Replaces InterpolationOptions for I18NextOptions 80 | * Updates I18Next inner workings to more contextualized methods. 81 | * Escapes interpolation strings in options for RegExp 82 | * Adds base nesting mechanism 83 | * Isolates Translator, PluralResolver, and Interpolator into separate classes 84 | * Makes I18NextOptions's properties optional and allows individual overrides 85 | * Makes I18NextOption conform to Map 86 | * Reduces API surface by merging most of the optional properties into I18NextOptions itself 87 | * Moves pattern builders from options to the classes themselves 88 | * Keeps property variables in I18NextOptions while keeping Map extension. 89 | * Adds/merges locale property in I18NextOptions 90 | 91 | ## [0.0.1+3] 92 | 93 | * Adds InterpolationOption 94 | * Allows locale and interpolation options override on `t` 95 | * Adds a little more documentation 96 | 97 | Internal: 98 | * Splits data fetching and translation into separate methods 99 | 100 | ## [0.0.1] - TODO: Add release date. 101 | 102 | * TODO: Describe initial release. 103 | -------------------------------------------------------------------------------- /example/ios/Runner.xcodeproj/xcshareddata/xcschemes/Runner.xcscheme: -------------------------------------------------------------------------------- 1 | 2 | 5 | 8 | 9 | 15 | 21 | 22 | 23 | 24 | 25 | 30 | 31 | 32 | 33 | 39 | 40 | 41 | 42 | 43 | 44 | 54 | 56 | 62 | 63 | 64 | 65 | 66 | 67 | 73 | 75 | 81 | 82 | 83 | 84 | 86 | 87 | 90 | 91 | 92 | -------------------------------------------------------------------------------- /lib/src/data_sources/asset_bundle_data_source.dart: -------------------------------------------------------------------------------- 1 | import 'dart:collection'; 2 | import 'dart:convert'; 3 | import 'dart:ui'; 4 | 5 | import 'package:flutter/services.dart'; 6 | import 'package:path/path.dart' as path; 7 | 8 | import 'localization_data_source.dart'; 9 | 10 | /// A [LocalizationDataSource] that retrieves assets from an [AssetBundle]. 11 | class AssetBundleLocalizationDataSource implements LocalizationDataSource { 12 | AssetBundleLocalizationDataSource({ 13 | this.bundlePath = 'localizations', 14 | AssetBundle? bundle, 15 | }) : bundle = bundle ?? rootBundle, 16 | super(); 17 | 18 | /// The path prefixed to the asset when retrieving from the [bundle]. 19 | /// 20 | /// Defaults to 'localizations'. 21 | final String bundlePath; 22 | 23 | /// The [AssetBundle] where it retrieves the assets from. 24 | /// 25 | /// Defaults no [rootBundle]. 26 | final AssetBundle bundle; 27 | 28 | /// Loads all '.json' localization files declared in [manifest] with 29 | /// [bundlePath] given a [locale]. The assets themselves must have been 30 | /// previously declared in `pubspec.yaml`. 31 | /// 32 | /// For example, if your project structure is as follows: 33 | /// 34 | /// ``` 35 | /// /app 36 | /// - l10n 37 | /// - en-US/localizations.json 38 | /// - pt-BR/localizations.json 39 | /// ``` 40 | /// 41 | /// Then the desired [bundlePath] should be `l10n`. 42 | /// 43 | /// - [manifest] determines from where the namespaced files will be loaded 44 | /// from. This file should contain a [Map] where the keys represent the 45 | /// asset's path. Defaults to 'AssetManifest.json'. 46 | /// 47 | /// The end result is a [Map] that contains all the namespaces which are 48 | /// the file names themselves (case sensitive). 49 | @override 50 | Future> load( 51 | Locale locale, { 52 | String manifest = 'AssetManifest.json', 53 | }) async { 54 | assert(manifest.isNotEmpty); 55 | 56 | final assetFiles = await bundle 57 | .loadString(manifest) 58 | .then>((string) => json.decode(string)) 59 | .then((map) => map.keys); 60 | 61 | /// On every platform you never should try to get the `path.separator`, 62 | /// because Flutter is fetching all assets in `/` style. 63 | /// `path.separator` should only be used to handle OS files. 64 | final bundleLocalePath = '$bundlePath/${locale.toLanguageTag()}'; 65 | 66 | final files = assetFiles 67 | // trailing slash is to guarantee the whole dir matches, otherwise 68 | // it might allow undesired files 69 | .where((key) => key.contains('$bundleLocalePath')) 70 | .where((key) => path.extension(key) == '.json'); 71 | 72 | return await loadFromFiles(files); 73 | } 74 | 75 | Future> loadFromFiles( 76 | Iterable files, 77 | ) async { 78 | // TODO: make it case insensitive? 79 | final namespaces = HashMap(); 80 | for (final file in files) { 81 | // TODO: make this a lazy eval and let loading be handed concurrently? 82 | final namespace = path.basenameWithoutExtension(file); 83 | final string = await bundle.loadString(file); 84 | namespaces[namespace] = jsonDecode(string); 85 | } 86 | return namespaces; 87 | } 88 | 89 | @override 90 | bool operator ==(Object other) => 91 | identical(this, other) || 92 | other is AssetBundleLocalizationDataSource && 93 | runtimeType == other.runtimeType && 94 | bundlePath == other.bundlePath && 95 | bundle == other.bundle; 96 | 97 | @override 98 | int get hashCode => hashValues( 99 | bundlePath, 100 | bundle, 101 | ); 102 | } 103 | -------------------------------------------------------------------------------- /lib/src/translator.dart: -------------------------------------------------------------------------------- 1 | import 'dart:ui'; 2 | 3 | import '../interpolator.dart' as interpolator; 4 | import 'options.dart'; 5 | import 'plural_resolver.dart'; 6 | import 'resource_store.dart'; 7 | 8 | class Translator { 9 | Translator( 10 | this.pluralResolver, 11 | this.resourceStore, [ 12 | this.contextNamespace, 13 | ]) : super(); 14 | 15 | final PluralResolver pluralResolver; 16 | final ResourceStore resourceStore; 17 | final String? contextNamespace; 18 | 19 | String? call( 20 | String key, 21 | Locale locale, 22 | Map variables, 23 | I18NextOptions options, 24 | ) { 25 | var namespace = contextNamespace; 26 | var keyPath = key; 27 | final nsSeparator = options.namespaceSeparator ?? ':'; 28 | final match = RegExp(nsSeparator).firstMatch(key); 29 | if (match != null) { 30 | namespace = key.substring(0, match.start); 31 | keyPath = key.substring(match.end); 32 | } 33 | return translateKey(locale, namespace ?? '', keyPath, variables, options); 34 | } 35 | 36 | /// Order of key resolution: 37 | /// 38 | /// Expects `variables['context']` to be a `String?` and 39 | /// `variables['count']` to be an `int?`. Otherwise throws cast error. 40 | /// 41 | /// - context + pluralization: 42 | /// ['key_ctx_plr', 'key_ctx', 'key_plr', 'key'] 43 | /// - context only: 44 | /// ['key_ctx', 'key'] 45 | /// - pluralization only: 46 | /// ['key_plr', 'key'] 47 | /// - Otherwise: 48 | /// ['key'] 49 | String? translateKey( 50 | Locale locale, 51 | String namespace, 52 | String key, 53 | Map variables, 54 | I18NextOptions options, 55 | ) { 56 | final context = variables['context'] as String?; 57 | final count = variables['count'] as int?; 58 | final needsContext = context != null && context.isNotEmpty; 59 | final needsPlural = count != null; 60 | 61 | var pluralSuffix = ''; 62 | if (needsPlural) { 63 | pluralSuffix = pluralResolver.pluralize(locale, count!, options); 64 | } 65 | 66 | var tempKey = key; 67 | final keys = [key]; 68 | if (needsContext && needsPlural) { 69 | keys.add(tempKey + pluralSuffix); 70 | } 71 | if (needsContext) { 72 | keys.add(tempKey += '${options.contextSeparator}$context'); 73 | } 74 | if (needsPlural) { 75 | keys.add(tempKey += pluralSuffix); 76 | } 77 | 78 | final namespaces = [ 79 | namespace, 80 | if (options.fallbackNamespaces != null) ...options.fallbackNamespaces!, 81 | ]; 82 | 83 | for (final currentNamespace in namespaces) { 84 | for (final currentKey in keys.reversed) { 85 | final found = find( 86 | locale, 87 | currentNamespace, 88 | currentKey, 89 | variables, 90 | options, 91 | ); 92 | if (found != null) return found; 93 | } 94 | } 95 | return null; 96 | } 97 | 98 | /// Attempts to find the value given a [namespace] and [key]. 99 | /// 100 | /// If one is not found directly, then tries to fallback (if necessary). May 101 | /// still return null if none is found. 102 | String? find( 103 | Locale locale, 104 | String namespace, 105 | String key, 106 | Map variables, 107 | I18NextOptions options, 108 | ) { 109 | final value = resourceStore.retrieve(locale, namespace, key, options); 110 | String? result; 111 | if (value != null) { 112 | result = interpolator.interpolate(locale, value, variables, options); 113 | result = interpolator.nest( 114 | locale, 115 | result, 116 | Translator(pluralResolver, resourceStore, namespace), 117 | variables, 118 | options); 119 | } 120 | return result; 121 | } 122 | } 123 | -------------------------------------------------------------------------------- /test/i18next_localization_delegate_test.dart: -------------------------------------------------------------------------------- 1 | import 'package:flutter/widgets.dart'; 2 | import 'package:flutter_test/flutter_test.dart'; 3 | import 'package:i18next/i18next.dart'; 4 | import 'package:mockito/annotations.dart'; 5 | import 'package:mockito/mockito.dart'; 6 | 7 | import 'i18next_localization_delegate_test.mocks.dart'; 8 | 9 | @GenerateMocks([LocalizationDataSource, ResourceStore]) 10 | void main() { 11 | const en = Locale('en'), enUS = Locale('en', 'US'); 12 | const pt = Locale('pt'), ptBR = Locale('pt', 'BR'); 13 | const ar = Locale('ar'); 14 | 15 | late MockLocalizationDataSource dataSource; 16 | late MockResourceStore resourceStore; 17 | late I18NextLocalizationDelegate localizationDelegate; 18 | 19 | setUp(() { 20 | dataSource = MockLocalizationDataSource(); 21 | resourceStore = MockResourceStore(); 22 | localizationDelegate = I18NextLocalizationDelegate( 23 | locales: [en, ptBR], 24 | dataSource: dataSource, 25 | resourceStore: resourceStore, 26 | ); 27 | }); 28 | 29 | group('#isSupported', () { 30 | test('given an exact matching locale', () { 31 | expect(localizationDelegate.isSupported(en), isTrue); 32 | expect(localizationDelegate.isSupported(ptBR), isTrue); 33 | }); 34 | 35 | test('given a language code matching locale', () { 36 | expect(localizationDelegate.isSupported(enUS), isTrue); 37 | expect(localizationDelegate.isSupported(pt), isTrue); 38 | }); 39 | 40 | test('given a non matching language code locale', () { 41 | expect(localizationDelegate.isSupported(ar), isFalse); 42 | }); 43 | }); 44 | 45 | group('#normalizeLocale', () { 46 | test('given an exact matching locale', () { 47 | expect(localizationDelegate.normalizeLocale(en), en); 48 | expect(localizationDelegate.normalizeLocale(ptBR), ptBR); 49 | }); 50 | 51 | test('given a language code matching locale', () { 52 | expect(localizationDelegate.normalizeLocale(enUS), en); 53 | expect(localizationDelegate.normalizeLocale(pt), ptBR); 54 | }); 55 | 56 | test('given a non matching language code locale', () { 57 | expect(() => localizationDelegate.normalizeLocale(ar), throwsException); 58 | }); 59 | }); 60 | 61 | group('#load', () { 62 | test('given an exact matching locale', () async { 63 | when(dataSource.load(any)).thenAnswer((_) async => {}); 64 | 65 | await expectLater(localizationDelegate.load(en), completes); 66 | verify(dataSource.load(en)).called(1); 67 | }); 68 | 69 | test('given a language code matching locale', () async { 70 | when(dataSource.load(any)).thenAnswer((_) async => {}); 71 | 72 | await expectLater(localizationDelegate.load(enUS), completes); 73 | verify(dataSource.load(en)).called(1); 74 | }); 75 | 76 | test('given a non matching language code locale', () async { 77 | when(dataSource.load(any)).thenAnswer((_) async => {}); 78 | 79 | await expectLater(() => localizationDelegate.load(ar), throwsException); 80 | verifyNever(dataSource.load(any)); 81 | }); 82 | 83 | test('when dataSource errors', () async { 84 | const error = 'Some error'; 85 | when(dataSource.load(any)).thenAnswer((_) async => throw error); 86 | 87 | await expectLater(localizationDelegate.load(en), throwsA(error)); 88 | }); 89 | 90 | test('when dataSource succeeds', () async { 91 | const data1 = {'key': 'ns1'}; 92 | const data2 = {'key': 'ns1'}; 93 | when(dataSource.load(any)).thenAnswer( 94 | (_) async => {'ns1': data1, 'ns2': data2}, 95 | ); 96 | 97 | final i18next = await localizationDelegate.load(en); 98 | expect(i18next.locale, en); 99 | verify(resourceStore.addNamespace(en, 'ns1', data1)).called(1); 100 | verify(resourceStore.addNamespace(en, 'ns2', data2)).called(1); 101 | }); 102 | }); 103 | } 104 | -------------------------------------------------------------------------------- /lib/src/i18next.dart: -------------------------------------------------------------------------------- 1 | import 'dart:ui'; 2 | 3 | import 'package:flutter/widgets.dart'; 4 | 5 | import 'options.dart'; 6 | import 'plural_resolver.dart'; 7 | import 'resource_store.dart'; 8 | import 'translator.dart'; 9 | 10 | /// It translates the i18next localized format in your localization objects 11 | /// (provided by [resourceStore]) to the final translation. 12 | /// 13 | /// Usually the most common usage: 14 | /// 15 | /// ```dart 16 | /// // { 17 | /// // "key": "My text", 18 | /// // "nested": { "key": "My nested text" } 19 | /// // } 20 | /// I18Next.localization('key') // -> 'My text' 21 | /// I18Next.localization('nested.key') // -> 'My nested text' 22 | /// ``` 23 | /// 24 | /// It also allows the usage of namespaces (as long as they are provided by 25 | /// [dataSource]: 26 | /// 27 | /// ```dart 28 | /// // common.json 29 | /// // { "continue": "Continue" } 30 | /// // feature.json 31 | /// // { "title": "My feature Title" } 32 | /// I18Next.t('common:continue') // -> 'Continue' 33 | /// I18Next.t('feature:title') // -> 'My feature title' 34 | /// ``` 35 | class I18Next { 36 | I18Next(this.locale, this.resourceStore, {I18NextOptions? options}) 37 | : pluralResolver = const PluralResolver(), 38 | options = I18NextOptions.base.merge(options); 39 | 40 | /// The current and default [Locale] for this instance. 41 | final Locale locale; 42 | 43 | /// The resources store that contains all the necessary values mapped by 44 | /// [Locale], namespace, and keys. 45 | final ResourceStore resourceStore; 46 | 47 | /// The pluralization resolver. 48 | final PluralResolver pluralResolver; 49 | 50 | /// The options used to find and format matching interpolations. 51 | final I18NextOptions options; 52 | 53 | /// Attempts to retrieve a translation at [key]. 54 | /// 55 | /// - If [context] is given, then attempts to search for the key 56 | /// at 'key_context', before defaulting to the [key] itself. It is useful 57 | /// for selections like gender. 58 | /// - If [count] is given, then based on [locale] attempts to find the 59 | /// appropriate pluralized key, before defaulting to the [key] itself. 60 | /// Most languages like `en` have only `one` and `other` pluralization 61 | /// forms but some like `ar` require a more complex system. 62 | /// - If [variables] are given, they are used as a lookup table when a match 63 | /// has been found (delimited by [I18NextOptions.interpolationPrefix] and 64 | /// [I18NextOptions.interpolationSuffix]). Before the result is added to 65 | /// the final message, it first goes through [I18NextOptions.formatter]. 66 | /// - If [locale] is given, it overrides the current locale value. 67 | /// - If [options] is given, it overrides any non-null property over current 68 | /// options. 69 | /// 70 | /// The named parameters in this method that are also declared by 71 | /// [I18NextOptions] supersede [options]'s values. 72 | /// 73 | /// Keys that allow both contextualization and pluralization must be declared 74 | /// in the order: `key_context_plural` 75 | String t( 76 | String key, { 77 | Locale? locale, 78 | String? context, 79 | int? count, 80 | Map? variables, 81 | I18NextOptions? options, 82 | }) { 83 | variables ??= {}; 84 | if (context != null) variables['context'] = context; 85 | if (count != null) variables['count'] = count; 86 | 87 | locale ??= this.locale; 88 | final newOptions = this.options.merge(options); 89 | 90 | // TODO: when translator fails, allow a fallback behavior (null or throw) 91 | return Translator(pluralResolver, resourceStore) 92 | .call(key, locale, variables, newOptions) ?? 93 | key; 94 | } 95 | 96 | /// Returns the localized [I18Next] in the widget tree that corresponds to 97 | /// the given [context] via [Localizations]. 98 | /// 99 | /// Returns null if not found. 100 | /// 101 | /// An instance is usually registered and created by the 102 | /// [I18NextLocalizationDelegate]. 103 | static I18Next? of(BuildContext context) => 104 | Localizations.of(context, I18Next); 105 | } 106 | -------------------------------------------------------------------------------- /lib/interpolator.dart: -------------------------------------------------------------------------------- 1 | import 'dart:convert'; 2 | import 'dart:ui'; 3 | 4 | import 'src/options.dart'; 5 | import 'utils.dart'; 6 | 7 | typedef Translate = String? Function( 8 | String, 9 | Locale, 10 | Map, 11 | I18NextOptions, 12 | ); 13 | 14 | /// Replaces occurrences of matches in [string] for the named values 15 | /// in [options] (if they exist), by first passing through the 16 | /// [I18NextOptions.formatter] before joining the resulting string. 17 | /// 18 | /// - 'Hello {{name}}' + {name: 'World'} -> 'Hello World'. 19 | /// This example illustrates a simple interpolation. 20 | /// - 'Now is {{date, dd/MM}}' + {date: DateTime.now()} -> 'Now is 23/09'. 21 | /// In this example, [I18NextOptions.formatter] must be able to 22 | /// properly format the date. 23 | /// - 'A string with {{grouped.key}}' + {'grouped': {'key': "grouped keys}} -> 24 | /// 'A string with grouped keys'. In this example the variables are in the 25 | /// grouped formation (denoted by the [I18NextOptions.keySeparator]). 26 | String interpolate( 27 | Locale locale, 28 | String string, 29 | Map variables, 30 | I18NextOptions options, 31 | ) { 32 | final pattern = interpolationPattern(options); 33 | final keySeparator = options.keySeparator ?? '.'; 34 | 35 | return string.splitMapJoin( 36 | pattern, 37 | onMatch: (match) { 38 | final regExpMatch = match as RegExpMatch; 39 | final variable = regExpMatch.namedGroup('variable'); 40 | 41 | String? result; 42 | if (variable != null) { 43 | final path = variable.split(keySeparator); 44 | final value = evaluate(path, variables); 45 | // TODO: throw error or fallback behavior on options here? 46 | if (value != null) { 47 | final formatter = options.formatter; 48 | if (formatter != null) { 49 | final format = regExpMatch.namedGroup('format'); 50 | result = formatter(value, format, locale); 51 | } 52 | } 53 | } 54 | return result ?? regExpMatch.group(0)!; 55 | }, 56 | ); 57 | } 58 | 59 | /// Replaces occurrences of nested key-values in [string] for other 60 | /// key-values. Essentially calls [I18Next.translate] with the nested value. 61 | /// 62 | /// E.g.: 63 | /// ```json 64 | /// { 65 | /// key1: "Hello $t(key2)!" 66 | /// key2: "World" 67 | /// } 68 | /// i18Next.t('key1') // "Hello World!" 69 | /// ``` 70 | String nest( 71 | Locale locale, 72 | String string, 73 | Translate translate, 74 | Map variables, 75 | I18NextOptions options, 76 | ) { 77 | final pattern = nestingPattern(options); 78 | return string.splitMapJoin(pattern, onMatch: (match) { 79 | final regExpMatch = match as RegExpMatch; 80 | final key = regExpMatch.namedGroup('key'); 81 | 82 | String? result; 83 | if (key != null && key.isNotEmpty) { 84 | final newVariables = Map.of(variables); 85 | final varsString = regExpMatch.namedGroup('variables'); 86 | if (varsString != null && varsString.isNotEmpty) { 87 | try { 88 | final Map decoded = jsonDecode(varsString); 89 | newVariables.addAll(decoded); 90 | } catch (error) { 91 | // TODO: throw/fallback nesting failure(s)? 92 | assert(true, error); 93 | } 94 | } 95 | 96 | result = translate(key, locale, newVariables, options); 97 | } 98 | return result ?? regExpMatch.group(0)!; 99 | }); 100 | } 101 | 102 | RegExp interpolationPattern(I18NextOptions options) { 103 | final prefix = RegExp.escape(options.interpolationPrefix ?? '{{'); 104 | final suffix = RegExp.escape(options.interpolationSuffix ?? '}}'); 105 | final separator = RegExp.escape(options.interpolationSeparator ?? ','); 106 | return RegExp( 107 | '$prefix' 108 | '(?.*?)' 109 | '($separator\\s*(?.*?)\\s*)?' 110 | '$suffix', 111 | ); 112 | } 113 | 114 | RegExp nestingPattern(I18NextOptions options) { 115 | final prefix = RegExp.escape(options.nestingPrefix ?? r'$t('); 116 | final suffix = RegExp.escape(options.nestingSuffix ?? ')'); 117 | final separator = RegExp.escape(options.nestingSeparator ?? ','); 118 | return RegExp( 119 | '$prefix' 120 | '(?.*?)' 121 | '($separator\\s*(?.*?)\\s*)?' 122 | '$suffix', 123 | ); 124 | } 125 | -------------------------------------------------------------------------------- /test/data_sources/asset_bundle_data_source_test.dart: -------------------------------------------------------------------------------- 1 | import 'dart:ui'; 2 | 3 | import 'package:flutter/services.dart'; 4 | import 'package:flutter_test/flutter_test.dart'; 5 | import 'package:i18next/i18next.dart'; 6 | import 'package:mockito/annotations.dart'; 7 | import 'package:mockito/mockito.dart'; 8 | 9 | import 'asset_bundle_data_source_test.mocks.dart'; 10 | 11 | @GenerateMocks([AssetBundle]) 12 | void main() { 13 | const bundlePath = 'bundle/path'; 14 | const defaultManifest = 'AssetManifest.json'; 15 | late MockAssetBundle bundle; 16 | late AssetBundleLocalizationDataSource dataSource; 17 | 18 | setUp(() { 19 | bundle = MockAssetBundle(); 20 | dataSource = AssetBundleLocalizationDataSource( 21 | bundlePath: bundlePath, 22 | bundle: bundle, 23 | ); 24 | }); 25 | 26 | group('#loadFromAssetBundle', () { 27 | setUp(() { 28 | when(bundle.loadString(defaultManifest)).thenAnswer((_) async => '''{ 29 | "another/asset/path": [""], 30 | "$bundlePath/en-US/file1.json": [""], 31 | "$bundlePath/en-US/file2.json": [""], 32 | "$bundlePath/pt/file1.json": [""], 33 | "$bundlePath/pt/file2.json": [""] 34 | }'''); 35 | }); 36 | 37 | test('given any locale', () async { 38 | await expectLater( 39 | dataSource.load(const Locale('any')), 40 | completes, 41 | ); 42 | verify(bundle.loadString(defaultManifest)).called(1); 43 | }); 44 | 45 | test('given an unregistered locale', () { 46 | expect( 47 | dataSource.load(const Locale('ar')), 48 | completion(isEmpty), 49 | ); 50 | }); 51 | 52 | test('given a supported full locale', () async { 53 | when(bundle.loadString(argThat(contains('$bundlePath/')))) 54 | .thenAnswer((_) async => '{}'); 55 | 56 | await expectLater( 57 | dataSource.load(const Locale('en', 'US')), 58 | completion(equals(>{ 59 | 'file1': {}, 60 | 'file2': {}, 61 | })), 62 | ); 63 | 64 | verify(bundle.loadString('$bundlePath/en-US/file1.json')).called(1); 65 | verify(bundle.loadString('$bundlePath/en-US/file2.json')).called(1); 66 | }); 67 | 68 | test('given an unsupported long locale', () async { 69 | await expectLater( 70 | dataSource.load(const Locale('pt-BR')), 71 | completion(isEmpty), 72 | ); 73 | 74 | verifyNever(bundle.loadString(argThat(contains('$bundlePath/pt/')))); 75 | verifyNever(bundle.loadString(argThat(contains('$bundlePath/pt-BR/')))); 76 | verifyNever(bundle.loadString(argThat(contains('$bundlePath/en-US/')))); 77 | }); 78 | 79 | test('given a supported short locale', () async { 80 | when(bundle.loadString(argThat(contains('$bundlePath/')))) 81 | .thenAnswer((_) async => '{}'); 82 | 83 | await expectLater( 84 | dataSource.load(const Locale('pt')), 85 | completion(equals(>{ 86 | 'file1': {}, 87 | 'file2': {}, 88 | })), 89 | ); 90 | 91 | verify(bundle.loadString('$bundlePath/pt/file1.json')).called(1); 92 | verify(bundle.loadString('$bundlePath/pt/file2.json')).called(1); 93 | verifyNever(bundle.loadString(argThat(contains('$bundlePath/en-US/')))); 94 | }); 95 | 96 | test('given an unsupported short locale', () async { 97 | await expectLater( 98 | dataSource.load(const Locale('ar')), 99 | completion(isEmpty), 100 | ); 101 | 102 | verifyNever(bundle.loadString(argThat(contains('$bundlePath/ar/')))); 103 | verifyNever(bundle.loadString(argThat(contains('$bundlePath/pt/')))); 104 | verifyNever(bundle.loadString(argThat(contains('$bundlePath/en-US/')))); 105 | }); 106 | 107 | test('when bundle errors', () async { 108 | const error = 'Some error'; 109 | when(bundle.loadString(any)).thenAnswer((_) async => throw error); 110 | 111 | expect( 112 | dataSource.load(const Locale('any')), 113 | throwsA(error), 114 | ); 115 | }); 116 | 117 | test('given manifest empty', () { 118 | expect( 119 | () => dataSource.load( 120 | const Locale('any'), 121 | manifest: '', 122 | ), 123 | throwsAssertionError, 124 | ); 125 | }); 126 | 127 | test('given manifest', () async { 128 | const manifest = 'SomeManifestFile.json'; 129 | when(bundle.loadString(any)).thenAnswer((_) async => '{}'); 130 | 131 | await expectLater( 132 | dataSource.load( 133 | const Locale('any'), 134 | manifest: manifest, 135 | ), 136 | completes, 137 | ); 138 | verify(bundle.loadString(manifest)).called(1); 139 | }); 140 | 141 | test('given incorrect source-path to any bundle asset', () async { 142 | await expectLater( 143 | dataSource.load(const Locale('any')), 144 | completes, 145 | ); 146 | 147 | verifyNever(bundle.loadString(argThat(contains('bundle\\path')))); 148 | }); 149 | }); 150 | } 151 | -------------------------------------------------------------------------------- /example/lib/main.dart: -------------------------------------------------------------------------------- 1 | import 'package:flutter/cupertino.dart'; 2 | import 'package:flutter/material.dart'; 3 | import 'package:flutter_localizations/flutter_localizations.dart'; 4 | import 'package:i18next/i18next.dart'; 5 | import 'package:intl/intl.dart'; 6 | 7 | import 'localizations.dart'; 8 | 9 | void main() => runApp(MyApp()); 10 | 11 | class MyApp extends StatefulWidget { 12 | final List locales = const [ 13 | Locale('en', 'US'), 14 | Locale('pt', 'BR'), 15 | // TODO: add multi plural language(s) 16 | ]; 17 | 18 | @override 19 | _MyAppState createState() => _MyAppState(); 20 | } 21 | 22 | class _MyAppState extends State { 23 | late Locale locale; 24 | 25 | @override 26 | void initState() { 27 | super.initState(); 28 | 29 | locale = widget.locales.first; 30 | } 31 | 32 | @override 33 | Widget build(BuildContext context) { 34 | return MaterialApp( 35 | title: 'I18nu Demo', 36 | theme: ThemeData( 37 | dividerTheme: const DividerThemeData( 38 | color: Colors.black45, 39 | space: 32.0, 40 | ), 41 | ), 42 | localizationsDelegates: [ 43 | ...GlobalMaterialLocalizations.delegates, 44 | I18NextLocalizationDelegate( 45 | locales: widget.locales, 46 | dataSource: AssetBundleLocalizationDataSource( 47 | // This is the path for the files declared in pubspec which should 48 | // contain all of your localizations 49 | bundlePath: 'localizations', 50 | ), 51 | // extra formatting options can be added here 52 | options: const I18NextOptions(formatter: formatter), 53 | ), 54 | ], 55 | home: MyHomePage( 56 | supportedLocales: widget.locales, 57 | onUpdateLocale: updateLocale, 58 | ), 59 | locale: locale, 60 | supportedLocales: widget.locales, 61 | ); 62 | } 63 | 64 | void updateLocale(Locale newLocale) { 65 | setState(() { 66 | locale = newLocale; 67 | }); 68 | } 69 | 70 | static String formatter(Object value, String? format, Locale? locale) { 71 | switch (format) { 72 | case 'uppercase': 73 | return value.toString().toUpperCase(); 74 | case 'lowercase': 75 | return value.toString().toLowerCase(); 76 | default: 77 | if (value is DateTime) { 78 | return DateFormat(format, locale?.toString()).format(value); 79 | } 80 | } 81 | return value.toString(); 82 | } 83 | } 84 | 85 | class MyHomePage extends StatefulWidget { 86 | const MyHomePage({ 87 | Key? key, 88 | required this.supportedLocales, 89 | required this.onUpdateLocale, 90 | }) : super(key: key); 91 | 92 | final List supportedLocales; 93 | final ValueChanged onUpdateLocale; 94 | 95 | @override 96 | _MyHomePageState createState() => _MyHomePageState(); 97 | } 98 | 99 | class _MyHomePageState extends State { 100 | int _counter = 0; 101 | String _gender = ''; 102 | 103 | @override 104 | Widget build(BuildContext context) { 105 | final theme = Theme.of(context); 106 | final homepageL10n = HomePageL10n.of(context); 107 | final counterL10n = CounterL10n.of(context); 108 | 109 | return Scaffold( 110 | appBar: AppBar(title: Text(homepageL10n.title)), 111 | body: SingleChildScrollView( 112 | padding: const EdgeInsets.symmetric(vertical: 16, horizontal: 24), 113 | child: Column( 114 | mainAxisAlignment: MainAxisAlignment.start, 115 | children: [ 116 | CupertinoSegmentedControl( 117 | children: { 118 | for (var e in widget.supportedLocales) e: Text(e.toString()) 119 | }, 120 | groupValue: Localizations.localeOf(context), 121 | onValueChanged: widget.onUpdateLocale, 122 | ), 123 | const Divider(), 124 | Text( 125 | homepageL10n.hello(name: 'Name', world: 'Flutter'), 126 | style: theme.textTheme.headline6, 127 | ), 128 | Text( 129 | homepageL10n.today(DateTime.now()), 130 | style: theme.textTheme.subtitle2, 131 | ), 132 | CupertinoSegmentedControl( 133 | padding: const EdgeInsets.symmetric(vertical: 8), 134 | children: const { 135 | 'male': Text('MALE'), 136 | 'female': Text('FEMALE'), 137 | '': Text('OTHER'), 138 | }, 139 | groupValue: _gender, 140 | onValueChanged: updateGender, 141 | ), 142 | Text(homepageL10n.gendered(_gender)), 143 | const Divider(), 144 | Text( 145 | counterL10n.clicked(_counter), 146 | style: theme.textTheme.headline4, 147 | ), 148 | TextButton( 149 | onPressed: resetCounter, 150 | child: Text(counterL10n.resetCounter), 151 | ), 152 | ], 153 | ), 154 | ), 155 | floatingActionButton: FloatingActionButton( 156 | onPressed: incrementCounter, 157 | tooltip: counterL10n.clickMe, 158 | child: const Icon(Icons.add), 159 | ), 160 | ); 161 | } 162 | 163 | void incrementCounter() => setState(() => _counter++); 164 | 165 | void resetCounter() => setState(() => _counter = 0); 166 | 167 | void updateGender(String gender) => setState(() => _gender = gender); 168 | } 169 | -------------------------------------------------------------------------------- /test/resource_store_test.dart: -------------------------------------------------------------------------------- 1 | import 'dart:ui'; 2 | 3 | import 'package:flutter_test/flutter_test.dart'; 4 | import 'package:i18next/i18next.dart'; 5 | 6 | void main() { 7 | const locale = Locale('any'); 8 | const data = {'a': '0', 'b': '1'}; 9 | const options = I18NextOptions.base; 10 | 11 | late ResourceStore store; 12 | 13 | setUp(() { 14 | store = ResourceStore(); 15 | }); 16 | 17 | group('#retrieve', () { 18 | const validNamespace = 'ns'; 19 | 20 | setUp(() { 21 | store = ResourceStore(data: { 22 | locale: { 23 | validNamespace: { 24 | 'key': 'This is a simple key', 25 | 'my': { 26 | 'key': 'This is a nested key', 27 | 'nested': { 28 | 'key': 'This is a more nested key', 29 | } 30 | } 31 | } 32 | } 33 | }); 34 | }); 35 | 36 | test('with unmatching locale', () { 37 | const anotherLocale = Locale('pt'); 38 | expect( 39 | store.retrieve(anotherLocale, validNamespace, 'key', options), 40 | isNull, 41 | ); 42 | }); 43 | 44 | group('with matching locale', () { 45 | test('with unmatching namespace', () { 46 | expect(store.retrieve(locale, '', 'key', options), isNull); 47 | }); 48 | 49 | group('with matching namespace', () { 50 | const namespace = validNamespace; 51 | 52 | test('given a non matching key', () { 53 | expect( 54 | store.retrieve(locale, namespace, 'another.key', options), 55 | isNull, 56 | ); 57 | }); 58 | 59 | test('given a partially matching key', () { 60 | expect(store.retrieve(locale, namespace, 'my', options), isNull); 61 | expect( 62 | store.retrieve(locale, namespace, 'my.nested', options), 63 | isNull, 64 | ); 65 | }); 66 | 67 | test('given a matching key', () { 68 | expect( 69 | store.retrieve(locale, namespace, 'key', options), 70 | 'This is a simple key', 71 | ); 72 | expect( 73 | store.retrieve(locale, namespace, 'my.key', options), 74 | 'This is a nested key', 75 | ); 76 | expect( 77 | store.retrieve(locale, namespace, 'my.nested.key', options), 78 | 'This is a more nested key', 79 | ); 80 | }); 81 | 82 | test('given an over matching key', () { 83 | expect( 84 | store.retrieve(locale, namespace, 'my.nested.key.value', options), 85 | isNull, 86 | ); 87 | }); 88 | }); 89 | }); 90 | 91 | test('given a keySeparator', () { 92 | final newOptions = options.copyWith( 93 | keySeparator: '+++', 94 | ); 95 | expect( 96 | store.retrieve( 97 | locale, 98 | validNamespace, 99 | 'my+++nested+++key', 100 | newOptions, 101 | ), 102 | 'This is a more nested key', 103 | ); 104 | 105 | expect( 106 | store.retrieve( 107 | locale, 108 | validNamespace, 109 | 'my.nested.key', 110 | newOptions, 111 | ), 112 | isNull, 113 | ); 114 | 115 | expect( 116 | store.retrieve( 117 | locale, 118 | validNamespace, 119 | 'my/nested/key', 120 | newOptions, 121 | ), 122 | isNull, 123 | ); 124 | }); 125 | }); 126 | 127 | group('given an unregistered locale', () { 128 | test('#isLocaleRegistered', () { 129 | expect(store.isLocaleRegistered(locale), isFalse); 130 | }); 131 | 132 | test('#addNamespace', () { 133 | const namespace = 'ns'; 134 | expect(store.isNamespaceRegistered(locale, namespace), isFalse); 135 | 136 | store.addNamespace(locale, namespace, data); 137 | expect(store.isNamespaceRegistered(locale, namespace), isTrue); 138 | }); 139 | 140 | test('#removeNamespace', () { 141 | expect(() => store.removeNamespace(locale, 'ns'), returnsNormally); 142 | }); 143 | 144 | test('#removeLocale', () { 145 | expect(() => store.removeLocale(locale), returnsNormally); 146 | expect(store.isLocaleRegistered(locale), isFalse); 147 | }); 148 | 149 | test('#removeAll', () { 150 | expect(() => store.removeAll(), returnsNormally); 151 | expect(store.isLocaleRegistered(locale), isFalse); 152 | }); 153 | }); 154 | 155 | group('given a registered locale and namespace', () { 156 | const registeredNamespace = 'ns1'; 157 | 158 | setUp(() { 159 | store.addNamespace(locale, registeredNamespace, data); 160 | }); 161 | 162 | test('#removeLocale', () { 163 | store.removeLocale(locale); 164 | expect(store.isLocaleRegistered(locale), isFalse); 165 | }); 166 | 167 | test('#removeAll', () { 168 | store.removeAll(); 169 | expect(store.isLocaleRegistered(locale), isFalse); 170 | }); 171 | 172 | test('#isNamespaceRegistered', () { 173 | expect(store.isNamespaceRegistered(locale, 'ns1'), isTrue); 174 | }); 175 | 176 | test('#isLocaleRegistered', () { 177 | expect(store.isLocaleRegistered(locale), isTrue); 178 | }); 179 | 180 | group('given an unregistered namespace', () { 181 | const newNamespace = 'ns2'; 182 | 183 | test('#addNamespace', () { 184 | expect(store.isNamespaceRegistered(locale, newNamespace), isFalse); 185 | 186 | store.addNamespace(locale, newNamespace, data); 187 | expect(store.isNamespaceRegistered(locale, newNamespace), isTrue); 188 | expect( 189 | store.isNamespaceRegistered(locale, registeredNamespace), 190 | isTrue, 191 | ); 192 | }); 193 | 194 | test('#removeNamespace', () { 195 | expect(store.isNamespaceRegistered(locale, newNamespace), isFalse); 196 | 197 | store.removeNamespace(locale, newNamespace); 198 | expect(store.isNamespaceRegistered(locale, newNamespace), isFalse); 199 | expect( 200 | store.isNamespaceRegistered(locale, registeredNamespace), 201 | isTrue, 202 | ); 203 | }); 204 | }); 205 | 206 | group('given a registered namespace', () { 207 | test('#addNamespace', () { 208 | const anotherData = {'a': '00', 'b': '11'}; 209 | store.addNamespace(locale, registeredNamespace, anotherData); 210 | expect( 211 | store.isNamespaceRegistered(locale, registeredNamespace), isTrue); 212 | expect( 213 | store.retrieve(locale, registeredNamespace, 'a', options), 214 | equals('00'), 215 | ); 216 | expect( 217 | store.retrieve(locale, registeredNamespace, 'b', options), 218 | equals('11'), 219 | ); 220 | }); 221 | 222 | test('#removeNamespace', () { 223 | expect( 224 | store.isNamespaceRegistered(locale, registeredNamespace), 225 | isTrue, 226 | ); 227 | 228 | store.removeNamespace(locale, registeredNamespace); 229 | expect( 230 | store.isNamespaceRegistered(locale, registeredNamespace), 231 | isFalse, 232 | ); 233 | }); 234 | }); 235 | }); 236 | } 237 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # i18next 2 | 3 | ![build](https://github.com/nubank/i18next/workflows/build/badge.svg) [![codecov](https://codecov.io/gh/nubank/i18next/branch/master/graph/badge.svg)](https://codecov.io/gh/nubank/i18next) 4 | 5 | This is an adaptation of i18next standard for Dart with support for Flutter localization techniques. This package is still a work in progress. 6 | Mind that this is still a pre-1.0.0 so breaking changes may occur frequently. 7 | 8 | - [x] Support for variables 9 | - [x] Support for namespaces 10 | - [x] Support for context 11 | - [x] Support for simple plural forms (one or plural) 12 | - [x] Support for multiple plural forms (one, few, many, plural, ...) 13 | - [x] Plural and context fallbacks 14 | - [ ] Locale and namespace fallbacks 15 | - [x] Get string or object tree 16 | - [x] Support for nesting 17 | - [ ] Sprintf support 18 | - [x] Flutter's `LocalizationsDelegate` support 19 | - [x] Asset bundle localizations data source (retrieves from `pubspec.yaml`). See the example for more details. 20 | - [ ] Resource caching :wip: 21 | - [ ] Retrieve resource files from server :wip: 22 | - [ ] Custom post processing 23 | 24 | ## Usage 25 | 26 | Simply declare the package in your `pubspec.yaml` 27 | 28 | ```yaml 29 | dependencies: 30 | i18next: ^0.0.1 31 | ``` 32 | 33 | To use it with flutter's `LocalizationsDelegate` you first create `I18NextLocalizationDelegate` and register it in your `WidgetsApp` (`MaterialApp` or `CupertinoApp`). 34 | 35 | ```dart 36 | I18NextLocalizationDelegate( 37 | locales: widget.locales, 38 | // this data source is from where the delegate will retrieve the localizations from (namespaces Map) 39 | dataSource: ..., 40 | // optional extra options can be added here 41 | options: I18NextOptions(...), 42 | ), 43 | ``` 44 | 45 | Then to access and use it, simply call 46 | 47 | ```dart 48 | Widget build(BuildContext context) { 49 | // It finds the i18next instance on the widgets tree via `Localizations.of` 50 | I18Next.of(context).t(...); 51 | ... 52 | } 53 | ``` 54 | 55 | But if you want to handle it yourself, then simply instantiate it: 56 | 57 | ```dart 58 | I18Next( 59 | locale, 60 | // This store is from where i18next will attempt to retrieve the localizations from. 61 | resourceStore: ..., 62 | // Optional extra options can be added here 63 | options: I18NextOptions(...) 64 | ); 65 | ``` 66 | 67 | ## Syntax 68 | 69 | For the simple and straightforward usages: 70 | 71 | ```json 72 | { 73 | "key": "Hello World!", 74 | "nested": { 75 | "key": "My nested key" 76 | } 77 | } 78 | ``` 79 | 80 | ```dart 81 | i18next.t('key'); // 'Hello World!' 82 | i18next.t('nested.key'); // 'My nested key' 83 | 84 | // unmapped keys usually return themselves (when graceful fallback fails) 85 | i18next.t('unspecifiedKey'); // 'unspecifiedKey' 86 | ``` 87 | 88 | - Basic [Interpolation](https://www.i18next.com/translation-function/interpolation): 89 | 90 | ```json 91 | { 92 | "key": "Hello {{name}}!", 93 | "grouped_key": "Hello {{grouped.name}}" 94 | } 95 | ``` 96 | 97 | ```dart 98 | i18next.t('key', arguments: {'name': 'World'}); // 'Hello World!' 99 | i18next.t('grouped_key', arguments: {'grouped': {'name': 'Grouped World'}}); // 'Hello Grouped World!' 100 | ``` 101 | 102 | - [Nesting](https://www.i18next.com/translation-function/nesting): 103 | 104 | ```json 105 | { 106 | "nesting1": "1 $t(nesting2)", 107 | "nesting2": "2 $t(nesting3)", 108 | "nesting3": "3" 109 | } 110 | ``` 111 | 112 | ```dart 113 | i18next.t('nesting1'); // "1 2 3" 114 | ``` 115 | 116 | - [Plurals](https://www.i18next.com/translation-function/plurals) 117 | 118 | ```json 119 | { 120 | "key": "item", 121 | "key_plural": "items", 122 | "keyWithCount": "{{count}} item", 123 | "keyWithCount_plural": "{{count}} items" 124 | } 125 | ``` 126 | 127 | ```dart 128 | i18next.t('key', count: 0); // 'items' 129 | i18next.t('key', count: 1); // 'item' 130 | i18next.t('key', count: 5); // 'items' 131 | i18next.t('keyWithCount', count: 0); // '0 items' 132 | i18next.t('keyWithCount', count: 1); // '1 item' 133 | i18next.t('keyWithCount', count: 5); // '5 items' 134 | ``` 135 | 136 | There are also ways of dealing with locales with multiple plural: `zero, one, few, many, others` ([key identifier](https://jsfiddle.net/sm9wgLze)) (**Unsupported**) 137 | 138 | - Contexts like gender, are marked via underscores 139 | 140 | ```json 141 | { 142 | "genderMessage": "They", 143 | "genderMessage_male": "Him", 144 | "genderMessage_female": "Her" 145 | } 146 | ``` 147 | 148 | ```dart 149 | i18next.t('genderMessage'); // 'They' 150 | i18next.t('genderMessage', context: 'male'); // 'Him' 151 | i18next.t('genderMessage', context: 'female'); // 'Her' 152 | ``` 153 | 154 | And can be used with plurals 155 | 156 | ```json 157 | { 158 | "friend": "A friend", 159 | "friend_plural": "{{count}} friends", 160 | "friend_male": "A boyfriend", 161 | "friend_female": "A girlfriend", 162 | "friend_male_plural": "{{count}} boyfriends", 163 | "friend_female_plural": "{{count}} girlfriends" 164 | } 165 | ``` 166 | 167 | ```dart 168 | i18next.t('friend'); // 'A friend' 169 | i18next.t('friend', count: 1); // 'A friend' 170 | i18next.t('friend', count: 100); // '100 friends' 171 | 172 | i18next.t('friend', context: 'male', count: 1); // 'A boyfriend' 173 | i18next.t('friend', context: 'female', count: 1); // 'A girlfriend' 174 | i18next.t('friend', context: 'male', count: 100); // '100 boyfriends' 175 | i18next.t('friend', context: 'female', count: 100); // '100 girlfriends' 176 | ``` 177 | 178 | - [Formatting](https://www.i18next.com/translation-function/formatting) 179 | 180 | ```json 181 | { 182 | "key1": "The current date is {{now, MM/DD/YYYY}}", 183 | "key2": "{{text, uppercase}} just uppercased" 184 | } 185 | ``` 186 | 187 | ```dart 188 | i18next.t('key1', arguments: { 'now': DateTime.now() }); // 'The current date is 01/01/2020' 189 | i18next.t('key2', arguments: { 'text': 'my text' }); // 'MY TEXT just uppercased' 190 | ``` 191 | 192 | There are other usages and possibilities as well, this is just an example of what is defined by this format. 193 | 194 | - [Namespaces](https://www.i18next.com/principles/namespaces): A namespace can be thought of as logical groupings of different sets of translations. In a given namespace you could have a set of languages, each with their own set of keys. They can also be understood as separate files. 195 | For example: 196 | 197 | - **common.json:** Things that are reused everywhere, eg. Button labels 'save', 'cancel' 198 | - **validation.json:** All validation texts 199 | - **glossary.json:** Words we want to be reused consistently inside texts 200 | 201 | ```json 202 | // common.json 203 | { 204 | "myKey": "This key is in common" 205 | } 206 | 207 | // feature.json 208 | { 209 | "myKey": "This key is in my feature" 210 | } 211 | ``` 212 | 213 | ```dart 214 | i18next.t('common:myKey'); // 'This key is in common' 215 | i18next.t('feature:myKey'); // 'This key is in my feature' 216 | ``` 217 | 218 | - Context/plural fallback mechanism: 219 | 220 | ```json 221 | { 222 | "friend": "A friend", 223 | "friend_female": "A girlfriend" 224 | } 225 | ``` 226 | 227 | ```dart 228 | i18next.t('friend'); // 'A friend' 229 | 230 | i18next.t('friend', count: 1); // 'A friend' 231 | // It fallbacks to `friend` since `friend_plural` is not present 232 | i18next.t('friend', count: 2); // 'A friend' 233 | 234 | i18next.t('friend', context: 'female'); // 'A girlfriend' 235 | // It fallbacks to `friend` since `friend_male` is not present 236 | i18next.t('friend', context: 'male'); // 'A friend' 237 | ``` 238 | 239 | There is a way to also set the default namespace or a order of namespaces so a key knows where to start looking for the translation. 240 | -------------------------------------------------------------------------------- /analysis_options.yaml: -------------------------------------------------------------------------------- 1 | # Specify analysis options. 2 | # 3 | # Until there are meta linter rules, each desired lint must be explicitly enabled. 4 | # See: https://github.com/dart-lang/linter/issues/288 5 | # 6 | # For a list of lints, see: http://dart-lang.github.io/linter/lints/ 7 | # See the configuration guide for more 8 | # https://github.com/dart-lang/sdk/tree/master/pkg/analyzer#configuring-the-analyzer 9 | # 10 | # There are other similar analysis options files in the flutter repos, 11 | # which should be kept in sync with this file: 12 | # 13 | # - analysis_options.yaml (this file) 14 | # - packages/flutter/lib/analysis_options_user.yaml 15 | # - https://github.com/flutter/plugins/blob/master/analysis_options.yaml 16 | # - https://github.com/flutter/engine/blob/master/analysis_options.yaml 17 | # 18 | # This file contains the analysis options used by Flutter tools, such as IntelliJ, 19 | # Android Studio, and the `flutter analyze` command. 20 | 21 | analyzer: 22 | strong-mode: 23 | # when false, the type inference never implicitly casts to a more specific 24 | # type 25 | # String myString = myObject // (Object type) implicit downcast 26 | implicit-casts: true 27 | # when false, the type inference never chooses dynamic type when it can't 28 | # determine a static type 29 | implicit-dynamic: true 30 | errors: 31 | # treat missing required parameters as a warning (not a hint) 32 | missing_required_param: warning 33 | # treat missing returns as a warning (not a hint) 34 | missing_return: warning 35 | # allow having TODOs in the code 36 | todo: ignore 37 | # allow having Deprecated members using from same package 38 | deprecated_member_use_from_same_package: ignore 39 | # Ignore analyzer hints for updating pubspecs when using Future or 40 | # Stream and not importing dart:async 41 | # Please see https://github.com/flutter/flutter/pull/24528 for details. 42 | sdk_version_async_exported_from_core: ignore 43 | 44 | linter: 45 | rules: 46 | - always_declare_return_types 47 | - always_require_non_null_named_parameters 48 | - annotate_overrides 49 | - avoid_bool_literals_in_conditional_expressions 50 | - avoid_classes_with_only_static_members 51 | - avoid_empty_else 52 | - avoid_field_initializers_in_const_classes 53 | - avoid_function_literals_in_foreach_calls 54 | - avoid_init_to_null 55 | - avoid_null_checks_in_equality_operators 56 | - avoid_positional_boolean_parameters 57 | - avoid_print 58 | - avoid_private_typedef_functions 59 | - avoid_relative_lib_imports 60 | - avoid_renaming_method_parameters 61 | - avoid_return_types_on_setters 62 | - avoid_returning_null_for_void 63 | - avoid_returning_this 64 | - avoid_setters_without_getters 65 | - avoid_shadowing_type_parameters 66 | - avoid_types_as_parameter_names 67 | - avoid_types_on_closure_parameters 68 | - avoid_unused_constructor_parameters 69 | - avoid_void_async 70 | - await_only_futures 71 | - camel_case_extensions 72 | - camel_case_types 73 | - cancel_subscriptions 74 | - close_sinks 75 | - constant_identifier_names 76 | - control_flow_in_finally 77 | - curly_braces_in_flow_control_structures 78 | - directives_ordering 79 | - empty_catches 80 | - empty_constructor_bodies 81 | - empty_statements 82 | - file_names 83 | - hash_and_equals 84 | - implementation_imports 85 | - iterable_contains_unrelated_type 86 | - join_return_with_assignment 87 | - library_names 88 | - library_prefixes 89 | - lines_longer_than_80_chars 90 | - list_remove_unrelated_type 91 | - literal_only_boolean_expressions 92 | - no_adjacent_strings_in_list 93 | - no_duplicate_case_values 94 | - non_constant_identifier_names 95 | - null_closures 96 | - omit_local_variable_types 97 | - overridden_fields 98 | - package_api_docs 99 | - package_names 100 | - package_prefixed_library_names 101 | - prefer_adjacent_string_concatenation 102 | - prefer_asserts_in_initializer_lists 103 | - prefer_collection_literals 104 | - prefer_conditional_assignment 105 | - prefer_const_constructors 106 | - prefer_const_constructors_in_immutables 107 | - prefer_const_declarations 108 | - prefer_const_literals_to_create_immutables 109 | - prefer_contains 110 | - prefer_equal_for_default_values 111 | - prefer_final_fields 112 | - prefer_final_in_for_each 113 | - prefer_final_locals 114 | - prefer_for_elements_to_map_fromIterable 115 | - prefer_function_declarations_over_variables 116 | - prefer_generic_function_type_aliases 117 | - prefer_if_null_operators 118 | - prefer_initializing_formals 119 | - prefer_inlined_adds 120 | - prefer_interpolation_to_compose_strings 121 | - prefer_is_empty 122 | - prefer_is_not_empty 123 | - prefer_is_not_operator 124 | - prefer_iterable_whereType 125 | - prefer_null_aware_operators 126 | - prefer_relative_imports 127 | - prefer_single_quotes 128 | - prefer_spread_collections 129 | - prefer_typing_uninitialized_variables 130 | - prefer_void_to_null 131 | - provide_deprecation_message 132 | - recursive_getters 133 | - slash_for_doc_comments 134 | - sort_child_properties_last 135 | - sort_constructors_first 136 | - sort_pub_dependencies 137 | - sort_unnamed_constructors_first 138 | - test_types_in_equals 139 | - throw_in_finally 140 | - type_annotate_public_apis 141 | - type_init_formals 142 | - unnecessary_brace_in_string_interps 143 | - unnecessary_const 144 | - unnecessary_getters_setters 145 | - unnecessary_lambdas 146 | - unnecessary_new 147 | - unnecessary_null_aware_assignments 148 | - unnecessary_null_in_if_null_operators 149 | - unnecessary_overrides 150 | - unnecessary_parenthesis 151 | - unnecessary_statements 152 | - unnecessary_this 153 | - unrelated_type_equality_checks 154 | - use_full_hex_values_for_flutter_colors 155 | - use_function_type_syntax_for_parameters 156 | - use_rethrow_when_possible 157 | - use_string_buffers 158 | - use_to_and_as_if_applicable 159 | - valid_regexps 160 | - void_checks 161 | 162 | ### Disabled rules 163 | # - prefer_mixin 164 | # - unawaited_futures 165 | # - unnecessary_await_in_return 166 | # - unnecessary_final 167 | # - unnecessary_raw_strings 168 | # - unnecessary_string_escapes 169 | # - unnecessary_string_interpolations 170 | # - unsafe_html 171 | # - use_key_in_widget_constructors 172 | # - use_raw_strings 173 | # - prefer_constructors_over_static_methods 174 | # - avoid_returning_null_for_future 175 | # - sort_pub_dependencies 176 | # - public_member_api_docs 177 | # - use_setters_to_change_properties 178 | # - avoid_as 179 | # - avoid_slow_async_io 180 | # - prefer_foreach 181 | # - always_put_control_body_on_new_line 182 | # - always_put_required_named_parameters_first 183 | # - always_specify_types 184 | # - avoid_annotating_with_dynamic 185 | # - avoid_catches_without_on_clauses 186 | # - avoid_catching_errors 187 | # - avoid_double_and_int_checks 188 | # - avoid_equals_and_hash_code_on_mutable_classes 189 | # - avoid_escaping_inner_quotes 190 | # - avoid_implementing_value_types 191 | # - avoid_js_rounded_ints 192 | # - avoid_redundant_argument_values 193 | # - avoid_returning_null 194 | # - avoid_single_cascade_in_expression_statements 195 | # - avoid_unnecessary_containers 196 | # - cascade_invocations 197 | # - avoid_web_libraries_in_flutter 198 | # - comment_references 199 | # - diagnostic_describe_all_properties 200 | # - flutter_style_todos 201 | # - invariant_booleans 202 | # - missing_whitespace_between_adjacent_strings 203 | # - no_logic_in_create_state 204 | # - no_runtimeType_toString 205 | # - one_member_abstracts 206 | # - only_throw_errors 207 | # - parameter_assignments 208 | # - prefer_asserts_with_message 209 | # - prefer_double_quotes 210 | # - prefer_expression_function_bodies 211 | # - prefer_if_elements_to_conditional_expressions 212 | # - prefer_int_literals 213 | -------------------------------------------------------------------------------- /test/options_test.dart: -------------------------------------------------------------------------------- 1 | import 'dart:ui'; 2 | 3 | import 'package:flutter_test/flutter_test.dart'; 4 | import 'package:i18next/src/options.dart'; 5 | 6 | void main() { 7 | const base = I18NextOptions.base; 8 | 9 | test('default values', () { 10 | const options = I18NextOptions(); 11 | expect(options.namespaceSeparator, isNull); 12 | expect(options.contextSeparator, isNull); 13 | expect(options.pluralSeparator, isNull); 14 | expect(options.keySeparator, isNull); 15 | expect(options.interpolationPrefix, isNull); 16 | expect(options.interpolationSuffix, isNull); 17 | expect(options.interpolationSeparator, isNull); 18 | expect(options.nestingPrefix, isNull); 19 | expect(options.nestingSuffix, isNull); 20 | expect(options.nestingSeparator, isNull); 21 | expect(options.pluralSuffix, isNull); 22 | expect(options.formatter, isNull); 23 | }); 24 | 25 | test('default base values', () { 26 | expect(base.namespaceSeparator, ':'); 27 | expect(base.contextSeparator, '_'); 28 | expect(base.pluralSeparator, '_'); 29 | expect(base.keySeparator, '.'); 30 | expect(base.interpolationPrefix, '{{'); 31 | expect(base.interpolationSuffix, '}}'); 32 | expect(base.interpolationSeparator, ','); 33 | expect(base.nestingPrefix, r'$t('); 34 | expect(base.nestingSuffix, ')'); 35 | expect(base.nestingSeparator, ','); 36 | expect(base.pluralSuffix, 'plural'); 37 | expect(base.formatter, I18NextOptions.defaultFormatter); 38 | }); 39 | 40 | test('.defaultFormatter', () { 41 | const format = 'format'; 42 | const locale = Locale('en'); 43 | const formatter = I18NextOptions.defaultFormatter; 44 | 45 | expect(formatter('My value', format, locale), 'My value'); 46 | expect(formatter(9876.1234, format, locale), '9876.1234'); 47 | 48 | const object = {'my': 'value'}; 49 | expect(formatter(object, format, locale), object.toString()); 50 | 51 | final date = DateTime.now(); 52 | expect(formatter(date, format, locale), date.toString()); 53 | }); 54 | 55 | group('#merge', () { 56 | const empty = I18NextOptions(); 57 | final another = I18NextOptions( 58 | fallbackNamespaces: ['Some fallbackNamespace'], 59 | namespaceSeparator: 'Some namespaceSeparator', 60 | contextSeparator: 'Some contextSeparator', 61 | pluralSeparator: 'Some pluralSeparator', 62 | keySeparator: 'Some keySeparator', 63 | interpolationPrefix: 'Some interpolationPrefix', 64 | interpolationSuffix: 'Some interpolationSuffix', 65 | interpolationSeparator: 'Some interpolationSeparator', 66 | nestingPrefix: 'Some nestingPrefix', 67 | nestingSuffix: 'Some nestingSuffix', 68 | nestingSeparator: 'Some nestingSeparator', 69 | pluralSuffix: 'Some pluralSuffix', 70 | formatter: (value, format, locale) => value.toString(), 71 | ); 72 | 73 | test('given no values', () { 74 | expect(base.merge(base), base); 75 | expect(base.copyWith(), base); 76 | expect(empty.merge(empty), empty); 77 | expect(another.copyWith(), another); 78 | expect(another.merge(another), another); 79 | }); 80 | 81 | test('from empty given full', () { 82 | expect(empty.merge(base), base); 83 | expect(empty.merge(another), another); 84 | }); 85 | 86 | test('from full given empty', () { 87 | expect(base.merge(empty), base); 88 | expect(another.merge(empty), another); 89 | }); 90 | 91 | test('from full given full', () { 92 | expect(base.merge(another), another); 93 | expect(another.merge(another), another); 94 | }); 95 | 96 | test('given null', () { 97 | expect(base.merge(null), equals(base)); 98 | expect(empty.merge(null), empty); 99 | expect(another.merge(null), another); 100 | 101 | expect(identical(base.merge(null), base), isTrue); 102 | }); 103 | }); 104 | 105 | group('#copyWith', () { 106 | final another = I18NextOptions( 107 | fallbackNamespaces: ['Some fallbackNamespace'], 108 | namespaceSeparator: 'Some namespaceSeparator', 109 | contextSeparator: 'Some contextSeparator', 110 | pluralSeparator: 'Some pluralSeparator', 111 | keySeparator: 'Some keySeparator', 112 | interpolationPrefix: 'Some interpolationPrefix', 113 | interpolationSuffix: 'Some interpolationSuffix', 114 | interpolationSeparator: 'Some interpolationSeparator', 115 | nestingPrefix: 'Some nestingPrefix', 116 | nestingSuffix: 'Some nestingSuffix', 117 | nestingSeparator: 'Some nestingSeparator', 118 | pluralSuffix: 'Some pluralSuffix', 119 | formatter: (value, format, locale) => value.toString(), 120 | ); 121 | 122 | test('equality', () { 123 | expect(base == base, isTrue); 124 | expect(another == another, isTrue); 125 | expect(another == base, isFalse); 126 | }); 127 | 128 | test('given no values', () { 129 | expect(base.copyWith(), base); 130 | expect(another.copyWith(), another); 131 | }); 132 | 133 | for (final permutation in _generatePermutations([ 134 | another.fallbackNamespaces!, 135 | another.namespaceSeparator!, 136 | another.contextSeparator!, 137 | another.pluralSeparator!, 138 | another.keySeparator!, 139 | another.interpolationPrefix!, 140 | another.interpolationSuffix!, 141 | another.interpolationSeparator!, 142 | another.nestingPrefix!, 143 | another.nestingSuffix!, 144 | another.nestingSeparator!, 145 | another.pluralSuffix!, 146 | ])) { 147 | test('given individual values=$permutation', () { 148 | final result = base.copyWith( 149 | fallbackNamespaces: permutation[0] as List?, 150 | namespaceSeparator: permutation[1] as String?, 151 | contextSeparator: permutation[2] as String?, 152 | pluralSeparator: permutation[3] as String?, 153 | keySeparator: permutation[4] as String?, 154 | interpolationPrefix: permutation[5] as String?, 155 | interpolationSuffix: permutation[6] as String?, 156 | interpolationSeparator: permutation[7] as String?, 157 | nestingPrefix: permutation[8] as String?, 158 | nestingSuffix: permutation[9] as String?, 159 | nestingSeparator: permutation[10] as String?, 160 | pluralSuffix: permutation[11] as String?, 161 | ); 162 | // at least one should be different 163 | expect(result, isNot(base)); 164 | expect( 165 | result.fallbackNamespaces, 166 | permutation[0] ?? base.fallbackNamespaces, 167 | ); 168 | expect( 169 | result.namespaceSeparator, 170 | permutation[1] ?? base.namespaceSeparator, 171 | ); 172 | expect( 173 | result.contextSeparator, 174 | permutation[2] ?? base.contextSeparator, 175 | ); 176 | expect( 177 | result.pluralSeparator, 178 | permutation[3] ?? base.pluralSeparator, 179 | ); 180 | expect( 181 | result.keySeparator, 182 | permutation[4] ?? base.keySeparator, 183 | ); 184 | expect( 185 | result.interpolationPrefix, 186 | permutation[5] ?? base.interpolationPrefix, 187 | ); 188 | expect( 189 | result.interpolationSuffix, 190 | permutation[6] ?? base.interpolationSuffix, 191 | ); 192 | expect( 193 | result.interpolationSeparator, 194 | permutation[7] ?? base.interpolationSeparator, 195 | ); 196 | expect( 197 | result.nestingPrefix, 198 | permutation[8] ?? base.nestingPrefix, 199 | ); 200 | expect( 201 | result.nestingSuffix, 202 | permutation[9] ?? base.nestingSuffix, 203 | ); 204 | expect( 205 | result.nestingSeparator, 206 | permutation[10] ?? base.nestingSeparator, 207 | ); 208 | expect( 209 | result.pluralSuffix, 210 | permutation[11] ?? base.pluralSuffix, 211 | ); 212 | }); 213 | } 214 | 215 | test('given all values', () { 216 | final result = base.copyWith( 217 | fallbackNamespaces: another.fallbackNamespaces, 218 | namespaceSeparator: another.namespaceSeparator, 219 | contextSeparator: another.contextSeparator, 220 | pluralSeparator: another.pluralSeparator, 221 | keySeparator: another.keySeparator, 222 | pluralSuffix: another.pluralSuffix, 223 | interpolationPrefix: another.interpolationPrefix, 224 | interpolationSuffix: another.interpolationSuffix, 225 | interpolationSeparator: another.interpolationSeparator, 226 | nestingPrefix: another.nestingPrefix, 227 | nestingSuffix: another.nestingSuffix, 228 | nestingSeparator: another.nestingSeparator, 229 | formatter: another.formatter, 230 | ); 231 | expect(result, another); 232 | }); 233 | }); 234 | } 235 | 236 | /// Generates a list of [input]s with just one non-null value 237 | List> _generatePermutations(List input) { 238 | final result = >[]; 239 | for (var index = 0; index < input.length; index += 1) { 240 | final alteredInput = List.filled(input.length, null); 241 | alteredInput[index] = input[index]; 242 | result.add(alteredInput); 243 | } 244 | return result; 245 | } 246 | -------------------------------------------------------------------------------- /lib/src/plural_resolver.dart: -------------------------------------------------------------------------------- 1 | import 'dart:ui'; 2 | 3 | import 'options.dart'; 4 | 5 | typedef PluralizationRule = int Function(int); 6 | 7 | class PluralResolver { 8 | const PluralResolver() : super(); 9 | 10 | /// Returns the plural suffix based on [count] and presented [options]. 11 | String pluralize(Locale locale, int count, I18NextOptions options) { 12 | final rule = _ruleForLocale(locale); 13 | final index = rule(count.abs()); 14 | final separator = options.pluralSeparator ?? '_'; 15 | 16 | if (_ruleUsesSimpleSuffixes(rule)) { 17 | final suffix = options.pluralSuffix ?? 'plural'; 18 | return index == 0 ? '' : '$separator$suffix'; 19 | } else { 20 | return '$separator$index'; 21 | } 22 | } 23 | 24 | /// Decide whether this rule simply uses "key" and "key_plural" rather than 25 | /// "key_0", "key_1", ... 26 | bool _ruleUsesSimpleSuffixes(PluralizationRule rule) { 27 | return rule == _rule1 || 28 | rule == _rule2 || 29 | rule == _rule3 || 30 | rule == _rule9 || 31 | rule == _rule12 || 32 | rule == _rule17; 33 | } 34 | 35 | PluralizationRule _ruleForLocale(Locale locale) { 36 | final language = locale.languageCode; 37 | switch (language) { 38 | // Portuguese pluralization is country-dependent: 39 | case 'pt': 40 | return locale.countryCode == 'BR' ? _rule1 : _rule2; 41 | 42 | // Rule 1: "n > 1" style plurals. 43 | case 'ach': 44 | case 'ak': 45 | case 'am': 46 | case 'arn': 47 | case 'br': 48 | case 'fil': 49 | case 'gun': 50 | case 'ln': 51 | case 'mfe': 52 | case 'mg': 53 | case 'mi': 54 | case 'oc': 55 | case 'tg': 56 | case 'ti': 57 | case 'tr': 58 | case 'uz': 59 | case 'wa': 60 | return _rule1; 61 | 62 | // Rule 2: "n != 1" style plurals. 63 | case 'af': 64 | case 'an': 65 | case 'ast': 66 | case 'az': 67 | case 'bg': 68 | case 'bn': 69 | case 'ca': 70 | case 'da': 71 | case 'de': 72 | case 'dev': 73 | case 'el': 74 | case 'en': 75 | case 'eo': 76 | case 'es': 77 | case 'et': 78 | case 'eu': 79 | case 'fi': 80 | case 'fo': 81 | case 'fur': 82 | case 'fy': 83 | case 'gl': 84 | case 'gu': 85 | case 'ha': 86 | case 'hi': 87 | case 'hu': 88 | case 'hy': 89 | case 'ia': 90 | case 'it': 91 | case 'kn': 92 | case 'ku': 93 | case 'lb': 94 | case 'mai': 95 | case 'ml': 96 | case 'mn': 97 | case 'mr': 98 | case 'nah': 99 | case 'nap': 100 | case 'nb': 101 | case 'ne': 102 | case 'nl': 103 | case 'nn': 104 | case 'no': 105 | case 'nso': 106 | case 'or': 107 | case 'pa': 108 | case 'pap': 109 | case 'pms': 110 | case 'ps': 111 | case 'rm': 112 | case 'sco': 113 | case 'se': 114 | case 'si': 115 | case 'so': 116 | case 'son': 117 | case 'sq': 118 | case 'sv': 119 | case 'sw': 120 | case 'ta': 121 | case 'te': 122 | case 'tk': 123 | case 'ur': 124 | case 'yo': 125 | return _rule2; 126 | 127 | // Rule 3: no pluralization. 128 | case 'ay': 129 | case 'bo': 130 | case 'cgg': 131 | case 'fa': 132 | case 'id': 133 | case 'ja': 134 | case 'jbo': 135 | case 'ka': 136 | case 'kk': 137 | case 'km': 138 | case 'ko': 139 | case 'ky': 140 | case 'lo': 141 | case 'ms': 142 | case 'sah': 143 | case 'su': 144 | case 'th': 145 | case 'tt': 146 | case 'ug': 147 | case 'vi': 148 | case 'wo': 149 | case 'zh': 150 | return _rule3; 151 | 152 | // Rule 4: Russian-style plurals. 153 | case 'be': 154 | case 'bs': 155 | case 'cnr': 156 | case 'dz': 157 | case 'hr': 158 | case 'ru': 159 | case 'sr': 160 | case 'uk': 161 | return _rule4; 162 | 163 | // Rule 5: Arabic. 164 | case 'ar': 165 | return _rule5; 166 | 167 | // Rule 6: Czech and Slovak. 168 | case 'cs': 169 | case 'sk': 170 | return _rule6; 171 | 172 | // Rule 7: Cashubian and Polish. 173 | case 'csb': 174 | case 'pl': 175 | return _rule7; 176 | 177 | // Rule 8: Welsh. 178 | case 'cy': 179 | return _rule8; 180 | 181 | // Rule 9: French. 182 | case 'fr': 183 | return _rule9; 184 | 185 | // Rule 10: Irish. 186 | case 'ga': 187 | return _rule10; 188 | 189 | // Rule 11: Scottish Gaelic. 190 | case 'gd': 191 | return _rule11; 192 | 193 | // Rule 12: Icelandic. 194 | case 'is': 195 | return _rule12; 196 | 197 | // Rule 13: Javanese. 198 | case 'jv': 199 | return _rule13; 200 | 201 | // Rule 14: Cornish. 202 | case 'kw': 203 | return _rule14; 204 | 205 | // Rule 15: Lithuanian. 206 | case 'lt': 207 | return _rule15; 208 | 209 | // Rule 16: Latvian. 210 | case 'lv': 211 | return _rule16; 212 | 213 | // Rule 17: Macedonian. 214 | case 'mk': 215 | return _rule17; 216 | 217 | // Rule 18: Mandinka. 218 | case 'mnk': 219 | return _rule18; 220 | 221 | // Rule 19: Maltese. 222 | case 'mt': 223 | return _rule19; 224 | 225 | // Rule 20: Romanian. 226 | case 'ro': 227 | return _rule20; 228 | 229 | // Rule 21: Slovene. 230 | case 'sl': 231 | return _rule21; 232 | 233 | // Rule 22: Hebrew. 234 | case 'he': 235 | return _rule22; 236 | 237 | // Use the "no plurals" rule as a default. 238 | default: 239 | return _rule3; 240 | } 241 | } 242 | 243 | // Pluralization rules from: https://github.com/i18next/i18next/blob/4bfa7a3ace9d5eb6f7dee2fe4640b918242ba441/src/PluralResolver.js#L41 244 | static int _rule1(int n) => n > 1 ? 1 : 0; 245 | 246 | static int _rule2(int n) => n != 1 ? 1 : 0; 247 | 248 | static int _rule3(int n) => 0; 249 | 250 | static int _rule4(int n) => n % 10 == 1 && n % 100 != 11 251 | ? 0 252 | : n % 10 >= 2 && n % 10 <= 4 && (n % 100 < 10 || n % 100 >= 20) 253 | ? 1 254 | : 2; 255 | 256 | static int _rule5(int n) => n == 0 257 | ? 0 258 | : n == 1 259 | ? 1 260 | : n == 2 261 | ? 2 262 | : n % 100 >= 3 && n % 100 <= 10 263 | ? 3 264 | : n % 100 >= 11 265 | ? 4 266 | : 5; 267 | 268 | static int _rule6(int n) => n == 1 269 | ? 0 270 | : n >= 2 && n <= 4 271 | ? 1 272 | : 2; 273 | 274 | static int _rule7(int n) => n == 1 275 | ? 0 276 | : n % 10 >= 2 && n % 10 <= 4 && (n % 100 < 10 || n % 100 >= 20) 277 | ? 1 278 | : 2; 279 | 280 | static int _rule8(int n) => (n == 1) 281 | ? 0 282 | : (n == 2) 283 | ? 1 284 | : (n != 8 && n != 11) 285 | ? 2 286 | : 3; 287 | 288 | static int _rule9(int n) => n >= 2 ? 1 : 0; 289 | 290 | static int _rule10(int n) => n == 1 291 | ? 0 292 | : n == 2 293 | ? 1 294 | : n < 7 295 | ? 2 296 | : n < 11 297 | ? 3 298 | : 4; 299 | 300 | static int _rule11(int n) => n == 1 || n == 11 301 | ? 0 302 | : n == 2 || n == 12 303 | ? 1 304 | : n > 2 && n < 20 305 | ? 2 306 | : 3; 307 | 308 | static int _rule12(int n) => n % 10 != 1 || n % 100 == 11 ? 1 : 0; 309 | 310 | static int _rule13(int n) => n != 0 ? 1 : 0; 311 | 312 | static int _rule14(int n) => n == 1 313 | ? 0 314 | : (n == 2) 315 | ? 1 316 | : (n == 3) 317 | ? 2 318 | : 3; 319 | 320 | static int _rule15(int n) => n % 10 == 1 && n % 100 != 11 321 | ? 0 322 | : n % 10 >= 2 && (n % 100 < 10 || n % 100 >= 20) 323 | ? 1 324 | : 2; 325 | 326 | static int _rule16(int n) => n % 10 == 1 && n % 100 != 11 327 | ? 0 328 | : n != 0 329 | ? 1 330 | : 2; 331 | 332 | static int _rule17(int n) => n == 1 || n % 10 == 1 && n % 100 != 11 ? 0 : 1; 333 | 334 | static int _rule18(int n) => n == 0 335 | ? 0 336 | : n == 1 337 | ? 1 338 | : 2; 339 | 340 | static int _rule19(int n) => n == 1 341 | ? 0 342 | : n == 0 || (n % 100 > 1 && n % 100 < 11) 343 | ? 1 344 | : n % 100 > 10 && n % 100 < 20 345 | ? 2 346 | : 3; 347 | 348 | static int _rule20(int n) => n == 1 349 | ? 0 350 | : n == 0 || n % 100 > 0 && n % 100 < 20 351 | ? 1 352 | : 2; 353 | 354 | static int _rule21(int n) => n % 100 == 1 355 | ? 1 356 | : n % 100 == 2 357 | ? 2 358 | : n % 100 == 3 || n % 100 == 4 359 | ? 3 360 | : 0; 361 | 362 | static int _rule22(int n) => n == 1 363 | ? 0 364 | : n == 2 365 | ? 1 366 | : (n < 0 || n > 10) && n % 10 == 0 367 | ? 2 368 | : 3; 369 | } 370 | -------------------------------------------------------------------------------- /lib/src/options.dart: -------------------------------------------------------------------------------- 1 | import 'dart:ui'; 2 | 3 | import 'package:flutter/foundation.dart'; 4 | 5 | typedef ArgumentFormatter = String Function( 6 | Object value, 7 | String? format, 8 | Locale locale, 9 | ); 10 | 11 | /// Contains all options for [I18Next] to work properly. 12 | class I18NextOptions with Diagnosticable { 13 | const I18NextOptions({ 14 | this.fallbackNamespaces, 15 | this.namespaceSeparator, 16 | this.contextSeparator, 17 | this.pluralSeparator, 18 | this.keySeparator, 19 | this.interpolationPrefix, 20 | this.interpolationSuffix, 21 | this.interpolationSeparator, 22 | this.nestingPrefix, 23 | this.nestingSuffix, 24 | this.nestingSeparator, 25 | this.pluralSuffix, 26 | this.formatter, 27 | }) : super(); 28 | 29 | static const I18NextOptions base = I18NextOptions( 30 | fallbackNamespaces: null, 31 | namespaceSeparator: ':', 32 | contextSeparator: '_', 33 | pluralSeparator: '_', 34 | keySeparator: '.', 35 | interpolationPrefix: '{{', 36 | interpolationSuffix: '}}', 37 | interpolationSeparator: ',', 38 | nestingPrefix: r'$t(', 39 | nestingSuffix: ')', 40 | nestingSeparator: ',', 41 | pluralSuffix: 'plural', 42 | formatter: defaultFormatter, 43 | ); 44 | 45 | /// The namespaces used to fallback to when no key matches were found on the 46 | /// current namespace. 47 | /// These namespaces are evaluated in the order they are put in the list. 48 | /// 49 | /// Defaults to null. 50 | final List? fallbackNamespaces; 51 | 52 | /// The separator used when splitting the key. 53 | /// 54 | /// Defaults to ':'. 55 | final String? namespaceSeparator; 56 | 57 | /// The separator for contexts, it is inserted between the key and the 58 | /// context value. 59 | /// 60 | /// Defaults to '_'. 61 | final String? contextSeparator; 62 | 63 | /// The separator for plural suffixes, it is inserted between the key and the 64 | /// plural value ("plural" for simple rules, or a numeric index for complex 65 | /// rules with multiple plurals). 66 | /// 67 | /// Defaults to '_'. 68 | final String? pluralSeparator; 69 | 70 | /// The separator for nested keys. It is used to denote multiple object 71 | /// levels of access when retrieving a key from a namespace. 72 | /// 73 | /// Defaults to '.'. 74 | final String? keySeparator; 75 | 76 | /// [pluralSuffix] is used for the pluralization mechanism. 77 | /// 78 | /// Defaults to 'plural' and is used for simple pluralization rules. 79 | /// 80 | /// For example, in english where it only has singular or plural forms: 81 | /// 82 | /// ``` 83 | /// "friend": "A friend" 84 | /// "friend_plural": "{{count}} friends" 85 | /// ``` 86 | final String? pluralSuffix; 87 | 88 | /// [interpolationPrefix] and [interpolationSuffix] are the deliminators 89 | /// for the variable interpolation and formatting mechanism. 90 | /// By default they are '{{' and '}}' respectively and can't be null but 91 | /// can be empty. 92 | /// 93 | /// [interpolationSeparator] is used to separate the variable's 94 | /// name from the format (if any). Defaults to ',' and cannot be null nor 95 | /// empty (otherwise it'll match every char in the interpolation). 96 | /// 97 | /// ``` 98 | /// - '{{title}}' name = 'title, format = null 99 | /// - '{{title, uppercase}}' name = 'title', format = 'uppercase' 100 | /// ``` 101 | final String? interpolationPrefix, 102 | interpolationSuffix, 103 | interpolationSeparator; 104 | 105 | /// [nestingPrefix] and [nestingSuffix] are the deliminators for nesting 106 | /// mechanism. By default they are '$t(' and ')' respectively and can't be 107 | /// null but can be empty. 108 | /// 109 | /// [nestingSeparator] is used to separate the key's name from the variables 110 | /// (if any) which must be JSON. Defaults to ',' and cannot be null nor empty 111 | /// (otherwise it'll match every char in the nesting). 112 | /// 113 | /// ```json 114 | /// { 115 | /// key1: "Hello $t(key2)!" 116 | /// key2: "World" 117 | /// } 118 | /// i18Next.t('key1') // "Hello World!" 119 | /// ``` 120 | final String? nestingPrefix, nestingSuffix, nestingSeparator; 121 | 122 | /// [formatter] is called when an interpolation has been found and is ready 123 | /// for substitution. 124 | /// 125 | /// Defaults to [defaultFormatter], which simply returns the value itself in 126 | /// String form ([Object.toString]). 127 | final ArgumentFormatter? formatter; 128 | 129 | /// Creates a new instance of [I18NextOptions] overriding any properties 130 | /// where [other] isn't null. 131 | /// 132 | /// If [other] is null, returns this. 133 | I18NextOptions merge(I18NextOptions? other) { 134 | if (other == null) return this; 135 | return copyWith( 136 | fallbackNamespaces: other.fallbackNamespaces ?? fallbackNamespaces, 137 | namespaceSeparator: other.namespaceSeparator ?? namespaceSeparator, 138 | contextSeparator: other.contextSeparator ?? contextSeparator, 139 | pluralSeparator: other.pluralSeparator ?? pluralSeparator, 140 | keySeparator: other.keySeparator ?? keySeparator, 141 | pluralSuffix: other.pluralSuffix ?? pluralSuffix, 142 | interpolationPrefix: other.interpolationPrefix ?? interpolationPrefix, 143 | interpolationSuffix: other.interpolationSuffix ?? interpolationSuffix, 144 | interpolationSeparator: 145 | other.interpolationSeparator ?? interpolationSeparator, 146 | nestingPrefix: other.nestingPrefix ?? nestingPrefix, 147 | nestingSuffix: other.nestingSuffix ?? nestingSuffix, 148 | nestingSeparator: other.nestingSeparator ?? nestingSeparator, 149 | formatter: other.formatter ?? formatter, 150 | ); 151 | } 152 | 153 | /// Creates a new instance of [I18NextOptions] overriding any of the 154 | /// properties that aren't null. 155 | I18NextOptions copyWith({ 156 | List? fallbackNamespaces, 157 | String? namespaceSeparator, 158 | String? contextSeparator, 159 | String? pluralSeparator, 160 | String? keySeparator, 161 | String? pluralSuffix, 162 | String? interpolationPrefix, 163 | String? interpolationSuffix, 164 | String? interpolationSeparator, 165 | String? nestingPrefix, 166 | String? nestingSuffix, 167 | String? nestingSeparator, 168 | ArgumentFormatter? formatter, 169 | }) { 170 | return I18NextOptions( 171 | fallbackNamespaces: fallbackNamespaces ?? this.fallbackNamespaces, 172 | namespaceSeparator: namespaceSeparator ?? this.namespaceSeparator, 173 | contextSeparator: contextSeparator ?? this.contextSeparator, 174 | pluralSeparator: pluralSeparator ?? this.pluralSeparator, 175 | keySeparator: keySeparator ?? this.keySeparator, 176 | pluralSuffix: pluralSuffix ?? this.pluralSuffix, 177 | interpolationPrefix: interpolationPrefix ?? this.interpolationPrefix, 178 | interpolationSuffix: interpolationSuffix ?? this.interpolationSuffix, 179 | interpolationSeparator: 180 | interpolationSeparator ?? this.interpolationSeparator, 181 | nestingPrefix: nestingPrefix ?? this.nestingPrefix, 182 | nestingSuffix: nestingSuffix ?? this.nestingSuffix, 183 | nestingSeparator: nestingSeparator ?? this.nestingSeparator, 184 | formatter: formatter ?? this.formatter, 185 | ); 186 | } 187 | 188 | @override 189 | int get hashCode => hashValues( 190 | namespaceSeparator, 191 | contextSeparator, 192 | pluralSeparator, 193 | keySeparator, 194 | interpolationPrefix, 195 | interpolationSuffix, 196 | interpolationSeparator, 197 | nestingPrefix, 198 | nestingSuffix, 199 | nestingSeparator, 200 | pluralSuffix, 201 | formatter, 202 | ); 203 | 204 | @override 205 | bool operator ==(Object other) => 206 | other.runtimeType == runtimeType && 207 | other is I18NextOptions && 208 | other.fallbackNamespaces == fallbackNamespaces && 209 | other.namespaceSeparator == namespaceSeparator && 210 | other.contextSeparator == contextSeparator && 211 | other.pluralSeparator == pluralSeparator && 212 | other.keySeparator == keySeparator && 213 | other.interpolationPrefix == interpolationPrefix && 214 | other.interpolationSuffix == interpolationSuffix && 215 | other.interpolationSeparator == interpolationSeparator && 216 | other.nestingPrefix == nestingPrefix && 217 | other.nestingSuffix == nestingSuffix && 218 | other.nestingSeparator == nestingSeparator && 219 | other.pluralSuffix == pluralSuffix && 220 | other.formatter == formatter; 221 | 222 | @override 223 | void debugFillProperties(DiagnosticPropertiesBuilder properties) { 224 | super.debugFillProperties(properties); 225 | properties 226 | ..add(IterableProperty('fallbackNamespaces', fallbackNamespaces)) 227 | ..add(StringProperty('namespaceSeparator', namespaceSeparator)) 228 | ..add(StringProperty('contextSeparator', contextSeparator)) 229 | ..add(StringProperty('pluralSeparator', pluralSeparator)) 230 | ..add(StringProperty('keySeparator', keySeparator)) 231 | ..add(StringProperty('interpolationPrefix', interpolationPrefix)) 232 | ..add(StringProperty('interpolationSuffix', interpolationSuffix)) 233 | ..add(StringProperty('interpolationSeparator', interpolationSeparator)) 234 | ..add(StringProperty('nestingPrefix', nestingPrefix)) 235 | ..add(StringProperty('nestingSuffix', nestingSuffix)) 236 | ..add(StringProperty('nestingSeparator', nestingSeparator)) 237 | ..add(StringProperty('pluralSuffix', pluralSuffix)) 238 | ..add(StringProperty('formatter', '$formatter')); 239 | } 240 | 241 | /// Simply returns [value] in string form. Ignores [format] and [locale]. 242 | static String defaultFormatter(Object value, String? format, Locale locale) => 243 | value.toString(); 244 | } 245 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /test/interpolator_test.dart: -------------------------------------------------------------------------------- 1 | import 'dart:ui'; 2 | 3 | import 'package:flutter_test/flutter_test.dart'; 4 | import 'package:i18next/i18next.dart'; 5 | import 'package:i18next/interpolator.dart'; 6 | 7 | void main() { 8 | const baseOptions = I18NextOptions.base; 9 | const defaultFormatter = I18NextOptions.defaultFormatter; 10 | const defaultLocale = Locale('en'); 11 | 12 | group('interpolate', () { 13 | String interpol( 14 | String string, { 15 | Map variables = const {}, 16 | Locale locale = defaultLocale, 17 | ArgumentFormatter? formatter, 18 | }) { 19 | final options = baseOptions.copyWith(formatter: formatter); 20 | return interpolate(locale, string, variables, options); 21 | } 22 | 23 | test('given a non matching string', () { 24 | expect( 25 | interpol( 26 | 'This is a normal string', 27 | formatter: expectAsync3(defaultFormatter, count: 0), 28 | ), 29 | 'This is a normal string', 30 | ); 31 | }); 32 | 33 | group('given a matching string', () { 34 | test('without variable or format', () { 35 | expect( 36 | interpol( 37 | 'This is a {{}} string', 38 | formatter: expectAsync3(defaultFormatter, count: 0), 39 | ), 40 | 'This is a {{}} string', 41 | ); 42 | }); 43 | 44 | group('with variable only', () { 45 | test('without variables', () { 46 | expect( 47 | interpol( 48 | 'This is a {{variable}} string', 49 | formatter: expectAsync3(defaultFormatter, count: 0), 50 | ), 51 | 'This is a {{variable}} string', 52 | ); 53 | }); 54 | 55 | test('with replaceable variables', () { 56 | expect( 57 | interpol( 58 | 'This is a {{variable}} string', 59 | variables: {'variable': 'my variable'}, 60 | formatter: expectAsync3((variable, format, locale) { 61 | expect(variable, 'my variable'); 62 | expect(format, isNull); 63 | expect(locale, defaultLocale); 64 | return 'VALUE'; 65 | }), 66 | ), 67 | 'This is a VALUE string', 68 | ); 69 | }); 70 | 71 | test('with replaceable grouped variables', () { 72 | expect( 73 | interpol( 74 | 'This is a {{grouped.key.variable}} string', 75 | variables: { 76 | 'grouped': { 77 | 'key': {'variable': 'grouped variable'} 78 | } 79 | }, 80 | ), 81 | 'This is a grouped variable string', 82 | ); 83 | }); 84 | 85 | test('with partially matching replaceable grouped variables', () { 86 | expect( 87 | interpol( 88 | 'This is a {{grouped.key.variable}} string', 89 | variables: { 90 | 'grouped': {'key': 'grouped variable'} 91 | }, 92 | ), 93 | 'This is a {{grouped.key.variable}} string', 94 | ); 95 | }); 96 | 97 | test('without replaceable variables', () { 98 | expect( 99 | interpol( 100 | 'This is a {{variable}} string', 101 | variables: {'another': 'value'}, 102 | formatter: expectAsync3(defaultFormatter, count: 0), 103 | ), 104 | 'This is a {{variable}} string', 105 | ); 106 | }); 107 | }); 108 | 109 | test('with format only', () { 110 | expect( 111 | interpol( 112 | 'This is a {{, some format}} string', 113 | formatter: expectAsync3(defaultFormatter, count: 0), 114 | ), 115 | 'This is a {{, some format}} string', 116 | ); 117 | }); 118 | 119 | test('with variable and format and replaceable variables', () { 120 | expect( 121 | interpol( 122 | 'This is a {{variable, format}} string', 123 | variables: {'variable': 'my variable'}, 124 | formatter: expectAsync3((variable, format, locale) { 125 | expect(variable, 'my variable'); 126 | expect(format, 'format'); 127 | expect(locale, defaultLocale); 128 | return 'VALUE'; 129 | }), 130 | ), 131 | 'This is a VALUE string', 132 | ); 133 | }); 134 | 135 | test('given locale', () { 136 | const anotherLocale = Locale('any'); 137 | expect( 138 | interpol( 139 | 'This is a {{variable}} string', 140 | locale: anotherLocale, 141 | variables: {'variable': 'my variable'}, 142 | formatter: expectAsync3((variable, format, locale) { 143 | expect(variable, 'my variable'); 144 | expect(format, isNull); 145 | expect(locale, anotherLocale); 146 | return 'VALUE'; 147 | }), 148 | ), 149 | 'This is a VALUE string', 150 | ); 151 | }); 152 | }); 153 | }); 154 | 155 | group('nest', () { 156 | String nst( 157 | String string, { 158 | Locale locale = defaultLocale, 159 | Map variables = const {}, 160 | Translate translate = _defaultTranslate, 161 | I18NextOptions options = baseOptions, 162 | }) { 163 | return nest(locale, string, translate, variables, options); 164 | } 165 | 166 | test('given a non matching string', () { 167 | expect( 168 | nst( 169 | 'This is my unmatching string', 170 | translate: expectAsync4(_defaultTranslate, count: 0), 171 | ), 172 | 'This is my unmatching string', 173 | ); 174 | }); 175 | 176 | group('given a nesting string', () { 177 | test('without key or variables', () { 178 | expect( 179 | nst( 180 | r'This is my $t() string', 181 | translate: expectAsync4(_defaultTranslate, count: 0), 182 | ), 183 | r'This is my $t() string', 184 | ); 185 | }); 186 | 187 | test('with key only', () { 188 | expect( 189 | nst( 190 | r'This is my $t(key) string', 191 | translate: expectAsync4((key, b, c, d) { 192 | expect(key, 'key'); 193 | return 'VALUE'; 194 | }), 195 | ), 196 | r'This is my VALUE string', 197 | ); 198 | }); 199 | 200 | test('with variables only', () { 201 | expect( 202 | nst( 203 | r'This is my $t(, {"x": "y"}) string', 204 | translate: expectAsync4(_defaultTranslate, count: 0), 205 | ), 206 | r'This is my $t(, {"x": "y"}) string', 207 | ); 208 | }); 209 | 210 | group('with key+variables', () { 211 | group('when variables are a well formed json', () { 212 | const string = r'This is my $t(key, {"x":"y"}) string'; 213 | 214 | test('the deserialized variables are passed', () { 215 | expect( 216 | nst( 217 | r'This is my $t(key, {"x":"y"}) string', 218 | translate: expectAsync4((key, b, variables, d) { 219 | expect(key, 'key'); 220 | expect(variables, {'x': 'y'}); 221 | return 'VALUE'; 222 | }), 223 | ), 224 | 'This is my VALUE string', 225 | ); 226 | }); 227 | 228 | test('the new variables are merged with the previous variables', () { 229 | expect( 230 | nst( 231 | string, 232 | variables: const {'x': 'x', 'y': 'y', 'z': 'z'}, 233 | translate: expectAsync4((key, b, variables, d) { 234 | expect(key, 'key'); 235 | expect( 236 | variables, 237 | // overridden "x" for "y" 238 | {'x': 'y', 'y': 'y', 'z': 'z'}, 239 | ); 240 | return 'VALUE'; 241 | }), 242 | ), 243 | 'This is my VALUE string', 244 | ); 245 | }); 246 | }); 247 | 248 | test('when variables are a malformed json', () { 249 | expect( 250 | nst( 251 | r'This is my $t(key, "x") string', 252 | translate: expectAsync4((key, b, variables, d) { 253 | expect(key, 'key'); 254 | expect(variables, isEmpty); 255 | return 'VALUE'; 256 | }), 257 | ), 258 | r'This is my VALUE string', 259 | ); 260 | }); 261 | }); 262 | 263 | test('with multiple split points', () { 264 | expect( 265 | nst( 266 | r'This is my $t(key, {"a":"a"}, {"b":"b"}) string', 267 | translate: expectAsync4((key, b, variables, d) { 268 | expect(key, 'key'); 269 | expect(variables, isEmpty); 270 | return 'VALUE'; 271 | }), 272 | ), 273 | r'This is my VALUE string', 274 | ); 275 | }); 276 | 277 | test('given locale and options', () { 278 | const locale = Locale('any'); 279 | 280 | expect( 281 | nst( 282 | r'This is my $t(key) string', 283 | locale: locale, 284 | options: baseOptions, 285 | translate: expectAsync4((key, loc, variables, options) { 286 | expect(loc, locale); 287 | expect(options, baseOptions); 288 | return 'VALUE'; 289 | }), 290 | ), 291 | r'This is my VALUE string', 292 | ); 293 | }); 294 | }); 295 | }); 296 | 297 | group('interpolationPattern', () { 298 | final pattern = interpolationPattern(baseOptions); 299 | 300 | Iterable> allMatches(String text) => 301 | pattern.allMatches(text).map((match) => [ 302 | match.namedGroup('variable'), 303 | match.namedGroup('format'), 304 | ]); 305 | 306 | test('default pattern', () { 307 | expect( 308 | pattern.pattern, 309 | r'\{\{(?.*?)(,\s*(?.*?)\s*)?\}\}', 310 | ); 311 | }); 312 | 313 | test('when has only one match without format', () { 314 | expect(allMatches('My text has {{one}} match'), [ 315 | ['one', null] 316 | ]); 317 | }); 318 | 319 | test('when has only one match with format', () { 320 | expect(allMatches('My text has {{one, Xyz}} match'), [ 321 | ['one', 'Xyz'] 322 | ]); 323 | }); 324 | 325 | test('when has only one match with format with whitespaces', () { 326 | expect(allMatches('My text has {{one, Xyz}} match'), [ 327 | ['one', 'Xyz'] 328 | ]); 329 | expect(allMatches('My text has {{one, Xyz }} match'), [ 330 | ['one', 'Xyz'] 331 | ]); 332 | expect(allMatches('My text has {{one, Xyz }} match'), [ 333 | ['one', 'Xyz'] 334 | ]); 335 | expect(allMatches('My text has {{one, \nXyz\n}} match'), [ 336 | ['one', 'Xyz'] 337 | ]); 338 | }); 339 | 340 | test('when has multiple matches without formats', () { 341 | expect(allMatches('My {{text}} {{has}} {{four}} {{matches}}'), [ 342 | ['text', null], 343 | ['has', null], 344 | ['four', null], 345 | ['matches', null] 346 | ]); 347 | }); 348 | 349 | test('when has multiple matches with formats', () { 350 | expect( 351 | allMatches( 352 | 'My {{text, Aaa}} {{has, Bbb}} {{four, Ccc}} {{matches, Ddd}}', 353 | ), 354 | [ 355 | ['text', 'Aaa'], 356 | ['has', 'Bbb'], 357 | ['four', 'Ccc'], 358 | ['matches', 'Ddd'] 359 | ], 360 | ); 361 | }); 362 | 363 | test('when has multiple mixed matches', () { 364 | final matches = allMatches( 365 | 'My {{text}} {{has, Bbb}} {{four, Ccc}} {{matches}}', 366 | ); 367 | 368 | expect(matches, [ 369 | ['text', null], 370 | ['has', 'Bbb'], 371 | ['four', 'Ccc'], 372 | ['matches', null] 373 | ]); 374 | }); 375 | }); 376 | 377 | group('nestingPattern', () { 378 | final pattern = nestingPattern(baseOptions); 379 | 380 | Iterable> allMatches(String text) => 381 | pattern.allMatches(text).map((match) => [ 382 | match.namedGroup('key'), 383 | match.namedGroup('variables'), 384 | ]); 385 | 386 | test('default pattern', () { 387 | expect( 388 | pattern.pattern, 389 | r'\$t\((?.*?)(,\s*(?.*?)\s*)?\)', 390 | ); 391 | }); 392 | 393 | test('when has only one match without variables', () { 394 | expect(allMatches(r'My text has $t(one) match'), [ 395 | ['one', null] 396 | ]); 397 | }); 398 | 399 | test('when has only one match with variables', () { 400 | expect(allMatches(r'My text has $t(one, {"my": "values"}) match'), [ 401 | ['one', '{"my": "values"}'] 402 | ]); 403 | }); 404 | 405 | test('when has only one match with variables and whitespaces', () { 406 | expect(allMatches(r'My text has $t(one, Xyz) match'), [ 407 | ['one', 'Xyz'] 408 | ]); 409 | expect(allMatches(r'My text has $t(one, Xyz ) match'), [ 410 | ['one', 'Xyz'] 411 | ]); 412 | expect(allMatches(r'My text has $t(one, Xyz ) match'), [ 413 | ['one', 'Xyz'] 414 | ]); 415 | expect(allMatches('My text has \$t(one, \nXyz\n) match'), [ 416 | ['one', 'Xyz'] 417 | ]); 418 | }); 419 | 420 | test('when has multiple matches without formats', () { 421 | expect(allMatches(r'My $t(text) $t(has) $t(four) $t(matches)'), [ 422 | ['text', null], 423 | ['has', null], 424 | ['four', null], 425 | ['matches', null] 426 | ]); 427 | }); 428 | 429 | test('when has multiple matches with formats', () { 430 | expect( 431 | allMatches( 432 | r'My $t(text, Aaa) $t(has, Bbb) $t(four, Ccc) $t(matches, Ddd)', 433 | ), 434 | [ 435 | ['text', 'Aaa'], 436 | ['has', 'Bbb'], 437 | ['four', 'Ccc'], 438 | ['matches', 'Ddd'] 439 | ], 440 | ); 441 | }); 442 | 443 | test('when has multiple mixed matches', () { 444 | final matches = allMatches( 445 | r'My $t(text) $t(has, Bbb) $t(four, Ccc) $t(matches)', 446 | ); 447 | 448 | expect(matches, [ 449 | ['text', null], 450 | ['has', 'Bbb'], 451 | ['four', 'Ccc'], 452 | ['matches', null] 453 | ]); 454 | }); 455 | }); 456 | } 457 | 458 | String? _defaultTranslate( 459 | String key, 460 | Locale locale, 461 | Map variables, 462 | I18NextOptions options, 463 | ) => 464 | key; 465 | -------------------------------------------------------------------------------- /example/ios/Runner.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 46; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | 1498D2341E8E89220040F4C2 /* GeneratedPluginRegistrant.m in Sources */ = {isa = PBXBuildFile; fileRef = 1498D2331E8E89220040F4C2 /* GeneratedPluginRegistrant.m */; }; 11 | 3B3967161E833CAA004F5970 /* AppFrameworkInfo.plist in Resources */ = {isa = PBXBuildFile; fileRef = 3B3967151E833CAA004F5970 /* AppFrameworkInfo.plist */; }; 12 | 74858FAF1ED2DC5600515810 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 74858FAE1ED2DC5600515810 /* AppDelegate.swift */; }; 13 | 97C146FC1CF9000F007C117D /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 97C146FA1CF9000F007C117D /* Main.storyboard */; }; 14 | 97C146FE1CF9000F007C117D /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 97C146FD1CF9000F007C117D /* Assets.xcassets */; }; 15 | 97C147011CF9000F007C117D /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 97C146FF1CF9000F007C117D /* LaunchScreen.storyboard */; }; 16 | /* End PBXBuildFile section */ 17 | 18 | /* Begin PBXCopyFilesBuildPhase section */ 19 | 9705A1C41CF9048500538489 /* Embed Frameworks */ = { 20 | isa = PBXCopyFilesBuildPhase; 21 | buildActionMask = 2147483647; 22 | dstPath = ""; 23 | dstSubfolderSpec = 10; 24 | files = ( 25 | ); 26 | name = "Embed Frameworks"; 27 | runOnlyForDeploymentPostprocessing = 0; 28 | }; 29 | /* End PBXCopyFilesBuildPhase section */ 30 | 31 | /* Begin PBXFileReference section */ 32 | 1498D2321E8E86230040F4C2 /* GeneratedPluginRegistrant.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = GeneratedPluginRegistrant.h; sourceTree = ""; }; 33 | 1498D2331E8E89220040F4C2 /* GeneratedPluginRegistrant.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GeneratedPluginRegistrant.m; sourceTree = ""; }; 34 | 3B3967151E833CAA004F5970 /* AppFrameworkInfo.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; name = AppFrameworkInfo.plist; path = Flutter/AppFrameworkInfo.plist; sourceTree = ""; }; 35 | 74858FAD1ED2DC5600515810 /* Runner-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "Runner-Bridging-Header.h"; sourceTree = ""; }; 36 | 74858FAE1ED2DC5600515810 /* AppDelegate.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; 37 | 7AFA3C8E1D35360C0083082E /* Release.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; name = Release.xcconfig; path = Flutter/Release.xcconfig; sourceTree = ""; }; 38 | 9740EEB21CF90195004384FC /* Debug.xcconfig */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xcconfig; name = Debug.xcconfig; path = Flutter/Debug.xcconfig; sourceTree = ""; }; 39 | 9740EEB31CF90195004384FC /* Generated.xcconfig */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xcconfig; name = Generated.xcconfig; path = Flutter/Generated.xcconfig; sourceTree = ""; }; 40 | 97C146EE1CF9000F007C117D /* Runner.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Runner.app; sourceTree = BUILT_PRODUCTS_DIR; }; 41 | 97C146FB1CF9000F007C117D /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; 42 | 97C146FD1CF9000F007C117D /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 43 | 97C147001CF9000F007C117D /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; 44 | 97C147021CF9000F007C117D /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 45 | /* End PBXFileReference section */ 46 | 47 | /* Begin PBXFrameworksBuildPhase section */ 48 | 97C146EB1CF9000F007C117D /* Frameworks */ = { 49 | isa = PBXFrameworksBuildPhase; 50 | buildActionMask = 2147483647; 51 | files = ( 52 | ); 53 | runOnlyForDeploymentPostprocessing = 0; 54 | }; 55 | /* End PBXFrameworksBuildPhase section */ 56 | 57 | /* Begin PBXGroup section */ 58 | 9740EEB11CF90186004384FC /* Flutter */ = { 59 | isa = PBXGroup; 60 | children = ( 61 | 3B3967151E833CAA004F5970 /* AppFrameworkInfo.plist */, 62 | 9740EEB21CF90195004384FC /* Debug.xcconfig */, 63 | 7AFA3C8E1D35360C0083082E /* Release.xcconfig */, 64 | 9740EEB31CF90195004384FC /* Generated.xcconfig */, 65 | ); 66 | name = Flutter; 67 | sourceTree = ""; 68 | }; 69 | 97C146E51CF9000F007C117D = { 70 | isa = PBXGroup; 71 | children = ( 72 | 9740EEB11CF90186004384FC /* Flutter */, 73 | 97C146F01CF9000F007C117D /* Runner */, 74 | 97C146EF1CF9000F007C117D /* Products */, 75 | ); 76 | sourceTree = ""; 77 | }; 78 | 97C146EF1CF9000F007C117D /* Products */ = { 79 | isa = PBXGroup; 80 | children = ( 81 | 97C146EE1CF9000F007C117D /* Runner.app */, 82 | ); 83 | name = Products; 84 | sourceTree = ""; 85 | }; 86 | 97C146F01CF9000F007C117D /* Runner */ = { 87 | isa = PBXGroup; 88 | children = ( 89 | 97C146FA1CF9000F007C117D /* Main.storyboard */, 90 | 97C146FD1CF9000F007C117D /* Assets.xcassets */, 91 | 97C146FF1CF9000F007C117D /* LaunchScreen.storyboard */, 92 | 97C147021CF9000F007C117D /* Info.plist */, 93 | 1498D2321E8E86230040F4C2 /* GeneratedPluginRegistrant.h */, 94 | 1498D2331E8E89220040F4C2 /* GeneratedPluginRegistrant.m */, 95 | 74858FAE1ED2DC5600515810 /* AppDelegate.swift */, 96 | 74858FAD1ED2DC5600515810 /* Runner-Bridging-Header.h */, 97 | ); 98 | path = Runner; 99 | sourceTree = ""; 100 | }; 101 | /* End PBXGroup section */ 102 | 103 | /* Begin PBXNativeTarget section */ 104 | 97C146ED1CF9000F007C117D /* Runner */ = { 105 | isa = PBXNativeTarget; 106 | buildConfigurationList = 97C147051CF9000F007C117D /* Build configuration list for PBXNativeTarget "Runner" */; 107 | buildPhases = ( 108 | 9740EEB61CF901F6004384FC /* Run Script */, 109 | 97C146EA1CF9000F007C117D /* Sources */, 110 | 97C146EB1CF9000F007C117D /* Frameworks */, 111 | 97C146EC1CF9000F007C117D /* Resources */, 112 | 9705A1C41CF9048500538489 /* Embed Frameworks */, 113 | 3B06AD1E1E4923F5004D2608 /* Thin Binary */, 114 | ); 115 | buildRules = ( 116 | ); 117 | dependencies = ( 118 | ); 119 | name = Runner; 120 | productName = Runner; 121 | productReference = 97C146EE1CF9000F007C117D /* Runner.app */; 122 | productType = "com.apple.product-type.application"; 123 | }; 124 | /* End PBXNativeTarget section */ 125 | 126 | /* Begin PBXProject section */ 127 | 97C146E61CF9000F007C117D /* Project object */ = { 128 | isa = PBXProject; 129 | attributes = { 130 | LastUpgradeCheck = 1020; 131 | ORGANIZATIONNAME = ""; 132 | TargetAttributes = { 133 | 97C146ED1CF9000F007C117D = { 134 | CreatedOnToolsVersion = 7.3.1; 135 | LastSwiftMigration = 1100; 136 | }; 137 | }; 138 | }; 139 | buildConfigurationList = 97C146E91CF9000F007C117D /* Build configuration list for PBXProject "Runner" */; 140 | compatibilityVersion = "Xcode 9.3"; 141 | developmentRegion = en; 142 | hasScannedForEncodings = 0; 143 | knownRegions = ( 144 | en, 145 | Base, 146 | ); 147 | mainGroup = 97C146E51CF9000F007C117D; 148 | productRefGroup = 97C146EF1CF9000F007C117D /* Products */; 149 | projectDirPath = ""; 150 | projectRoot = ""; 151 | targets = ( 152 | 97C146ED1CF9000F007C117D /* Runner */, 153 | ); 154 | }; 155 | /* End PBXProject section */ 156 | 157 | /* Begin PBXResourcesBuildPhase section */ 158 | 97C146EC1CF9000F007C117D /* Resources */ = { 159 | isa = PBXResourcesBuildPhase; 160 | buildActionMask = 2147483647; 161 | files = ( 162 | 97C147011CF9000F007C117D /* LaunchScreen.storyboard in Resources */, 163 | 3B3967161E833CAA004F5970 /* AppFrameworkInfo.plist in Resources */, 164 | 97C146FE1CF9000F007C117D /* Assets.xcassets in Resources */, 165 | 97C146FC1CF9000F007C117D /* Main.storyboard in Resources */, 166 | ); 167 | runOnlyForDeploymentPostprocessing = 0; 168 | }; 169 | /* End PBXResourcesBuildPhase section */ 170 | 171 | /* Begin PBXShellScriptBuildPhase section */ 172 | 3B06AD1E1E4923F5004D2608 /* Thin Binary */ = { 173 | isa = PBXShellScriptBuildPhase; 174 | buildActionMask = 2147483647; 175 | files = ( 176 | ); 177 | inputPaths = ( 178 | ); 179 | name = "Thin Binary"; 180 | outputPaths = ( 181 | ); 182 | runOnlyForDeploymentPostprocessing = 0; 183 | shellPath = /bin/sh; 184 | shellScript = "/bin/sh \"$FLUTTER_ROOT/packages/flutter_tools/bin/xcode_backend.sh\" embed_and_thin"; 185 | }; 186 | 9740EEB61CF901F6004384FC /* Run Script */ = { 187 | isa = PBXShellScriptBuildPhase; 188 | buildActionMask = 2147483647; 189 | files = ( 190 | ); 191 | inputPaths = ( 192 | ); 193 | name = "Run Script"; 194 | outputPaths = ( 195 | ); 196 | runOnlyForDeploymentPostprocessing = 0; 197 | shellPath = /bin/sh; 198 | shellScript = "/bin/sh \"$FLUTTER_ROOT/packages/flutter_tools/bin/xcode_backend.sh\" build"; 199 | }; 200 | /* End PBXShellScriptBuildPhase section */ 201 | 202 | /* Begin PBXSourcesBuildPhase section */ 203 | 97C146EA1CF9000F007C117D /* Sources */ = { 204 | isa = PBXSourcesBuildPhase; 205 | buildActionMask = 2147483647; 206 | files = ( 207 | 74858FAF1ED2DC5600515810 /* AppDelegate.swift in Sources */, 208 | 1498D2341E8E89220040F4C2 /* GeneratedPluginRegistrant.m in Sources */, 209 | ); 210 | runOnlyForDeploymentPostprocessing = 0; 211 | }; 212 | /* End PBXSourcesBuildPhase section */ 213 | 214 | /* Begin PBXVariantGroup section */ 215 | 97C146FA1CF9000F007C117D /* Main.storyboard */ = { 216 | isa = PBXVariantGroup; 217 | children = ( 218 | 97C146FB1CF9000F007C117D /* Base */, 219 | ); 220 | name = Main.storyboard; 221 | sourceTree = ""; 222 | }; 223 | 97C146FF1CF9000F007C117D /* LaunchScreen.storyboard */ = { 224 | isa = PBXVariantGroup; 225 | children = ( 226 | 97C147001CF9000F007C117D /* Base */, 227 | ); 228 | name = LaunchScreen.storyboard; 229 | sourceTree = ""; 230 | }; 231 | /* End PBXVariantGroup section */ 232 | 233 | /* Begin XCBuildConfiguration section */ 234 | 249021D3217E4FDB00AE95B9 /* Profile */ = { 235 | isa = XCBuildConfiguration; 236 | buildSettings = { 237 | ALWAYS_SEARCH_USER_PATHS = NO; 238 | CLANG_ANALYZER_NONNULL = YES; 239 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 240 | CLANG_CXX_LIBRARY = "libc++"; 241 | CLANG_ENABLE_MODULES = YES; 242 | CLANG_ENABLE_OBJC_ARC = YES; 243 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 244 | CLANG_WARN_BOOL_CONVERSION = YES; 245 | CLANG_WARN_COMMA = YES; 246 | CLANG_WARN_CONSTANT_CONVERSION = YES; 247 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 248 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 249 | CLANG_WARN_EMPTY_BODY = YES; 250 | CLANG_WARN_ENUM_CONVERSION = YES; 251 | CLANG_WARN_INFINITE_RECURSION = YES; 252 | CLANG_WARN_INT_CONVERSION = YES; 253 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 254 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 255 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 256 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 257 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 258 | CLANG_WARN_STRICT_PROTOTYPES = YES; 259 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 260 | CLANG_WARN_UNREACHABLE_CODE = YES; 261 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 262 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 263 | COPY_PHASE_STRIP = NO; 264 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 265 | ENABLE_NS_ASSERTIONS = NO; 266 | ENABLE_STRICT_OBJC_MSGSEND = YES; 267 | GCC_C_LANGUAGE_STANDARD = gnu99; 268 | GCC_NO_COMMON_BLOCKS = YES; 269 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 270 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 271 | GCC_WARN_UNDECLARED_SELECTOR = YES; 272 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 273 | GCC_WARN_UNUSED_FUNCTION = YES; 274 | GCC_WARN_UNUSED_VARIABLE = YES; 275 | IPHONEOS_DEPLOYMENT_TARGET = 9.0; 276 | MTL_ENABLE_DEBUG_INFO = NO; 277 | SDKROOT = iphoneos; 278 | SUPPORTED_PLATFORMS = iphoneos; 279 | TARGETED_DEVICE_FAMILY = "1,2"; 280 | VALIDATE_PRODUCT = YES; 281 | }; 282 | name = Profile; 283 | }; 284 | 249021D4217E4FDB00AE95B9 /* Profile */ = { 285 | isa = XCBuildConfiguration; 286 | baseConfigurationReference = 7AFA3C8E1D35360C0083082E /* Release.xcconfig */; 287 | buildSettings = { 288 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 289 | CLANG_ENABLE_MODULES = YES; 290 | CURRENT_PROJECT_VERSION = "$(FLUTTER_BUILD_NUMBER)"; 291 | ENABLE_BITCODE = NO; 292 | INFOPLIST_FILE = Runner/Info.plist; 293 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 294 | PRODUCT_BUNDLE_IDENTIFIER = com.example.example; 295 | PRODUCT_NAME = "$(TARGET_NAME)"; 296 | SWIFT_OBJC_BRIDGING_HEADER = "Runner/Runner-Bridging-Header.h"; 297 | SWIFT_VERSION = 5.0; 298 | VERSIONING_SYSTEM = "apple-generic"; 299 | }; 300 | name = Profile; 301 | }; 302 | 97C147031CF9000F007C117D /* Debug */ = { 303 | isa = XCBuildConfiguration; 304 | buildSettings = { 305 | ALWAYS_SEARCH_USER_PATHS = NO; 306 | CLANG_ANALYZER_NONNULL = YES; 307 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 308 | CLANG_CXX_LIBRARY = "libc++"; 309 | CLANG_ENABLE_MODULES = YES; 310 | CLANG_ENABLE_OBJC_ARC = YES; 311 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 312 | CLANG_WARN_BOOL_CONVERSION = YES; 313 | CLANG_WARN_COMMA = YES; 314 | CLANG_WARN_CONSTANT_CONVERSION = YES; 315 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 316 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 317 | CLANG_WARN_EMPTY_BODY = YES; 318 | CLANG_WARN_ENUM_CONVERSION = YES; 319 | CLANG_WARN_INFINITE_RECURSION = YES; 320 | CLANG_WARN_INT_CONVERSION = YES; 321 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 322 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 323 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 324 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 325 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 326 | CLANG_WARN_STRICT_PROTOTYPES = YES; 327 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 328 | CLANG_WARN_UNREACHABLE_CODE = YES; 329 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 330 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 331 | COPY_PHASE_STRIP = NO; 332 | DEBUG_INFORMATION_FORMAT = dwarf; 333 | ENABLE_STRICT_OBJC_MSGSEND = YES; 334 | ENABLE_TESTABILITY = YES; 335 | GCC_C_LANGUAGE_STANDARD = gnu99; 336 | GCC_DYNAMIC_NO_PIC = NO; 337 | GCC_NO_COMMON_BLOCKS = YES; 338 | GCC_OPTIMIZATION_LEVEL = 0; 339 | GCC_PREPROCESSOR_DEFINITIONS = ( 340 | "DEBUG=1", 341 | "$(inherited)", 342 | ); 343 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 344 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 345 | GCC_WARN_UNDECLARED_SELECTOR = YES; 346 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 347 | GCC_WARN_UNUSED_FUNCTION = YES; 348 | GCC_WARN_UNUSED_VARIABLE = YES; 349 | IPHONEOS_DEPLOYMENT_TARGET = 9.0; 350 | MTL_ENABLE_DEBUG_INFO = YES; 351 | ONLY_ACTIVE_ARCH = YES; 352 | SDKROOT = iphoneos; 353 | TARGETED_DEVICE_FAMILY = "1,2"; 354 | }; 355 | name = Debug; 356 | }; 357 | 97C147041CF9000F007C117D /* Release */ = { 358 | isa = XCBuildConfiguration; 359 | buildSettings = { 360 | ALWAYS_SEARCH_USER_PATHS = NO; 361 | CLANG_ANALYZER_NONNULL = YES; 362 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 363 | CLANG_CXX_LIBRARY = "libc++"; 364 | CLANG_ENABLE_MODULES = YES; 365 | CLANG_ENABLE_OBJC_ARC = YES; 366 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 367 | CLANG_WARN_BOOL_CONVERSION = YES; 368 | CLANG_WARN_COMMA = YES; 369 | CLANG_WARN_CONSTANT_CONVERSION = YES; 370 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 371 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 372 | CLANG_WARN_EMPTY_BODY = YES; 373 | CLANG_WARN_ENUM_CONVERSION = YES; 374 | CLANG_WARN_INFINITE_RECURSION = YES; 375 | CLANG_WARN_INT_CONVERSION = YES; 376 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 377 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 378 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 379 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 380 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 381 | CLANG_WARN_STRICT_PROTOTYPES = YES; 382 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 383 | CLANG_WARN_UNREACHABLE_CODE = YES; 384 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 385 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 386 | COPY_PHASE_STRIP = NO; 387 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 388 | ENABLE_NS_ASSERTIONS = NO; 389 | ENABLE_STRICT_OBJC_MSGSEND = YES; 390 | GCC_C_LANGUAGE_STANDARD = gnu99; 391 | GCC_NO_COMMON_BLOCKS = YES; 392 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 393 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 394 | GCC_WARN_UNDECLARED_SELECTOR = YES; 395 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 396 | GCC_WARN_UNUSED_FUNCTION = YES; 397 | GCC_WARN_UNUSED_VARIABLE = YES; 398 | IPHONEOS_DEPLOYMENT_TARGET = 9.0; 399 | MTL_ENABLE_DEBUG_INFO = NO; 400 | SDKROOT = iphoneos; 401 | SUPPORTED_PLATFORMS = iphoneos; 402 | SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule"; 403 | TARGETED_DEVICE_FAMILY = "1,2"; 404 | VALIDATE_PRODUCT = YES; 405 | }; 406 | name = Release; 407 | }; 408 | 97C147061CF9000F007C117D /* Debug */ = { 409 | isa = XCBuildConfiguration; 410 | baseConfigurationReference = 9740EEB21CF90195004384FC /* Debug.xcconfig */; 411 | buildSettings = { 412 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 413 | CLANG_ENABLE_MODULES = YES; 414 | CURRENT_PROJECT_VERSION = "$(FLUTTER_BUILD_NUMBER)"; 415 | ENABLE_BITCODE = NO; 416 | INFOPLIST_FILE = Runner/Info.plist; 417 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 418 | PRODUCT_BUNDLE_IDENTIFIER = com.example.example; 419 | PRODUCT_NAME = "$(TARGET_NAME)"; 420 | SWIFT_OBJC_BRIDGING_HEADER = "Runner/Runner-Bridging-Header.h"; 421 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 422 | SWIFT_VERSION = 5.0; 423 | VERSIONING_SYSTEM = "apple-generic"; 424 | }; 425 | name = Debug; 426 | }; 427 | 97C147071CF9000F007C117D /* Release */ = { 428 | isa = XCBuildConfiguration; 429 | baseConfigurationReference = 7AFA3C8E1D35360C0083082E /* Release.xcconfig */; 430 | buildSettings = { 431 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 432 | CLANG_ENABLE_MODULES = YES; 433 | CURRENT_PROJECT_VERSION = "$(FLUTTER_BUILD_NUMBER)"; 434 | ENABLE_BITCODE = NO; 435 | INFOPLIST_FILE = Runner/Info.plist; 436 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 437 | PRODUCT_BUNDLE_IDENTIFIER = com.example.example; 438 | PRODUCT_NAME = "$(TARGET_NAME)"; 439 | SWIFT_OBJC_BRIDGING_HEADER = "Runner/Runner-Bridging-Header.h"; 440 | SWIFT_VERSION = 5.0; 441 | VERSIONING_SYSTEM = "apple-generic"; 442 | }; 443 | name = Release; 444 | }; 445 | /* End XCBuildConfiguration section */ 446 | 447 | /* Begin XCConfigurationList section */ 448 | 97C146E91CF9000F007C117D /* Build configuration list for PBXProject "Runner" */ = { 449 | isa = XCConfigurationList; 450 | buildConfigurations = ( 451 | 97C147031CF9000F007C117D /* Debug */, 452 | 97C147041CF9000F007C117D /* Release */, 453 | 249021D3217E4FDB00AE95B9 /* Profile */, 454 | ); 455 | defaultConfigurationIsVisible = 0; 456 | defaultConfigurationName = Release; 457 | }; 458 | 97C147051CF9000F007C117D /* Build configuration list for PBXNativeTarget "Runner" */ = { 459 | isa = XCConfigurationList; 460 | buildConfigurations = ( 461 | 97C147061CF9000F007C117D /* Debug */, 462 | 97C147071CF9000F007C117D /* Release */, 463 | 249021D4217E4FDB00AE95B9 /* Profile */, 464 | ); 465 | defaultConfigurationIsVisible = 0; 466 | defaultConfigurationName = Release; 467 | }; 468 | /* End XCConfigurationList section */ 469 | }; 470 | rootObject = 97C146E61CF9000F007C117D /* Project object */; 471 | } 472 | -------------------------------------------------------------------------------- /test/i18next_test.dart: -------------------------------------------------------------------------------- 1 | import 'package:flutter/widgets.dart'; 2 | import 'package:flutter_test/flutter_test.dart'; 3 | import 'package:i18next/i18next.dart'; 4 | import 'package:mockito/annotations.dart'; 5 | import 'package:mockito/mockito.dart'; 6 | 7 | import 'i18next_localization_delegate_test.mocks.dart'; 8 | 9 | @GenerateMocks([ResourceStore, LocalizationDataSource]) 10 | void main() { 11 | const namespace = 'local_namespace'; 12 | const locale = Locale('en'); 13 | const defaultFormatter = I18NextOptions.defaultFormatter; 14 | 15 | late I18Next i18next; 16 | late MockResourceStore resourceStore; 17 | 18 | setUp(() { 19 | resourceStore = MockResourceStore(); 20 | i18next = I18Next(locale, resourceStore); 21 | when(resourceStore.retrieve(any, any, any, any)).thenReturn(null); 22 | }); 23 | 24 | void mockKey( 25 | String key, 26 | String answer, { 27 | String ns = namespace, 28 | Locale locale = locale, 29 | }) { 30 | when(resourceStore.retrieve(locale, ns, key, any)).thenReturn(answer); 31 | } 32 | 33 | group('given named namespaces', () { 34 | setUp(() { 35 | mockKey('key', 'My first value', ns: 'ns1'); 36 | mockKey('key', 'My second value', ns: 'ns2'); 37 | }); 38 | 39 | test('given key for matching namespaces', () { 40 | expect(i18next.t('ns1:key'), 'My first value'); 41 | verify(resourceStore.retrieve(locale, 'ns1', 'key', any)); 42 | 43 | expect(i18next.t('ns2:key'), 'My second value'); 44 | verify(resourceStore.retrieve(locale, 'ns2', 'key', any)); 45 | }); 46 | 47 | test('given key for unmatching namespaces', () { 48 | expect(i18next.t('ns3:key'), 'ns3:key'); 49 | verify(resourceStore.retrieve(locale, 'ns3', 'key', any)); 50 | }); 51 | 52 | test('given key for partially matching namespaces', () { 53 | expect(i18next.t('ns:key'), 'ns:key'); 54 | verify(resourceStore.retrieve(locale, 'ns', 'key', any)); 55 | }); 56 | }); 57 | 58 | test('given resource store', () { 59 | mockKey('key', 'My value', ns: 'ns'); 60 | 61 | expect(i18next.t('ns:key'), 'My value'); 62 | verify(resourceStore.retrieve(locale, 'ns', 'key', any)).called(1); 63 | }); 64 | 65 | test('given key without namespace', () { 66 | when(resourceStore.retrieve(any, any, any, any)).thenReturn(null); 67 | 68 | expect(i18next.t('someKey'), 'someKey'); 69 | expect(i18next.t('some.key'), 'some.key'); 70 | }); 71 | 72 | test('given an existing string key', () { 73 | mockKey('myKey', 'This is my key'); 74 | expect(i18next.t('$namespace:myKey'), 'This is my key'); 75 | }); 76 | 77 | test('given a non-existing or non matching key', () { 78 | expect(i18next.t('someKey'), 'someKey'); 79 | expect(i18next.t('some.key'), 'some.key'); 80 | }); 81 | 82 | test('given overriding locale', () { 83 | const anotherLocale = Locale('another'); 84 | mockKey('key', 'my value', locale: anotherLocale); 85 | 86 | expect(i18next.t('$namespace:key', locale: anotherLocale), 'my value'); 87 | verify(resourceStore.retrieve( 88 | anotherLocale, 89 | namespace, 90 | 'key', 91 | any, 92 | )).called(1); 93 | }); 94 | 95 | group('given formatter', () { 96 | test('with no interpolations', () { 97 | i18next = I18Next( 98 | locale, 99 | resourceStore, 100 | options: I18NextOptions( 101 | formatter: expectAsync3(defaultFormatter, count: 0), 102 | ), 103 | ); 104 | mockKey('key', 'no interpolations here'); 105 | 106 | expect(i18next.t('$namespace:key'), 'no interpolations here'); 107 | }); 108 | 109 | test('with no matching variables', () { 110 | i18next = I18Next( 111 | locale, 112 | resourceStore, 113 | options: I18NextOptions( 114 | formatter: expectAsync3( 115 | (value, format, locale) => value.toString(), 116 | count: 0, 117 | ), 118 | ), 119 | ); 120 | mockKey('key', 'leading {{value, format}} trailing'); 121 | 122 | expect( 123 | i18next.t('$namespace:key', variables: {'name': 'World'}), 124 | 'leading {{value, format}} trailing', 125 | ); 126 | }); 127 | 128 | test('with matching variables', () { 129 | i18next = I18Next( 130 | locale, 131 | resourceStore, 132 | options: I18NextOptions( 133 | formatter: expectAsync3((value, format, locale) => value.toString()), 134 | ), 135 | ); 136 | mockKey('key', 'leading {{value, format}} trailing'); 137 | 138 | expect( 139 | i18next.t('$namespace:key', variables: {'value': 'eulav'}), 140 | 'leading eulav trailing', 141 | ); 142 | }); 143 | 144 | test('with one matching interpolation', () { 145 | i18next = I18Next( 146 | locale, 147 | resourceStore, 148 | options: I18NextOptions( 149 | formatter: expectAsync3( 150 | (value, format, locale) { 151 | expect(value, 'eulav'); 152 | expect(format, 'format'); 153 | expect(locale, locale); 154 | return value.toString(); 155 | }, 156 | ), 157 | ), 158 | ); 159 | mockKey('key', 'leading {{value, format}} trailing'); 160 | 161 | expect( 162 | i18next.t('$namespace:key', variables: {'value': 'eulav'}), 163 | 'leading eulav trailing', 164 | ); 165 | }); 166 | 167 | test('with multiple matching interpolations', () { 168 | final values = []; 169 | final formats = []; 170 | i18next = I18Next( 171 | locale, 172 | resourceStore, 173 | options: I18NextOptions( 174 | formatter: expectAsync3( 175 | (value, format, locale) { 176 | values.add(value); 177 | formats.add(format); 178 | return value.toString(); 179 | }, 180 | count: 2, 181 | ), 182 | ), 183 | ); 184 | mockKey( 185 | 'key', 186 | 'leading {{value1, format1}} middle ' 187 | '{{value2, format2}} trailing'); 188 | 189 | expect( 190 | i18next.t('$namespace:key', variables: { 191 | 'value1': '1eulav', 192 | 'value2': '2eulav', 193 | }), 194 | 'leading 1eulav middle 2eulav trailing', 195 | ); 196 | expect(values, orderedEquals(['1eulav', '2eulav'])); 197 | expect(formats, orderedEquals(['format1', 'format2'])); 198 | }); 199 | }); 200 | 201 | group('fallback', () { 202 | test('given a global fallback key substitution', () { 203 | const fallbackNamespace1 = 'fallback_namespace_1'; 204 | i18next = I18Next( 205 | locale, 206 | resourceStore, 207 | options: const I18NextOptions( 208 | fallbackNamespaces: [fallbackNamespace1], 209 | ), 210 | ); 211 | 212 | mockKey('key', 'fallbackValue', ns: fallbackNamespace1); 213 | mockKey('key', 'value', ns: namespace); 214 | 215 | expect(i18next.t('key'), 'fallbackValue'); 216 | expect(i18next.t('$namespace:key'), 'value'); 217 | }); 218 | 219 | group('given 2 global fallback keys subsitution', () { 220 | const fallbackNamespace1 = 'fallback_namespace_1'; 221 | const fallbackNamespace2 = 'fallback_namespace_2'; 222 | 223 | setUp(() { 224 | i18next = I18Next( 225 | locale, 226 | resourceStore, 227 | options: const I18NextOptions( 228 | fallbackNamespaces: [fallbackNamespace1, fallbackNamespace2], 229 | ), 230 | ); 231 | }); 232 | 233 | test('key only exists in second fallbackNamespace', () { 234 | mockKey('key2', 'fallbackValue2', ns: fallbackNamespace2); 235 | mockKey('key2', 'value2', ns: namespace); 236 | 237 | expect(i18next.t('key2'), 'fallbackValue2'); 238 | expect(i18next.t('$namespace:key2'), 'value2'); 239 | }); 240 | 241 | test('key exists in both first and second fallbackNamespace', () { 242 | mockKey('key', 'fallbackValue1', ns: fallbackNamespace1); 243 | mockKey('key', 'fallbackValue2', ns: fallbackNamespace2); 244 | mockKey('key', 'value', ns: namespace); 245 | 246 | expect(i18next.t('key'), 'fallbackValue1'); 247 | expect(i18next.t('$namespace:key'), 'value'); 248 | }); 249 | }); 250 | }); 251 | 252 | group('pluralization', () { 253 | setUp(() { 254 | // English, which is "simple" (key and key_plural): 255 | mockKey('friend-no-count', 'A friend'); 256 | mockKey('friend-no-count_plural', 'Friends'); 257 | mockKey('friend', '{{count}} friend'); 258 | mockKey('friend_plural', '{{count}} friends'); 259 | 260 | // Icelandic, which is also "simple" but has a subtly different rule. 261 | // (In Icelandic, you say "twenty and one friend".) 262 | const ic = Locale('is'); 263 | mockKey('friend', '{{count}} vinur', locale: ic); 264 | mockKey('friend_plural', '{{count}} vinir', locale: ic); 265 | 266 | // Russian, which has three pluralization forms: 267 | const ru = Locale('ru'); 268 | mockKey('friend_0', '{{count}} друг', locale: ru); 269 | mockKey('friend_1', '{{count}} друга', locale: ru); 270 | mockKey('friend_2', '{{count}} друзей', locale: ru); 271 | 272 | // Japanese, which has none: 273 | const ja = Locale('ja'); 274 | mockKey('friend', '友達{{count}}人', locale: ja); 275 | }); 276 | 277 | test('given key without count', () { 278 | expect(i18next.t('$namespace:friend-no-count'), 'A friend'); 279 | }); 280 | 281 | test('given key with count', () { 282 | expect(i18next.t('$namespace:friend', count: 0), '0 friends'); 283 | expect(i18next.t('$namespace:friend', count: 1), '1 friend'); 284 | expect(i18next.t('$namespace:friend', count: 99), '99 friends'); 285 | }); 286 | 287 | test('given key with count in Icelandic (alternate plural rule)', () { 288 | const ic = Locale('is'); 289 | expect(i18next.t('$namespace:friend', count: 1, locale: ic), '1 vinur'); 290 | expect(i18next.t('$namespace:friend', count: 20, locale: ic), '20 vinir'); 291 | expect(i18next.t('$namespace:friend', count: 21, locale: ic), '21 vinur'); 292 | }); 293 | 294 | test('given key with count in Russian (multiple plurals)', () { 295 | const ru = Locale('ru'); 296 | expect(i18next.t('$namespace:friend', count: 1, locale: ru), '1 друг'); 297 | expect(i18next.t('$namespace:friend', count: 2, locale: ru), '2 друга'); 298 | expect(i18next.t('$namespace:friend', count: 9, locale: ru), '9 друзей'); 299 | }); 300 | 301 | test('given key with count in Japanese (no plurals)', () { 302 | const ja = Locale('ja'); 303 | expect(i18next.t('$namespace:friend', count: 1, locale: ja), '友達1人'); 304 | expect(i18next.t('$namespace:friend', count: 5, locale: ja), '友達5人'); 305 | }); 306 | 307 | test('given key with count in variables', () { 308 | expect( 309 | i18next.t('$namespace:friend', variables: {'count': 0}), 310 | '0 friends', 311 | ); 312 | expect( 313 | i18next.t('$namespace:friend', variables: {'count': 1}), 314 | '1 friend', 315 | ); 316 | expect( 317 | i18next.t('$namespace:friend', variables: {'count': -1}), 318 | '-1 friend', 319 | ); 320 | expect( 321 | i18next.t('$namespace:friend', variables: {'count': 99}), 322 | '99 friends', 323 | ); 324 | }); 325 | 326 | test('given key with both count property and in variables', () { 327 | expect( 328 | i18next.t('$namespace:friend', count: 0, variables: {'count': 1}), 329 | '0 friends', 330 | ); 331 | expect( 332 | i18next.t('$namespace:friend', count: 1, variables: {'count': 0}), 333 | '1 friend', 334 | ); 335 | }); 336 | 337 | test('given key with count and unmmaped context', () { 338 | expect( 339 | i18next.t('$namespace:friend', count: 1, context: 'something'), 340 | '1 friend', 341 | ); 342 | expect( 343 | i18next.t('$namespace:friend', count: 99, context: 'something'), 344 | '99 friends', 345 | ); 346 | }); 347 | 348 | // TODO: add special pluralization rules 349 | }); 350 | 351 | group('contextualization', () { 352 | setUp(() { 353 | mockKey('friend', 'A friend'); 354 | mockKey('friend_male', 'A boyfriend'); 355 | mockKey('friend_female', 'A girlfriend'); 356 | }); 357 | 358 | test('given key without context', () { 359 | expect(i18next.t('$namespace:friend'), 'A friend'); 360 | }); 361 | 362 | test('given key with mapped context', () { 363 | expect(i18next.t('$namespace:friend', context: 'male'), 'A boyfriend'); 364 | expect(i18next.t('$namespace:friend', context: 'female'), 'A girlfriend'); 365 | }); 366 | 367 | test('given key with mapped context in variables', () { 368 | expect( 369 | i18next.t('$namespace:friend', variables: {'context': 'male'}), 370 | 'A boyfriend', 371 | ); 372 | expect( 373 | i18next.t('$namespace:friend', variables: {'context': 'female'}), 374 | 'A girlfriend', 375 | ); 376 | }); 377 | 378 | test('given key with both mapped context property and in variables', () { 379 | expect( 380 | i18next.t( 381 | '$namespace:friend', 382 | context: 'female', 383 | variables: {'context': 'male'}, 384 | ), 385 | 'A girlfriend', 386 | ); 387 | expect( 388 | i18next.t( 389 | '$namespace:friend', 390 | context: 'male', 391 | variables: {'context': 'female'}, 392 | ), 393 | 'A boyfriend', 394 | ); 395 | }); 396 | 397 | test('given key with unmaped context', () { 398 | expect(i18next.t('$namespace:friend', context: 'other'), 'A friend'); 399 | }); 400 | 401 | test('given key with mapped context and count', () { 402 | expect( 403 | i18next.t('$namespace:friend', context: 'male', count: 0), 404 | 'A boyfriend', 405 | ); 406 | expect( 407 | i18next.t('$namespace:friend', context: 'male', count: 1), 408 | 'A boyfriend', 409 | ); 410 | }); 411 | 412 | test('given key with unmapped context and count', () { 413 | expect( 414 | i18next.t('$namespace:friend', context: 'other', count: 1), 415 | 'A friend', 416 | ); 417 | expect( 418 | i18next.t('$namespace:friend', context: 'other', count: 99), 419 | 'A friend', 420 | ); 421 | }); 422 | }); 423 | 424 | group('contextualization and pluralization', () { 425 | setUp(() { 426 | mockKey('friend', 'A friend'); 427 | mockKey('friend_plural', '{{count}} friends'); 428 | mockKey('friend_male', 'A boyfriend'); 429 | mockKey('friend_male_plural', '{{count}} boyfriends'); 430 | mockKey('friend_female', 'A girlfriend'); 431 | mockKey('friend_female_plural', '{{count}} girlfriends'); 432 | }); 433 | 434 | test('given key with mapped context and count', () { 435 | expect( 436 | i18next.t('$namespace:friend', context: 'male', count: 0), 437 | '0 boyfriends', 438 | ); 439 | expect( 440 | i18next.t('$namespace:friend', context: 'male', count: 1), 441 | 'A boyfriend', 442 | ); 443 | expect( 444 | i18next.t('$namespace:friend', context: 'female', count: 0), 445 | '0 girlfriends', 446 | ); 447 | expect( 448 | i18next.t('$namespace:friend', context: 'female', count: 1), 449 | 'A girlfriend', 450 | ); 451 | }); 452 | 453 | test('given key with unmmaped context and count', () { 454 | expect( 455 | i18next.t('$namespace:friend', context: 'other', count: 0), 456 | '0 friends', 457 | ); 458 | expect( 459 | i18next.t('$namespace:friend', context: 'other', count: 1), 460 | 'A friend', 461 | ); 462 | }); 463 | }); 464 | 465 | group('interpolation', () { 466 | setUp(() { 467 | mockKey('key', '{{first}}, {{second}}, and then {{third}}!'); 468 | }); 469 | 470 | test('given empty interpolation', () { 471 | mockKey('key', 'This is some {{}}'); 472 | expect(i18next.t('$namespace:key'), 'This is some {{}}'); 473 | }); 474 | 475 | test('given non matching arguments', () { 476 | expect( 477 | i18next.t('$namespace:key', variables: {'none': 'none'}), 478 | '{{first}}, {{second}}, and then {{third}}!', 479 | ); 480 | }); 481 | 482 | test('given partially matching arguments', () { 483 | expect( 484 | i18next.t('$namespace:key', variables: {'first': 'fst'}), 485 | 'fst, {{second}}, and then {{third}}!', 486 | ); 487 | expect( 488 | i18next.t( 489 | '$namespace:key', 490 | variables: {'first': 'fst', 'third': 'trd'}, 491 | ), 492 | 'fst, {{second}}, and then trd!', 493 | ); 494 | }); 495 | 496 | test('given all matching arguments', () { 497 | expect( 498 | i18next.t('$namespace:key', variables: { 499 | 'first': 'fst', 500 | 'second': 'snd', 501 | 'third': 'trd', 502 | }), 503 | 'fst, snd, and then trd!', 504 | ); 505 | }); 506 | 507 | test('given extra matching arguments', () { 508 | expect( 509 | i18next.t('$namespace:key', variables: { 510 | 'first': 'fst', 511 | 'second': 'snd', 512 | 'third': 'trd', 513 | 'none': 'none', 514 | }), 515 | 'fst, snd, and then trd!', 516 | ); 517 | }); 518 | }); 519 | 520 | group('nesting', () { 521 | test('when nested key is not found', () { 522 | mockKey('key', r'This is my $t(anotherKey)'); 523 | 524 | expect(i18next.t('$namespace:key'), r'This is my $t(anotherKey)'); 525 | }); 526 | 527 | test('given multiple simple key substitutions', () { 528 | mockKey('nesting1', r'1 $t(nesting2)'); 529 | mockKey('nesting2', r'2 $t(nesting3)'); 530 | mockKey('nesting3', '3'); 531 | 532 | expect(i18next.t('$namespace:nesting1'), '1 2 3'); 533 | }); 534 | 535 | test('given a grouped key substitution', () { 536 | mockKey('keyA', 'A'); 537 | mockKey('group.keyB', 'B'); 538 | mockKey('local', r'$t(keyA), and $t(group.keyB)!'); 539 | 540 | expect(i18next.t('$namespace:local'), 'A, and B!'); 541 | }); 542 | 543 | test('given a global fallback key substitution', () { 544 | const fallbackNamespace = 'fallback_namespace'; 545 | i18next = I18Next( 546 | locale, 547 | resourceStore, 548 | options: const I18NextOptions(fallbackNamespaces: [fallbackNamespace]), 549 | ); 550 | 551 | mockKey('keyZ', 'Z', ns: fallbackNamespace); 552 | mockKey('keyA', 'A', ns: namespace); 553 | 554 | mockKey('example', r'$t(keyA), and $t(keyZ)!', ns: namespace); 555 | 556 | expect(i18next.t('$namespace:example'), 'A, and Z!'); 557 | }); 558 | 559 | test('when nested local and fallback namespaces have same key', () { 560 | const fallbackNamespace = 'fallback_namespace'; 561 | i18next = I18Next( 562 | locale, 563 | resourceStore, 564 | options: const I18NextOptions(fallbackNamespaces: [fallbackNamespace]), 565 | ); 566 | 567 | mockKey('keyX', 'Global X', ns: fallbackNamespace); 568 | mockKey('keyX', 'Local X', ns: namespace); 569 | mockKey( 570 | 'example', 571 | // explicit namespace key nesting 572 | '\$t(keyX), and \$t($fallbackNamespace:keyX)!', 573 | ns: namespace, 574 | ); 575 | 576 | expect(i18next.t('$namespace:example'), 'Local X, and Global X!'); 577 | }); 578 | 579 | test('interpolation from immediate variables', () { 580 | mockKey('key1', 'hello world'); 581 | mockKey('key2', 'say: {{val}}'); 582 | 583 | expect( 584 | i18next.t('$namespace:key2', variables: {'val': r'$t(key1)'}), 585 | 'say: hello world', 586 | ); 587 | }); 588 | 589 | test('nested interpolations', () { 590 | mockKey('key1', 'hello {{name}}'); 591 | mockKey('key2', r'say: $t(key1)'); 592 | 593 | expect( 594 | i18next.t('$namespace:key2', variables: {'name': 'world'}), 595 | 'say: hello world', 596 | ); 597 | }); 598 | 599 | test('nested pluralization and interpolation ', () { 600 | mockKey('girlsAndBoys', 601 | r'$t(girls, {"count": {{girls}} }) and {{count}} boy'); 602 | mockKey('girlsAndBoys_plural', 603 | r'$t(girls, {"count": {{girls}} }) and {{count}} boys'); 604 | mockKey('girls', '{{count}} girl'); 605 | mockKey('girls_plural', '{{count}} girls'); 606 | 607 | expect( 608 | i18next.t('$namespace:girlsAndBoys', count: 2, variables: {'girls': 3}), 609 | '3 girls and 2 boys', 610 | ); 611 | }); 612 | }); 613 | 614 | group('.of', () { 615 | BuildContext? capturedContext; 616 | 617 | final builder = Builder(builder: (context) { 618 | capturedContext = context; 619 | return Container(); 620 | }); 621 | 622 | setUp(() { 623 | capturedContext = null; 624 | }); 625 | 626 | testWidgets('when not registered in the widget tree', (tester) async { 627 | await tester.pumpWidget(builder); 628 | expect(I18Next.of(capturedContext!), isNull); 629 | }); 630 | 631 | testWidgets('when is registered in the widget tree', (tester) async { 632 | final dataSource = MockLocalizationDataSource(); 633 | when(dataSource.load(any)).thenAnswer((_) async => {}); 634 | 635 | await tester.pumpWidget(Localizations( 636 | locale: locale, 637 | delegates: [ 638 | DefaultWidgetsLocalizations.delegate, 639 | I18NextLocalizationDelegate( 640 | locales: [locale], 641 | dataSource: dataSource, 642 | ), 643 | ], 644 | child: builder, 645 | )); 646 | await tester.pump(); 647 | expect(I18Next.of(capturedContext!), isNotNull); 648 | }); 649 | }); 650 | } 651 | --------------------------------------------------------------------------------