### Add Real Project Example Source: https://github.com/slang-i18n/slang/blob/main/slang/CHANGELOG_OLD.md Includes a real project example in the documentation for better understanding. ```dart // Add real project example ``` -------------------------------- ### Namespace File Structure Example 1 Source: https://github.com/slang-i18n/slang/blob/main/slang/README.md Example directory structure for organizing translations into namespaces. ```text i18n/ └── widgets_en.i18n.json └── widgets_fr.i18n.json └── errorDialogs_en.i18n.json <-- camel case for multiple words └── errorDialogs_fr.i18n.json ``` -------------------------------- ### Wildcard Locale File Structure Example Source: https://github.com/slang-i18n/slang/blob/main/slang/README.md Example directory structure demonstrating the use of wildcard locales. ```text i18n/ └── de.json └── en.json └── fr.json └── [de,fr,ja].json └── [any]-FR.json ``` -------------------------------- ### Namespace File Structure Example 2 (Nested Folders) Source: https://github.com/slang-i18n/slang/blob/main/slang/README.md Example directory structure using nested folders for namespaces. ```text i18n/ └── widgets/ └── widgets_en.i18n.json └── widgets_fr.i18n.json └── errorDialogs/ └── errorDialogs_en.i18n.json └── errorDialogs_fr.i18n.json ``` -------------------------------- ### ARB Input Example Source: https://github.com/slang-i18n/slang/blob/main/README.md Example of an ARB file input for migration, including locale, context, and pluralization. ```json { "@@locale": "en_US", "@@context": "HomePage", "title_bar": "My Cool Home", "@title_bar": { "type": "text", "context": "HomePage", "description": "Page title." }, "FOO_123": "Your pending cost is {COST}", "foo456": "Hello {0}", "pageHomeInboxCount" : "{count, plural, zero{You have no new messages} one{You have 1 new message} other{You have {count} new messages}}", "@pageHomeInboxCount" : { "placeholders": { "count": {} } } } ``` -------------------------------- ### YAML File Example Source: https://github.com/slang-i18n/slang/blob/main/slang/README.md Example of a translation file in YAML format, which supports multiline strings and comments. ```yaml welcome: title: Welcome $name # some comment ``` -------------------------------- ### Namespace File Structure Example 3 (Directory Locales) Source: https://github.com/slang-i18n/slang/blob/main/slang/README.md Example directory structure using directory locales where the directory name indicates the locale. ```text i18n/ └── en/ └── widgets_en.i18n.json └── errorDialogs_en.i18n.json └── fr/ └── widgets_fr.i18n.json └── errorDialogs.i18n.json <-- directory locale will be used ``` -------------------------------- ### English Translation Example Source: https://github.com/slang-i18n/slang/blob/main/slang/README.md Example of an English translation file. ```json5 // English { "hello": "Hello", "bye": "Bye" } ``` -------------------------------- ### Recasing Example Source: https://github.com/slang-i18n/slang/blob/main/slang/README.md Example demonstrating recasing of keys and parameters in JSON and corresponding Dart code. ```json { "must_be_camel_case": "The parameter is in {snakeCase}", "my_map(map)": { "this_should_be_in_pascal": "hi" } } ``` -------------------------------- ### Install Slang Package from Source Source: https://github.com/slang-i18n/slang/blob/main/slang_mcp/README.md Command to install the Slang package globally after cloning its repository. This is useful for development or when needing the latest unreleased version. ```bash dart pub global activate --source path . ``` -------------------------------- ### JSON Translation File Example (German) Source: https://github.com/slang-i18n/slang/blob/main/slang/README.md Example of a JSON translation file for the German locale, demonstrating different translations for the same keys. ```json // File: de.i18n.json { "hello": "Hallo $name", "save": "Speichern", "login": { "success": "Login erfolgreich", "fail": "Login fehlgeschlagen" } } ``` -------------------------------- ### JSON File Example Source: https://github.com/slang-i18n/slang/blob/main/slang/README.md Example of a translation file in JSON format, supporting string interpolation. ```json { "welcome": { "title": "Welcome $name" } } ``` -------------------------------- ### Wildcard Locale Spreading Example Source: https://github.com/slang-i18n/slang/blob/main/README.md Demonstrates how wildcard locales spread to specific locale files. ```text This will spread to the following locales: | Locale | Source | |---------|------------------------------| | `de` | `de.json`, `[de,fr,ja].json` | | `en` | `en.json` | | `fr` | `fr.json`, `[de,fr,ja].json` | | `ja` | `[de,fr,ja].json` | | `de-FR` | `[any]-FR.json` | | `en-FR` | `[any]-FR.json` | | `fr-FR` | `[any]-FR.json` | | `ja-FR` | `[any]-FR.json` | ``` -------------------------------- ### Wildcard Locale Syntax Examples Source: https://github.com/slang-i18n/slang/blob/main/README.md Illustrates the syntax for wildcard locales to spread translations across multiple locales. ```text [de,en,fr-FR] # Spreads to the specified codes [any] # Spreads to all existing codes in that position ``` -------------------------------- ### Compact CSV Format Example Source: https://github.com/slang-i18n/slang/blob/main/slang/README.md Example of a compact CSV file containing translations for multiple locales. ```csv key,en,de-DE welcome.title,Welcome $name,Willkommen $name welcome.button,Start,Start ``` -------------------------------- ### Namespace File Structure Example 4 (Deeply Nested) Source: https://github.com/slang-i18n/slang/blob/main/slang/README.md Example of a deeply nested directory structure for organizing translations with directory locales. ```text i18n/ └── en/ └── widgets/ └── welcomeCard.json └── profileCard.json └── errorDialogs/ └── login.json └── network.json ``` -------------------------------- ### Recommended Setup with TranslationProvider Source: https://github.com/slang-i18n/slang/blob/main/slang/CHANGELOG_OLD.md Shows the recommended way to set up the TranslationProvider for your Flutter application to manage internationalization. ```dart void main() { WidgetsFlutterBinding.ensureInitialized(); // LocaleSettings.useDeviceLocale(); runApp(TranslationProvider(child: MyApp())); // Wrap your app with TranslationProvider } ``` ```dart MaterialApp( locale: TranslationProvider.of(context).flutterLocale, // use provider supportedLocales: LocaleSettings.supportedLocales, localizationsDelegates: GlobalMaterialLocalizations.delegates, ) ``` -------------------------------- ### Default Namespace File Structure Source: https://github.com/slang-i18n/slang/blob/main/slang/README.md Example file structure including the `_default` namespace for root translations. ```text i18n/ └── _default_en.i18n.json └── _default_fr.i18n.json └── widgets_en.i18n.json └── widgets_fr.i18n.json ``` -------------------------------- ### ARB File Example Source: https://github.com/slang-i18n/slang/blob/main/slang/README.md Example of a translation file in ARB format, commonly used in Flutter projects. It supports placeholders and pluralization. ```json { "@@locale": "en", "welcomeTitle": "Welcome {name}", "@welcomeTitle": { "placeholders": { "name": {} } }, "inboxPageCount": "You have {count, plural, one {1 message} other {{count} messages}}", "@inboxPageCount": { "description": "The number of messages in the user's inbox", "placeholders": { "count": { "type": "int" } } } } ``` -------------------------------- ### French Translation Example with Fallback Source: https://github.com/slang-i18n/slang/blob/main/slang/README.md Example of a French translation file where a missing key falls back to English. ```json5 // French { "hello": "Salut", // "bye" is missing, fallback to English version } ``` -------------------------------- ### Riverpod Dependency Injection Example Source: https://github.com/slang-i18n/slang/blob/main/slang/CHANGELOG_OLD.md Demonstrates how to integrate slang-i18n with Riverpod for dependency injection, allowing custom plural resolvers and access to translation instances. ```dart final english = AppLocale.en.build(cardinalResolver: myEnResolver); final german = AppLocale.de.build(cardinalResolver: myDeResolver); final translationProvider = StateProvider((ref) => german); // access the current instance final t = ref.watch(translationProvider); String a = t.welcome.title; ``` -------------------------------- ### JSON Result Example after ARB Migration Source: https://github.com/slang-i18n/slang/blob/main/README.md Example of the JSON output after migrating from an ARB file, showing transformed keys and structure. ```json { "@@locale": "en_US", "@@context": "HomePage", "title": { "bar": "My Cool Home", "@bar": "Page title." }, "foo123": "Your pending cost is {cost}", "foo456": "Hello {arg0}", "page": { "home": { "inbox": { "count(param=count)": { "zero": "You have no new messages", "one": "You have 1 new message", "other": "You have {count} new messages" } } } } } ``` -------------------------------- ### LLM Prompt for Applying WIP Strings Source: https://github.com/slang-i18n/slang/blob/main/slang_mcp/README.md Example prompt to send to an LLM (like the MCP server) to apply and translate new WIP strings discovered in the codebase. This automates the process of updating translation files. ```text Apply my new WIP strings and translate ``` -------------------------------- ### Sanitized Key Example Source: https://github.com/slang-i18n/slang/blob/main/slang/README.md Example of a JSON key that is a reserved word and how it is sanitized in Dart. ```json { "continue": "Continue" } ``` -------------------------------- ### Install slang_mcp Globally Source: https://github.com/slang-i18n/slang/blob/main/slang_mcp/README.md Install the slang_mcp package globally using dart pub. This command makes the slang_mcp executable available on your system. ```bash dart pub global activate slang_mcp ``` -------------------------------- ### Run the build_runner command Source: https://github.com/slang-i18n/slang/blob/main/slang_build_runner/README.md Execute the build_runner build command to generate code using the configured slang setup. The --delete-conflicting-outputs flag is recommended to avoid issues with previous builds. ```text dart run build_runner build --delete-conflicting-outputs ``` -------------------------------- ### Update README Source: https://github.com/slang-i18n/slang/blob/main/slang/CHANGELOG_OLD.md Updates to the README file, including documentation and code examples. ```markdown See README. ``` -------------------------------- ### JSON Translation File Example Source: https://github.com/slang-i18n/slang/blob/main/slang/README.md Example of a JSON translation file for the English locale. Slang supports JSON, JSON5, and YAML formats. File names should follow the pattern .. ```json // File: en.i18n.json { "hello": "Hello $name", "save": "Save", "login": { "success": "Logged in successfully", "fail": "Logged in failed" } } ``` -------------------------------- ### Configure build.yaml for Fast i18n Source: https://github.com/slang-i18n/slang/blob/main/slang/CHANGELOG_OLD.md Example configuration for the build.yaml file to customize i18n generation. Specifies base locale, input/output directories, file patterns, and key casing. ```yaml targets: $default: builders: fast_i18n:i18nBuilder: options: base_locale: en input_directory: lib/i18n input_file_pattern: .i18n.json output_directory: lib/i18n output_file_pattern: .g.dart key_case: snake maps: - a - b - c.d ``` -------------------------------- ### CSV File Example Source: https://github.com/slang-i18n/slang/blob/main/slang/README.md Example of a translation file in CSV format. This format can also be used to combine multiple locales into a single file. ```csv welcome.title,Welcome $name pages.0.title,First Page pages.1.title,Second Page ``` -------------------------------- ### Interface Configuration via Paths Source: https://github.com/slang-i18n/slang/blob/main/slang/documentation/interfaces.md Example of configuring interfaces in YAML by specifying paths. This allows targeting specific nodes or containers for interface definition. ```yaml interfaces: MyInterface: paths: - first.* - second.* ``` -------------------------------- ### RichText Translation Example Source: https://github.com/slang-i18n/slang/blob/main/slang/README.md Example of a translation string that includes rich text formatting, allowing for styled spans and dynamic content. ```json { "myText(rich)": "Welcome $name. Please click ${tapHere(here)}!" } ``` -------------------------------- ### Specify Source Directories for Slang Configure Source: https://github.com/slang-i18n/slang/blob/main/slang/README.md Example of using the `--source-dirs` argument with the `slang configure` command to specify directories where translation resources are located. ```sh dart run slang configure --source-dirs=dir1,dir2 ``` -------------------------------- ### Generated Translation File after WIP Apply Source: https://github.com/slang-i18n/slang/blob/main/slang/README.md Example of a translation file updated automatically after applying WIP changes. ```json5 { "welcome": { "name": "Hello $name" // added automatically } } ``` -------------------------------- ### Configure Key Case and Paths Source: https://github.com/slang-i18n/slang/blob/main/slang/MIGRATION.md When using `key_case` and path-related features like `maps`, ensure paths are cased according to `key_case`. This example uses `camel` case for keys. ```json { "a_b": { "hi": "hi" } } ``` ```yaml targets: $default: builders: fast_i18n: options: key_case: camel # this forces every other path also be in camel case maps: - aB ``` -------------------------------- ### List Translation Example Source: https://github.com/slang-i18n/slang/blob/main/slang/README.md Example of a translation structure containing nested lists and maps. Lists are fully supported without extra configuration. ```json { "niceList": [ "hello", "nice", [ "first item in nested list", "second item in nested list" ], { "wow": "WOW!", "ok": "OK!" }, { "a map entry": "access via key", "another entry": "access via second key" } ] } ``` -------------------------------- ### JSON Structure with List of Interfaces Source: https://github.com/slang-i18n/slang/blob/main/slang/documentation/interfaces.md Example JSON demonstrating a structure containing a list of items, where each item conforms to an interface. This showcases how interfaces apply to collections. ```json { "onboarding": { "firstPage": { "title": "hi", "content": "hi" }, "pages": [ { "title": "hi", "content": "hi" }, { "title": "hi", "content": "hi" }, { "title": "hi", "content": "hi" } ] } } ``` -------------------------------- ### Wildcard Locale Spreading Example Source: https://github.com/slang-i18n/slang/blob/main/slang/README.md Table illustrating how wildcard locales spread to specific locale codes. ```text Locale | Source | de | de.json, [de,fr,ja].json | en | en.json | fr | fr.json, [de,fr,ja].json | ja | [de,fr,ja].json | de-FR | [any]-FR.json | en-FR | [any]-FR.json | fr-FR | [any]-FR.json | ja-FR | [any]-FR.json ``` -------------------------------- ### Run slang_gpt with full translation Source: https://github.com/slang-i18n/slang/blob/main/slang_gpt/README.md Use the `--full` flag to translate all keys, not just missing ones. This ensures all translations are generated by GPT, which can be useful for initial setup or complete overhauls. ```bash dart run slang_gpt --target=fr --full --api-key= ``` -------------------------------- ### Map Translation Example Source: https://github.com/slang-i18n/slang/blob/main/slang/README.md Example of translation data structured as maps, using the '(map)' modifier for specific keys. This allows for string-based key access. ```json5 // File: en.json { "a(map)": { "hello world": "hello" }, "b": { "b0": "hey", "b1(map)": { "hi there": "hi" } } } ``` -------------------------------- ### Custom Context Example Source: https://github.com/slang-i18n/slang/blob/main/slang/CHANGELOG_OLD.md Demonstrates how to use custom contexts for translations. Ensure the context is correctly defined and passed during translation. ```json { "greet": { "male": "Hello Mr $name", "female": "Hello Ms $name" } } ``` ```dart String a = t.greet(name: 'Anna', context: GenderContext.female); ``` -------------------------------- ### Slang Translation Analysis Result Example Source: https://github.com/slang-i18n/slang/blob/main/slang/README.md An example JSON output from the `slang analyze` command, showing the structure of analysis results for different locales, including missing translations. ```json5 { "de": { "mainScreen": { "login": "This translation is missing, showing base translation here" } }, "fr": {} // everything ok } ``` -------------------------------- ### Generated Dart Code after WIP Apply Source: https://github.com/slang-i18n/slang/blob/main/slang/README.md Example of Dart code updated automatically after applying WIP changes. ```dart t.welcome.name(name: name); // updated automatically ``` -------------------------------- ### Override Translations Dynamically Source: https://github.com/slang-i18n/slang/blob/main/slang/README.md Configure Slang to allow dynamic overriding of translations, for example, by fetching them from a backend server. This example shows how to override translations for a specific locale and file type. ```yaml # Config translation_overrides: true ``` -------------------------------- ### Riverpod Integration for Translations Source: https://github.com/slang-i18n/slang/blob/main/slang/README.md Example of integrating Slang translations with the Riverpod state management library, demonstrating how to build locale-specific translation instances and provide them. ```dart final english = AppLocale.en.buildSync(); final german = AppLocale.de.buildSync(); final translationProvider = StateProvider((ref) => german); // set it // access the current instance final t = ref.watch(translationProvider); String a = t.welcome.title; // get translation AppLocale locale = t.$meta.locale; // get locale ``` -------------------------------- ### Generated Dart Code with Comments Source: https://github.com/slang-i18n/slang/blob/main/slang/README.md Example of Dart code generated with documentation comments derived from translation files. ```dart /// The submit button shown at the bottom String get button => 'Submit'; ``` -------------------------------- ### Pluralization Detection Example Source: https://github.com/slang-i18n/slang/blob/main/slang/README.md Shows how Slang detects pluralization in JSON. If an unknown key like 'three' is present, the node is not considered a pluralization. ```json5 { "fake": { "one": "One apple", "two": "Two apples", "three": "Three apples" // unknown key word 'three', 'fake' is not a pluralization } } ``` -------------------------------- ### Dynamic Key Access Example Source: https://github.com/slang-i18n/slang/blob/main/slang/README.md Access translations using a flattened, one-dimensional map structure with dot notation. This feature is enabled by default and can be disabled globally. ```dart String a = t['myPath.anotherPath']; String b = t['myPath.anotherPath.3']; // with index for arrays String c = t['myPath.anotherPath'](name: 'Tom'); // with arguments ``` -------------------------------- ### Namespace File Naming Convention Source: https://github.com/slang-i18n/slang/blob/main/slang/README.md Illustrates the file naming convention for namespaces, locale, and extension. ```text _. ``` -------------------------------- ### Get Translation Statistics Source: https://github.com/slang-i18n/slang/blob/main/README.md Run the 'stats' command to quickly obtain statistics about your translations, such as the number of keys, translations, words, and characters. ```sh dart run slang stats ``` -------------------------------- ### Correct CSV Parsing Example Source: https://github.com/slang-i18n/slang/blob/main/slang/README.md Demonstrates the correct way to format translated EOL characters in CSV files. Ensure translated EOL is written as '\n'. ```csv my.path,hello\nworld ``` -------------------------------- ### Dart Obfuscated String Example Source: https://github.com/slang-i18n/slang/blob/main/slang/README.md Illustrates how Dart strings are obfuscated by Slang, replacing plain text with a method call that includes obfuscated character codes. ```dart String get hello => _root.$meta.d([104, 69, 76, 76, 79]); ``` -------------------------------- ### Initialize Slang: Use Specific Locale Source: https://github.com/slang-i18n/slang/blob/main/slang/README.md Initialize Slang to use a specific locale, for example, by loading a stored preference. This method allows manual control over the locale. ```dart @override void initState() { super.initState(); String storedLocale = loadFromStorage(); // your logic here LocaleSettings.setLocaleRaw(storedLocale); } ``` -------------------------------- ### Wrap TranslationProvider Widgets for Auto Rebuild Source: https://github.com/slang-i18n/slang/blob/main/slang/README.md To ensure automatic rebuilds on locale changes across multiple packages, wrap all generated `TranslationProvider` widgets. This example shows how to nest them for `MaterialApp`. ```dart import 'package:my_package1/gen/strings.g.dart' as package1; import 'package:my_package2/gen/strings.g.dart' as package2; void main() { final widget = package1.TranslationProvider( child: package2.TranslationProvider( child: MaterialApp( home: Scaffold( body: Builder(builder: (context) { final t1 = package1.Translations.of(context); final t2 = package2.Translations.of(context); return Column( children: [ Text(t1.title), Text(t2.title), ], ); }), floatingActionButton: FloatingActionButton( onPressed: () { // this changes the locale for all packages // does not matter which package you call it on package1.LocaleSettings.setLocale(AppLocale.en); }, ), ), ), ), ); runApp(widget); } ``` -------------------------------- ### Example Translation Notes Source: https://github.com/slang-i18n/slang/blob/main/slang_mcp/README.md Provides context to the LLM for translation tasks. Notes can include general guidelines, tone, and specific word definitions. This JSON structure is automatically included when calling get-missing-translations. ```json { "@@notes": { "general": [ "The app is a banking app, so use financial terms.", "The tone should be formal and professional." ], "dictionary": { "bank": "Bank (financial institution, not 'Ufer')" } }, "welcome": "Willkommen" } ``` -------------------------------- ### Configure build.yaml for slang_build_runner Source: https://github.com/slang-i18n/slang/blob/main/slang_build_runner/README.md Set up the build.yaml file to configure the slang_build_runner. Specify the base locale for your translations. This file replaces the need for slang.yaml. ```yaml # build.yaml targets: $default: builders: slang_build_runner: options: base_locale: en ``` -------------------------------- ### Example of Linked Translations Source: https://github.com/slang-i18n/slang/blob/main/slang/MIGRATION.md Messages containing `@:` are interpreted as linked translations. This example shows how to link to a value within the `meta` object. ```json { "meta": { "appName": "My App" }, "welcome": "Welcome to @:meta.appName" } ``` -------------------------------- ### Prototyping UI with WIP Translations Source: https://github.com/slang-i18n/slang/blob/main/slang_mcp/README.md Demonstrates how to use Slang's $wip feature to embed new translation keys directly within Dart UI code. This allows for rapid feature development before finalizing translation files. ```dart class LoginPage extends StatelessWidget { @override Widget build(BuildContext context) { return Scaffold( body: Column( children: [ TextButton( onPressed: () {}, child: Text(context.t.$wip.loginPage.loginButton('Login')), ), TextButton( onPressed: () {}, child: Text(context.t.$wip.loginPage.forgotPasswordButton('Forgot Password')), ), ], ), ); } } ``` -------------------------------- ### Configure Input and Output Directories in slang.yaml Source: https://github.com/slang-i18n/slang/blob/main/slang/README.md Alternatively, configure the input and output directories for Slang assets directly in your slang.yaml file. This provides a centralized location for Slang-specific build configurations. ```yaml input_directory: assets/i18n output_directory: lib/i18n # defaulting to lib/gen if input is outside of lib/ ``` -------------------------------- ### Configure Input and Output Directories in build.yaml Source: https://github.com/slang-i18n/slang/blob/main/slang/README.md Specify the input and output directories for Slang assets within your build.yaml file. This ensures that your i18n files are processed correctly. ```yaml targets: $default: sources: - "custom-directory/**" # optional; only assets/* and lib/* are scanned by build_runner builders: slang_build_runner: options: input_directory: assets/i18n output_directory: lib/i18n # defaulting to lib/gen if input is outside of lib/ ``` -------------------------------- ### Configure slang_gpt in build.yaml or slang.yaml Source: https://github.com/slang-i18n/slang/blob/main/slang_gpt/README.md Add the 'gpt' configuration block to your build.yaml or slang.yaml file, specifying the model and app description. Ensure base_locale, fallback_strategy, input_directory, input_file_pattern, and output_directory are also configured. ```yaml existing config base_locale: fr fallback_strategy: base_locale input_directory: lib/i18n input_file_pattern: .i18n.json output_directory: lib/i18n # add this gpt: model: gpt-4 description: | "River Adventure" is a game where you need to cross a river by jumping on stones. The game is over when you either fall into the water or reach the other side. ``` -------------------------------- ### Prototyping with $wip and Path Source: https://github.com/slang-i18n/slang/blob/main/slang/README.md Extend the `$wip` function with a path for more structured prototyping. ```dart t.$wip.welcome.name('Hello $name'); // still returns the same ``` -------------------------------- ### Typed Locale Settings Source: https://github.com/slang-i18n/slang/blob/main/slang/CHANGELOG_OLD.md Utilize type-safe functions for setting and getting the current locale. ```dart LocaleSettings.setLocaleTyped(AppLocale.en); ``` ```dart LocaleSettings.currentLocaleTyped ``` -------------------------------- ### Using Custom Context in Dart Source: https://github.com/slang-i18n/slang/blob/main/slang/README.md Example of how to use the custom context enum in Dart code. ```dart String a = t.greet(name: 'Maria', context: GenderContext.female); ``` -------------------------------- ### Get Supported Locales Source: https://github.com/slang-i18n/slang/blob/main/slang/CHANGELOG_OLD.md Access the list of supported locales configured for the application using LocaleSettings.locales. ```dart LocaleSettings.locales ``` -------------------------------- ### Enable Namespaces Configuration Source: https://github.com/slang-i18n/slang/blob/main/README.md Enable the namespaces feature and specify output settings in the configuration. ```yaml namespaces: true # enable this feature output_directory: lib/i18n # optional output_file_name: translations.g.dart # set file name (mandatory) ``` -------------------------------- ### Prototyping with $wip Source: https://github.com/slang-i18n/slang/blob/main/slang/README.md Use the `$wip` function for rapid prototyping without defining keys or running code generation. It works with hot reload. ```dart String name = 'Tom'; t.$wip('Hello $name'); // => e.g. 'Hello Tom' ``` -------------------------------- ### Input Directory and File Pattern Configuration Source: https://github.com/slang-i18n/slang/blob/main/slang/README.md Configuration for specifying the input directory and file pattern for translation files. ```yaml input_directory: assets/i18n input_file_pattern: .json ``` -------------------------------- ### Slang WIP Apply Command Arguments Source: https://github.com/slang-i18n/slang/blob/main/slang/README.md Details the arguments for the `slang wip apply` command, specifically the `--source-dirs` option to specify directories for searching translation resources. ```text | Argument | Usage | |------------------------|---------------------------------------------------------| | `--source-dirs=` | Comma-separated list of source directories to search in | ``` -------------------------------- ### Outdated Translation JSON Structure Source: https://github.com/slang-i18n/slang/blob/main/README.md Example JSON structure showing how an outdated translation is represented with the '(OUTDATED)' modifier. ```json5 { "a": { "b": { "c(OUTDATED)": "This translation is outdated" } } } ``` -------------------------------- ### Initialize Slang: Dependency Injection Source: https://github.com/slang-i18n/slang/blob/main/slang/README.md Initialize Slang using dependency injection, allowing you to manually build and manage locale instances. This provides full control over locale handling. ```dart final english = AppLocale.en.build(); final german = AppLocale.de.build(); // read String a = german.login.success; ``` -------------------------------- ### Recasing with Path Configuration Source: https://github.com/slang-i18n/slang/blob/main/slang/README.md Configure key casing for paths within the configuration file. ```yaml # Config key_case: camel maps: - myMap # all paths must be cased accordingly ``` -------------------------------- ### Using Custom Parameter Name in Dart Source: https://github.com/slang-i18n/slang/blob/main/slang/README.md Example of using the custom parameter name 'gender' in Dart code. ```dart String a = t.greet(gender: GenderContext.female); // notice 'gender' instead of 'context' ``` -------------------------------- ### Slang Configuration with slang.yaml Source: https://github.com/slang-i18n/slang/blob/main/slang/README.md Define Slang library configurations in a slang.yaml file for projects not using build_runner. This reduces boilerplate by directly specifying settings. ```yaml base_locale: fr fallback_strategy: base_locale input_directory: lib/i18n input_file_pattern: .i18n.json output_directory: lib/i18n output_file_name: translations.g.dart lazy: true locale_handling: true flutter_integration: true namespaces: false translate_var: t enum_name: AppLocale class_name: Translations translation_class_visibility: private key_case: snake key_map_case: camel param_case: pascal sanitization: enabled: true prefix: k case: camel string_interpolation: double_braces flat_map: false translation_overrides: false timestamp: true statistics: true maps: - error.codes - category - iconNames pluralization: auto: cardinal default_parameter: n cardinal: - someKey.apple ordinal: - someKey.place contexts: GenderContext: default_parameter: gender generate_enum: true interfaces: PageData: onboarding.pages.* PageData2: attributes: - String title - String? content obfuscation: enabled: false secret: somekey format: enabled: true width: 150 autodoc: enabled: true locales: - en - fr imports: - 'package:my_package/path_to_enum.dart' generate_enum: true ``` -------------------------------- ### Set Locale Typed Example Source: https://github.com/slang-i18n/slang/blob/main/slang/CHANGELOG_OLD.md Use `setLocaleTyped` for type-safe locale setting, ensuring compatibility with `AppLocale` enum. ```dart LocaleSettings.setLocaleTyped(AppLocale.en_US); ``` -------------------------------- ### Get Device Locale Source: https://github.com/slang-i18n/slang/blob/main/slang/CHANGELOG_OLD.md Retrieve the device's current locale using FastI18n.getDeviceLocale(). This is a renamed function from FastI18n.findDeviceLocale. ```dart FastI18n.getDeviceLocale() ``` -------------------------------- ### Updated Dart Code After WIP Apply Source: https://github.com/slang-i18n/slang/blob/main/README.md Illustrates the Dart code automatically updated to use the generated method for the 'welcome.name' key. ```dart t.welcome.name(name: name); ``` -------------------------------- ### Set Locale Raw Example Source: https://github.com/slang-i18n/slang/blob/main/slang/CHANGELOG_OLD.md Use `setLocaleRaw` to change the application's locale, especially for locales encoded with an underscore. ```dart LocaleSettings.setLocaleRaw('en_US'); ``` -------------------------------- ### Get Translation Statistics Source: https://github.com/slang-i18n/slang/blob/main/slang/README.md Generate statistics for your translation files, including key counts, word counts, and character counts. ```sh dart run slang stats ``` ```text [en] - 9 keys (including intermediate keys) - 6 translations (leaves only) - 15 words - 82 characters (ex. [,.?!'¿¡]) ``` -------------------------------- ### Run slang_gpt for translation Source: https://github.com/slang-i18n/slang/blob/main/slang_gpt/README.md Execute the slang_gpt command-line tool to perform translations. Specify the target language and provide your OpenAI API key. The output directory and description can be overridden via command-line arguments. ```bash dart run slang_gpt --target=fr --api-key= ``` -------------------------------- ### Get Supported Locales Source: https://github.com/slang-i18n/slang/blob/main/slang_flutter/README.md Provides a list of all supported locales for the application. This list is commonly used when configuring the `MaterialApp` widget. ```dart // get supported locales (handy for MaterialApp) List locales = AppLocaleUtils.supportedLocales; ``` -------------------------------- ### Recasing Configuration Source: https://github.com/slang-i18n/slang/blob/main/slang/README.md Configure key casing for translations and parameters. ```yaml # Config key_case: camel key_map_case: pascal param_case: snake ``` -------------------------------- ### Build and Access Translations with Riverpod Source: https://github.com/slang-i18n/slang/blob/main/slang/documentation/dependency_injection.md Demonstrates how to build translation instances for different locales and manage them using Riverpod's `StateProvider`. Access translations and locale metadata. ```dart final english = AppLocale.en.build(); final german = AppLocale.de.build(); final translationProvider = StateProvider((ref) => german); // set it // access the current instance final t = ref.watch(translationProvider); String a = t.welcome.title; // get translation AppLocale locale = t.$meta.locale; // get locale // initialize MaterialApp MaterialApp( locale: ref.watch(translationProvider).$meta.locale.flutterLocale, supportedLocales: AppLocaleUtils.supportedLocales, localizationsDelegates: GlobalMaterialLocalizations.delegates, // from flutter_localizations package // ... ); ``` -------------------------------- ### JSON with Modifiers Source: https://github.com/slang-i18n/slang/blob/main/slang/README.md Example of combining multiple modifiers like plural, parameter, and rich text within a JSON translation structure. ```json { "apple(plural, param=appleCount, rich)": { "one": "I have $appleCount apple.", "other": "I have $appleCount apples." } } ``` -------------------------------- ### Configure Slang with slang.yaml Source: https://github.com/slang-i18n/slang/blob/main/slang/MIGRATION.md An alternative to using `build_runner`, Slang can be configured using `slang.yaml` to specify the base locale and translation variable. ```yaml # slang.yaml base_locale: en translate_var: t ``` -------------------------------- ### Using Linked Translations in Dart Source: https://github.com/slang-i18n/slang/blob/main/slang/README.md Example of how to use linked translations in Dart code, passing different plural parameters for 'apples' and 'bananas'. ```dart String a = t.sentence(appleCount: 1, bananaCount: 2); // two different plural parameters! ``` -------------------------------- ### Get Current Device Locale Source: https://github.com/slang-i18n/slang/blob/main/slang_flutter/README.md Retrieves the current device's locale settings. This can be used to determine the user's preferred language. ```dart // get current device locale AppLocale locale = AppLocaleUtils.findDeviceLocale(); ``` -------------------------------- ### Slang Apply Command Arguments Source: https://github.com/slang-i18n/slang/blob/main/slang/README.md Details the arguments for the `slang apply` command, including options for specifying a locale, output directory, and preserving the order of keys. ```text | Argument | Usage | |---------------------|--------------------------------------------------------------------------| | `--locale=` | Apply only one specific locale | | `--outdir=` | Path of analysis output (`input_directory` by default) | | `--preserve-order` | Keep the existing key order of the target files and append new keys last | ``` -------------------------------- ### Synchronize Locales Across Multiple Packages Source: https://github.com/slang-i18n/slang/blob/main/slang/README.md Demonstrates how to synchronize locales across multiple packages using Slang. Calling `LocaleSettings.setLocale` on any package will update the locale for all integrated packages. ```dart import 'package:my_package1/gen/strings.g.dart' as package1; import 'package:my_package2/gen/strings.g.dart' as package2; void main() { final t1 = package1.Translations.of(context); final t2 = package2.Translations.of(context); // this changes the locale for all packages to Spanish package1.LocaleSettings.setLocale(AppLocale.es); String spanishTitle = t2.title; // this will be in Spanish // this changes the locale for all packages to English package2.LocaleSettings.setLocale(AppLocale.en); String englishTitle = t1.title; // this will be in English } ``` -------------------------------- ### Enable Namespaces Configuration Source: https://github.com/slang-i18n/slang/blob/main/slang/README.md Enable the namespaces feature and specify the output directory and file name for generated translation files. ```yaml namespaces: true output_directory: lib/i18n output_file_name: translations.g.dart ``` -------------------------------- ### Initialize TranslationProvider in main.dart Source: https://github.com/slang-i18n/slang/blob/main/slang/README.md Ensure Flutter is initialized and wrap your root widget with `TranslationProvider` in your `main.dart` file. ```dart void main() { WidgetsFlutterBinding.ensureInitialized(); runApp(TranslationProvider(child: MyApp())); // Wrap your app with TranslationProvider } ``` -------------------------------- ### Add Hint for .i18n.json Extension Source: https://github.com/slang-i18n/slang/blob/main/slang/CHANGELOG_OLD.md Added a hint in the documentation recommending the `.i18n.json` extension for translation files. ```markdown Hint for `.i18n.json` extension ``` -------------------------------- ### Incorrect CSV Parsing Example Source: https://github.com/slang-i18n/slang/blob/main/slang/README.md Illustrates an incorrect format for translated EOL characters in CSV files. Using '' instead of '\n' will lead to parsing errors. ```csv my.path,hello\nworld ``` -------------------------------- ### Enable Deterministic Obfuscation Source: https://github.com/slang-i18n/slang/blob/main/README.md Configure Slang to enable obfuscation with a secret key for deterministic obfuscation. This helps in preventing simple string searches in the binary. ```yaml obfuscation: enabled: true secret: somekey ``` -------------------------------- ### Configure iOS Info.plist for Supported Locales Source: https://github.com/slang-i18n/slang/blob/main/slang/README.md Add the `CFBundleLocalizations` key to your `ios/Runner/Info.plist` file to specify the supported locales for your iOS application. Example includes English and German. ```xml CFBundleLocalizations en de ``` -------------------------------- ### Slang Configuration with build.yaml Source: https://github.com/slang-i18n/slang/blob/main/slang/README.md Configure Slang library options within a build.yaml file, which is necessary when using build_runner. This configuration is also recognized by `dart run slang`. ```yaml targets: $default: builders: slang_build_runner: options: base_locale: fr fallback_strategy: base_locale input_directory: lib/i18n input_file_pattern: .i18n.json output_directory: lib/i18n output_file_name: translations.g.dart lazy: true locale_handling: true flutter_integration: true namespaces: false translate_var: t enum_name: AppLocale class_name: Translations translation_class_visibility: private key_case: snake key_map_case: camel param_case: pascal sanitization: enabled: true prefix: k case: camel string_interpolation: double_braces flat_map: false translation_overrides: false timestamp: true statistics: true maps: - error.codes - category - iconNames pluralization: auto: cardinal default_parameter: n cardinal: - someKey.apple ordinal: - someKey.place contexts: GenderContext: default_parameter: gender generate_enum: true interfaces: PageData: onboarding.pages.* PageData2: attributes: - String title - String? content obfuscation: enabled: false secret: somekey format: enabled: true width: 150 autodoc: enabled: true locales: - en - fr imports: - 'package:my_package/path_to_enum.dart' generate_enum: true ``` -------------------------------- ### Build Translations with Overrides using AppLocaleUtils Source: https://github.com/slang-i18n/slang/blob/main/slang/README.md When using dependency injection, this method allows building a translation instance with overrides synchronously, useful for custom DI setups. ```dart Translations t2 = AppLocaleUtils.buildWithOverridesSync( locale: AppLocale.en, fileType: FileType.yaml, content: r''' onboarding title: 'Welcome {name}' ''', ); String a = t2.onboarding.title(name: 'Tom'); // "Welcome Tom" ``` -------------------------------- ### Apply WIP Translations with Slang Source: https://github.com/slang-i18n/slang/blob/main/slang/README.md Command to apply changes related to the Prototyping feature to translation files. Requires specifying source directories. ```sh dart run slang wip apply [--source-dirs=dir1,dir2] ``` -------------------------------- ### Run Slang Configuration Command Source: https://github.com/slang-i18n/slang/blob/main/slang/README.md Execute the `dart run slang configure` command for convenience in setting up the project configuration. ```bash dart run slang configure ``` -------------------------------- ### JSON Comments and ARB Style Source: https://github.com/slang-i18n/slang/blob/main/slang/README.md Add comments to JSON translation files. Keys starting with `@` are ignored, and their values can serve as comments. ARB style with description is also supported. ```json5 { "@@locale": "en", // fully ignored "mainScreen": { "button": "Submit", // ignored as translation but rendered as a comment "@button": "The submit button shown at the bottom", // ARB style is also possible, the description will be rendered as a comment "@button2": { "context": "HomePage", "description": "The submit button shown at the bottom" }, } } ``` -------------------------------- ### General Migration Syntax Source: https://github.com/slang-i18n/slang/blob/main/README.md Use the 'migrate' command with a specified type, source, and destination to migrate translations from other i18n solutions. ```sh dart run slang migrate ``` -------------------------------- ### AppLocale Extension Methods Source: https://github.com/slang-i18n/slang/blob/main/slang/README.md Extension methods on the AppLocale enum to easily convert to Flutter's native Locale, get the language tag string, and access translations for a specific locale. ```dart // extension methods Locale locale = AppLocale.en.flutterLocale; // to native flutter locale String tag = AppLocale.en.languageTag; // to string tag (e.g. en-US) final t = AppLocale.en.translations; // get translations of one locale ``` -------------------------------- ### Slang Analyze Command Arguments Source: https://github.com/slang-i18n/slang/blob/main/slang/README.md Details the various arguments available for the `slang analyze` command, including options for splitting output, full analysis, output directory, CI integration, and source directories. ```text | Argument | Usage | |------------------------|-----------------------------------------------------------------------| | `--split` | Split analysis for each locale | | `--split-missing` | Split missing translations for each locale | | `--split-unused` | Split unused translations for each locale | | `--full` | Find unused translations in whole source code | | `--outdir=` | Path of analysis output (`input_directory` by default) | | `--exit-if-changed` | Exit with code 1 if there are changes (for CI) | | `--source-dirs=` | Comma-separated list of source directories (default: `lib`) | ``` -------------------------------- ### Slang CLI Commands Source: https://github.com/slang-i18n/slang/blob/main/slang/README.md Provides a list of essential commands for managing translations using the Slang Command Line Interface, including generation, analysis, normalization, and migration. ```bash dart run slang # generate dart file dart run slang analyze # unused and missing translations dart run slang normalize # sort translations according to base locale dart run slang configure # automatically update CFBundleLocalizations dart run slang edit move loginPage authPage # move or rename translations dart run slang migrate arb src.arb dest.json # migrate arb to json ``` -------------------------------- ### Migrate ARB to JSON Source: https://github.com/slang-i18n/slang/blob/main/slang/README.md Convert ARB files to Slang's compatible JSON format, preserving all descriptions. ```sh dart run slang migrate arb source.arb destination.json ``` ```json { "@@locale": "en_US", "@@context": "HomePage", "title_bar": "My Cool Home", "@title_bar": { "type": "text", "context": "HomePage", "description": "Page title." }, "FOO_123": "Your pending cost is {COST}", "foo456": "Hello {0}", "pageHomeInboxCount" : "{count, plural, zero{You have no new messages} one{You have 1 new message} other{You have {count} new messages}}", "@pageHomeInboxCount" : { "placeholders": { "count": {} } } } ``` ```json { "@@locale": "en_US", "@@context": "HomePage", "title": { "bar": "My Cool Home", "@bar": "Page title." }, "foo123": "Your pending cost is {cost}", "foo456": "Hello {arg0}", "page": { "home": { "inbox": { "count(param=count)": { "zero": "You have no new messages", "one": "You have 1 new message", "other": "You have {count} new messages" } } } } } ``` -------------------------------- ### Build Runner Command Source: https://github.com/slang-i18n/slang/blob/main/slang/CHANGELOG_OLD.md Execute the `build_runner` command to generate translation files. A rebuild is often necessary after updates. ```bash flutter pub run build_runner build ``` -------------------------------- ### Add Slang Dependencies Source: https://github.com/slang-i18n/slang/blob/main/slang/README.md Add the necessary Slang packages to your project's pubspec.yaml file. Include slang_flutter if you are using Flutter. build_runner and slang_build_runner are only needed if you use build_runner. ```yaml dependencies: slang: slang_flutter: # also add this if you use flutter dev_dependencies: build_runner: # ONLY if you use build_runner (1/2) slang_build_runner: # ONLY if you use build_runner (2/2) ``` -------------------------------- ### Initialize Slang: Use Device Locale Source: https://github.com/slang-i18n/slang/blob/main/slang/README.md Initialize Slang to automatically use the device's locale. Ensure WidgetsFlutterBinding.ensureInitialized() is called before runApp(). ```dart void main() { WidgetsFlutterBinding.ensureInitialized(); // add this LocaleSettings.useDeviceLocale(); // and this runApp(MyApp()); } ``` -------------------------------- ### Configure Interface with YAML Source: https://github.com/slang-i18n/slang/blob/main/slang/documentation/interfaces.md Configure interfaces using a YAML file, specifying paths and optionally generating mixins. This allows for more fine-grained control over interface definitions. ```yaml # Config imports: - 'package:my_package/path_to_mixin.dart' # define where your mixin is (only if generate_mixin is false) interfaces: MyInterface: about.changelog.* # shorthand, deprecated: Specify the interfaces directly in the translation keys with the modifier (interface=MyInterface) MyOtherInterface: # full config paths: # deprecated: Specify the interfaces directly in the translation keys with the modifier (interface=MyOtherInterface) - onboarding.whatsNew.* generate_mixin: true # default: true attributes: - String title - String? content ``` -------------------------------- ### Library in dev_dependencies Source: https://github.com/slang-i18n/slang/blob/main/slang/CHANGELOG_OLD.md It is now recommended to place this library in `dev_dependencies` in your `pubspec.yaml` file. ```yaml dev_dependencies: fast_i18n: version: ^x.y.z ``` -------------------------------- ### Add slang and build_runner to pubspec.yaml Source: https://github.com/slang-i18n/slang/blob/main/slang_build_runner/README.md Include slang and build_runner as dependencies in your pubspec.yaml file. Ensure you specify the correct versions for your project. ```yaml # pubspec.yaml dependencies: slang: dev_dependencies: build_runner: slang_build_runner: ``` -------------------------------- ### Generate Dart Code (Build Runner) Source: https://github.com/slang-i18n/slang/blob/main/slang/README.md Alternatively, use build_runner to generate Dart code. This is useful for CI environments and initial Git checkouts. Requires the slang_build_runner package. ```text dart run build_runner build -d ``` -------------------------------- ### Applying WIP Changes Source: https://github.com/slang-i18n/slang/blob/main/slang/README.md Command to apply WIP changes, updating translation files and Dart code. ```text dart run slang wip apply ``` -------------------------------- ### Fast i18n Command Source: https://github.com/slang-i18n/slang/blob/main/slang/CHANGELOG_OLD.md Use the `fast_i18n` command for faster translation file generation compared to `build_runner`. ```bash flutter pub run fast_i18n ``` -------------------------------- ### Enable Formatting in Slang Configuration Source: https://github.com/slang-i18n/slang/blob/main/slang/README.md Configure Slang to enable code formatting for generated files. An optional width parameter can be specified. ```yaml # Config format: enabled: true width: 150 # optional ``` -------------------------------- ### Enable Obfuscation in Slang Configuration Source: https://github.com/slang-i18n/slang/blob/main/slang/README.md Configure Slang to enable string obfuscation for deterministic output. The secret key is hidden in the generated code. This primarily prevents simple string searches in binaries. ```yaml obfuscation: enabled: true secret: somekey # set this if you want deterministic obfuscation ``` -------------------------------- ### Configure Input File Pattern Source: https://github.com/slang-i18n/slang/blob/main/slang/README.md Set the input_file_pattern to specify the file type for translations. Ensure it ends with a supported extension like .json, .yaml, .csv, or .arb. ```yaml # Config input_directory: assets/i18n input_file_pattern: .i18n.json # must end with .json, .yaml, .csv, or .arb ``` -------------------------------- ### Register MCP Package Source: https://github.com/slang-i18n/slang/blob/main/slang/README.md Register the slang_mcp package with the MCP server for translation generation. ```bash # Claude Code claude mcp add --transport stdio slang_mcp -- slang_mcp ``` -------------------------------- ### Generate LocaleSettings.supportedLocales Statically Source: https://github.com/slang-i18n/slang/blob/main/slang/CHANGELOG_OLD.md Optimized `LocaleSettings.supportedLocales` generation to be static, avoiding library calls. ```dart // Generate LocaleSettings.supportedLocales statically without library call ``` -------------------------------- ### Update Build Command from fast_i18n to Slang Source: https://github.com/slang-i18n/slang/blob/main/slang/MIGRATION.md When generating translations, replace the `dart run fast_i18n` command with `dart run slang`. ```bash dart run slang ```