Skip to content

Commit

Permalink
Add i18n basic support
Browse files Browse the repository at this point in the history
  • Loading branch information
tom8zds committed Jan 17, 2024
1 parent 465365f commit aefa695
Show file tree
Hide file tree
Showing 28 changed files with 1,034 additions and 876 deletions.
22 changes: 22 additions & 0 deletions assets/i18n/strings.i18n.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"appTitle": "RustSend",
"home": {
"title": "Home Page"
},
"setting": {
"title": "Settings",
"brightness": {
"title": "Brightness",
"subTitle": "Current mode: $mode",
"themeMode": {
"system": "Follow system",
"light": "Light mode",
"dark": "Dark mode"
}
},
"language": {
"title": "Language",
"subTitle": "Current language: $language"
}
}
}
22 changes: 22 additions & 0 deletions assets/i18n/strings_zh.i18n.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"appTitle": "锈船",
"home": {
"title": "主页"
},
"setting": {
"title": "设置",
"brightness": {
"title": "明暗",
"subTitle": "当前模式: $mode",
"themeMode": {
"system": "跟随系统",
"light": "浅色模式",
"dark": "深色模式"
}
},
"language": {
"title": "语言",
"subTitle": "当前语言: $language"
}
}
}
7 changes: 7 additions & 0 deletions build.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
targets:
$default:
builders:
slang_build_runner:
options:
input_directory: assets/i18n
output_directory: lib/i18n # defaulting to lib/gen if input is outside of lib/
2 changes: 1 addition & 1 deletion flutter_rust_bridge.yaml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
rust_input: rust/src/bridge/**/*.rs
dart_output: lib/src/rust
dart_output: lib/rust
2 changes: 1 addition & 1 deletion integration_test/simple_test.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:localsend_rs/main.dart';
import 'package:localsend_rs/src/rust/frb_generated.dart';
import 'package:localsend_rs/rust/frb_generated.dart';
import 'package:integration_test/integration_test.dart';

void main() {
Expand Down
11 changes: 11 additions & 0 deletions lib/common/constants.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Language {
final String name;
final String localeName;

const Language(this.name, this.localeName);
}

const Map<String, Language> supportLanguages = {
"zh": Language("中文", "zh"),
"en": Language("English", "en"),
};
47 changes: 47 additions & 0 deletions lib/config_store.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';

class ConfigStore {
static ConfigStore? _instance;
static SharedPreferences? _prefs;

factory ConfigStore() {
if (_instance == null) {
throw Exception('AppSharedPrefs is not initialized. '
'Please call AppSharedPrefs.ensureInitialized before.');
}
return _instance!;
}

const ConfigStore._();

static ensureInitialized() async {
_prefs ??= await SharedPreferences.getInstance();
_instance ??= const ConfigStore._();
}

static const _themeKey = 'theme';
static const _localeKey = 'locale';

ThemeMode themeMode() {
final themeValue = _prefs!.getInt(_themeKey);
if (themeValue == null) return ThemeMode.system;

return ThemeMode.values[themeValue];
}

Future<void> updateThemeMode(ThemeMode theme) async {
await _prefs!.setInt(_themeKey, theme.index);
}

String locale() {
final localeValue = _prefs!.getString(_localeKey) ?? Platform.localeName;
return localeValue;
}

Future<void> updateLocale(String locale) async {
await _prefs!.setString(_localeKey, locale);
}
}
Loading

0 comments on commit aefa695

Please sign in to comment.