Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/speeedev/slang_example
https://github.com/speeedev/slang_example
Last synced: about 2 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/speeedev/slang_example
- Owner: speeedev
- Created: 2024-11-24T21:39:25.000Z (2 months ago)
- Default Branch: master
- Last Pushed: 2024-11-24T22:09:44.000Z (2 months ago)
- Last Synced: 2024-11-24T22:26:53.659Z (2 months ago)
- Language: Dart
- Size: 0 Bytes
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
**Medium content:** https://medium.com/@speedev/flutterda-lokalizasyon-i18n-nas%C4%B1l-yap%C4%B1l%C4%B1r-ad%C4%B1m-ad%C4%B1m-0c438fcb8537 (Turkish)
# Flutter Slang Localization Example
This project demonstrates how to implement localization (i18n) in a Flutter application using the Slang package efficiently and effectively. The application includes two main screens: a login screen (`LoginView`) and a home screen (`HomeView`). It also features dynamic language switching and automatic language detection based on the device's default settings.
### Features
- Automatically generated localization code using the Slang package.
- Dynamic language switching via a dropdown menu.
- Automatic language selection based on the device's default locale.
- Supports English (en), Turkish (tr), and German (de).
- Simple and clean code structure.### Project Structure
**Key Directories and Files**
- `lib/main.dart`: The entry point of the application, which includes localization setup and the main navigation structure.
- `lib/views/login_view.dart`: The login screen, where users can input their credentials and navigate to the home screen.
- `lib/views/home_view.dart`: The home screen, displaying a welcome message and a dropdown for language selection.
- `assets/i18n/`: Contains the JSON files.
- `lib/i18n/`: Contains generated Dart files for localization.**Localization Process**
### 1. Define Localization Files
Create JSON files for each supported language in the assets/i18n directory. For example:- `strings.i18n.json` (default locale: English)
- `strings_tr.i18n.json` (Turkish translations)
- `strings_de.i18n.json` (German translations)**Example `strings.i18n.json`:**
```json
{
"views": {
"login": {
"welcome": "Welcome",
"userName": "Username",
"userNameRequired": "Please enter your username.",
"password": "Password",
"passwordRequired": "Please enter your password.",
"passwordInvalid": "Password must be at least 6 characters.",
"loginButton": "Login",
"forgotPassword": "Forgot your password?"
},
"home": {
"welcome": "Welcome Home!",
"greeting": "Hello, {{userName}}!",
"language": "Current Language"
}
}
}
```### 2. Generate Code
Run the following command to generate Dart files for localization:```
dart run slang
```This generates files such as `strings.g.dart` in the `lib/i18n` directory.
### 3. Main Dart Configuration
In this step, we configure the app's localization and initialization settings in main.dart. This ensures that the app automatically adapts to the user's device language and enables translation features across the app.
**Main Function Configuration**
```dart
void main() {
WidgetsFlutterBinding.ensureInitialized();
LocaleSettings.useDeviceLocale(); // Automatically sets the app's locale based on device settings
runApp(
TranslationProvider( // Provides access to translations globally in the app
child: const MyApp(),
),
);
}
```**MaterialApp Configuration**
```dart
class MyApp extends StatelessWidget {
const MyApp({super.key});@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
locale: TranslationProvider.of(context).flutterLocale, // Uses the locale set by TranslationProvider
supportedLocales: AppLocaleUtils.supportedLocales,
localizationsDelegates: GlobalMaterialLocalizations.delegates,
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: LoginView(), // The first screen displayed in the app
);
}
}
```### Final: Use Translations in Code
Access localized strings via the `t` variable:```dart
Text(t.views.login.welcome) // Displays "Welcome" in English.
Text(t.views.home.greeting(userName: "John")) // Displays "Hello, John!".
```### Dynamic Language Switching
The application allows users to change the language dynamically using the dropdown menu on the `HomeView` screen. The language is switched using:```dart
LocaleSettings.setLocale(AppLocale.tr); // Switch to Turkish.
```### Screenshots
| View | Screenshot |
|------------------|------------------------------------------------------------------------|
| **Login Screen** | ![Login Screen](https://github.com/speeedev/slang_example/blob/master/screenshots/login.png) |
| **Home Screen** | ![Home Screen](https://github.com/speeedev/slang_example/blob/master/screenshots/home.png) |### Setup Instructions
Clone the Repository
```
git clone https://github.com/speeedev/slang_example.git
cd slang-example
```Install Dependencies Run the following command to install the required packages:
```
flutter pub get
```Run the Application Start the application using:
```
flutter run
```Thanks for read.