Skip to content

Commit f74222b

Browse files
authored
Merge pull request #3 from Maistrox9/multi_language_support
Add Multi language support
2 parents 0be1f5a + 60dc676 commit f74222b

File tree

3 files changed

+171
-27
lines changed

3 files changed

+171
-27
lines changed

README.md

+65-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ Here are some attributes that you could change. keep in mind that the controller
2222
```dart
2323
BuiltInKeyboard(
2424
controller: this.textController, // required
25-
layoutType: 'EN', // required, Only QWERTY and AZERTY are currently available
25+
language: Language.DE, // optional
26+
layout: Layout.QWERTZ, // optional, BUT MUST be checked if you modify the language!
2627
enableSpaceBar: true, // optional, add a spacebar
2728
enableBackSpace: true, // optional, add a backspace button
2829
enableCapsLock: true, // optional, add a Caps Lock button
@@ -34,7 +35,8 @@ BuiltInKeyboard(
3435
Option | Required | By default | Description
3536
--- | --- | --- | ---
3637
**controller** | yes | - | `TextEditingController`
37-
**layoutType** | yes | - | the layout of the keyboard
38+
**language** | no | Language.EN | the language of the keyboard
39+
**layout** | no | Layout.QWERTY | the layout of the keyboard
3840
**height** | no | - | height of keys
3941
**width** | no | - | width of keys
4042
**spacing** | no | 8.0 | the spacing between each row
@@ -48,3 +50,64 @@ Option | Required | By default | Description
4850
**enableLongPressUppercase** | no | false | writes an uppercase when long pressing on the keys
4951
**highlightColor** | no | - | color when pressed
5052
**splashColor** | no | - | color when pressed (material style)
53+
54+
## Contribution
55+
### Languages
56+
To add a new language or a keyboad layout to the package you only need to modify the language.dart file. The following steps will show you how to do that.
57+
58+
1. Add the short name form of your new language to the `Language` enum.
59+
```
60+
enum Language {
61+
EN,
62+
FR,
63+
DE,
64+
<your new language>,
65+
}
66+
```
67+
2. Add your layout name to the `Layout` enum if not present.
68+
```
69+
enum Layout {
70+
QWERTY,
71+
QWERTZ,
72+
AZERTY,
73+
<new layout>,
74+
}
75+
```
76+
3. Create a new map variable called `<language name>Config`. The keys of this map will be the layouts from the `Layout` enum (e.g: Layout.QWERTY), and the values will be maps with types `<String, String>` that contain the core configuration. These latter maps must contain four fields o:
77+
1. `layout`: The full text layout of the keyboard.
78+
2. `horizontalSpacing`: A floating number which will represent the spaceing between each key.
79+
3. `topLength`: The lenght of the top/first row of keys. In other words, the number of keys to display in the top row.
80+
4. `middleLength`: The number of keys in the middle row.
81+
82+
(The bottom/last row will just take the remaining keys).
83+
84+
example:
85+
```
86+
var frenchConfig = {
87+
Layout.QWERTY: <String, String>{
88+
'layout': 'qwertyuiopasdfghjklzxcvbnm',
89+
'horizontalSpacing': '6.0',
90+
'topLength': '10',
91+
'middleLength': '9',
92+
},
93+
Layout.AZERTY: <String, String>{
94+
'layout': 'azertyuiopqsdfghjklmwxcvbn',
95+
'horizontalSpacing': '6.0',
96+
'topLength': '10',
97+
'middleLength': '9',
98+
},
99+
};
100+
```
101+
4. And finally your new language config map to the `languageConfig` map, with the key as the short language name from the `Language` enum.
102+
```
103+
var languageConfig = {
104+
Language.EN: englishConfig,
105+
Language.FR: frenchConfig,
106+
Language.DE: germanConfig,
107+
Language.<short language name>: <language name>Config,
108+
};
109+
```
110+
111+
Feel free to also modify or add new things to existing languges.
112+
113+

lib/built_in_keyboard.dart

+42-25
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
library built_in_keyboard;
22

3+
import 'dart:io';
4+
35
import 'package:flutter/cupertino.dart';
46
import 'package:flutter/material.dart';
57
import 'package:flutter/services.dart';
68

9+
import 'language.dart';
10+
711
class BuiltInKeyboard extends StatefulWidget {
8-
// customize user letters
9-
final List<String>? customLetters;
12+
// Language of the keyboard
13+
final Language language;
1014

11-
// layoutType of the keyboard
12-
final String? layoutType;
15+
// layout of the keyboard
16+
final Layout layout;
1317

1418
// The controller connected to the InputField
1519
final TextEditingController? controller;
@@ -49,7 +53,8 @@ class BuiltInKeyboard extends StatefulWidget {
4953

5054
BuiltInKeyboard({
5155
@required this.controller,
52-
@required this.layoutType,
56+
this.language = Language.EN,
57+
this.layout = Layout.QWERTY,
5358
this.height,
5459
this.width,
5560
this.spacing = 8.0,
@@ -63,7 +68,6 @@ class BuiltInKeyboard extends StatefulWidget {
6368
this.enableLongPressUppercase = false,
6469
this.highlightColor,
6570
this.splashColor,
66-
this.customLetters,
6771
});
6872
@override
6973
BuiltInKeyboardState createState() => BuiltInKeyboardState();
@@ -80,22 +84,36 @@ class BuiltInKeyboardState extends State<BuiltInKeyboard> {
8084
height = screenHeight > 800 ? screenHeight * 0.059 : screenHeight * 0.07;
8185
width = screenWidth > 350 ? screenWidth * 0.084 : screenWidth * 0.082;
8286
List<Widget> keyboardLayout = layout();
87+
double hspacing;
88+
int topLen, midLen;
89+
try {
90+
hspacing = double.parse(languageConfig[widget.language]![widget.layout]![
91+
'horizontalSpacing']!);
92+
topLen = int.parse(
93+
languageConfig[widget.language]![widget.layout]!['topLength']!);
94+
midLen = int.parse(
95+
languageConfig[widget.language]![widget.layout]!['middleLength']!);
96+
} catch (_CastError) {
97+
printError(
98+
"Uknown language or layout was used, or Incorrect combination of language-layout");
99+
exit(0);
100+
}
83101
return Column(
84102
children: [
85103
Wrap(
86104
alignment: WrapAlignment.center,
87-
spacing: 6,
105+
spacing: hspacing,
88106
runSpacing: 5,
89-
children: keyboardLayout.sublist(0, 10),
107+
children: keyboardLayout.sublist(0, topLen),
90108
),
91109
SizedBox(
92110
height: widget.spacing,
93111
),
94112
Wrap(
95113
alignment: WrapAlignment.center,
96-
spacing: 6,
114+
spacing: hspacing,
97115
runSpacing: 5,
98-
children: keyboardLayout.sublist(10, 19),
116+
children: keyboardLayout.sublist(topLen, topLen + midLen),
99117
),
100118
SizedBox(
101119
height: widget.spacing,
@@ -110,9 +128,9 @@ class BuiltInKeyboardState extends State<BuiltInKeyboard> {
110128
),
111129
Wrap(
112130
alignment: WrapAlignment.center,
113-
spacing: 6,
131+
spacing: hspacing,
114132
runSpacing: 5,
115-
children: keyboardLayout.sublist(19),
133+
children: keyboardLayout.sublist(topLen + midLen),
116134
),
117135
widget.enableBackSpace
118136
? backSpace()
@@ -284,19 +302,14 @@ class BuiltInKeyboardState extends State<BuiltInKeyboard> {
284302

285303
// Keyboard layout list
286304
List<Widget> layout() {
287-
List<String>? letters = [];
288-
if (widget.layoutType == 'EN') {
289-
if (widget.enableAllUppercase || capsLockUppercase) {
290-
letters = 'qwertyuiopasdfghjklzxcvbnm'.toUpperCase().split("");
291-
} else {
292-
letters = 'qwertyuiopasdfghjklzxcvbnm'.split("");
293-
}
294-
} else if (widget.layoutType == 'FR') {
295-
if (widget.enableAllUppercase || capsLockUppercase) {
296-
letters = 'azertyuiopqsdfghjklmwxcvbn'.toUpperCase().split("");
297-
} else {
298-
letters = 'azertyuiopqsdfghjklmwxcvbn'.split("");
299-
}
305+
List<String> letters = [];
306+
try {
307+
letters =
308+
languageConfig[widget.language]![widget.layout]!['layout']!.split("");
309+
} catch (_CastError) {
310+
printError(
311+
"Uknown language or layout was used, or Incorrect combination of language-layout");
312+
exit(0);
300313
}
301314

302315
List<Widget> keyboard = [];
@@ -308,3 +321,7 @@ class BuiltInKeyboardState extends State<BuiltInKeyboard> {
308321
return keyboard;
309322
}
310323
}
324+
325+
void printError(String text) {
326+
print('\x1B[31m$text\x1B[0m');
327+
}

lib/language.dart

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
enum Language {
2+
EN,
3+
FR,
4+
DE,
5+
}
6+
7+
enum Layout {
8+
QWERTY,
9+
QWERTZ,
10+
AZERTY,
11+
}
12+
13+
var languageConfig = {
14+
Language.EN: englishConfig,
15+
Language.FR: frenchConfig,
16+
Language.DE: germanConfig,
17+
};
18+
19+
// Languages Configurations
20+
21+
var englishConfig = {
22+
Layout.QWERTY: <String, String>{
23+
'layout': 'qwertyuiopasdfghjklzxcvbnm',
24+
'horizontalSpacing': '6.0',
25+
'topLength': '10',
26+
'middleLength': '9',
27+
},
28+
Layout.QWERTZ: <String, String>{
29+
'layout': 'qwertzuiopasdfghjklyxcvbnm',
30+
'horizontalSpacing': '6.0',
31+
'topLength': '10',
32+
'middleLength': '9',
33+
},
34+
};
35+
36+
var frenchConfig = {
37+
Layout.QWERTY: <String, String>{
38+
'layout': 'qwertyuiopasdfghjklzxcvbnm',
39+
'horizontalSpacing': '6.0',
40+
'topLength': '10',
41+
'middleLength': '9',
42+
},
43+
Layout.AZERTY: <String, String>{
44+
'layout': 'azertyuiopqsdfghjklmwxcvbn',
45+
'horizontalSpacing': '6.0',
46+
'topLength': '10',
47+
'middleLength': '9',
48+
},
49+
};
50+
51+
var germanConfig = {
52+
Layout.QWERTY: <String, String>{
53+
'layout': 'qwertyuiopüasdfghjklöäzxcvbnmß',
54+
'horizontalSpacing': '2.5',
55+
'topLength': '11',
56+
'middleLength': '11',
57+
},
58+
Layout.QWERTZ: <String, String>{
59+
'layout': 'qwertzuiopüasdfghjklöäyxcvbnmß',
60+
'horizontalSpacing': '2.5',
61+
'topLength': '10',
62+
'middleLength': '11',
63+
},
64+
};

0 commit comments

Comments
 (0)