Reputation: 2656
I'm new in Dart
and flutter
.
I want to replace English number with Farsi number. How can implement this?
1-2-3-4-5-6-7-8-9 ==> ۱-۲-۳-۴-۵-۶-۷-۸-۹
Upvotes: 23
Views: 8941
Reputation: 37372
I have published the localizeDigits()
extension method on String in the package hrk_batteries
.
Add the package:
$ dart pub add hrk_batteries
Use it as follows:
import 'package:hrk_batteries/hrk_batteries.dart';
void main() {
final String enToArLocalizedString = 'A0123456789Z'.localizeDigits(
toZeroDigit: '\u0660', // toZeroDigit: '٠',
);
print(enToArLocalizedString); // A٠١٢٣٤٥٦٧٨٩Z
final String enToFaLocalizedString = 'A0123456789Z'.localizeDigits(
toZeroDigit: '\u06f0', // toZeroDigit: '۰',
);
print(enToFaLocalizedString); // A۰۱۲۳۴۵۶۷۸۹Z
}
Upvotes: 0
Reputation: 2404
I'm a bit late to the party but here is a solution using RegExp
and String.replaceAllMapped(...)
:
extension ToFarsiNumber on String {
String toFarsi() {
const Map<String, String> numbers = {
'0': '۰',
'1': '۱',
'2': '۲',
'3': '۳',
'4': '۴',
'5': '۵',
'6': '۶',
'7': '۷',
'8': '۸',
'9': '۹',
};
return replaceAllMapped(
RegExp('[0-9]'),
(match) => numbers[this[match.start]]!,
);
}
}
Usage:
'0-1-2-3-4-5-6-7-8-9'.toFarsi(); // ۰-۱-۲-۳-۴-۵-۶-۷-۸-۹
Upvotes: 1
Reputation: 197
@Xavier answer is correct, however I would suggest to define an extension to use instead of each time wrapping your string with function
extension FarsiNumberExtension on String {
String get farsiNumber {
const english = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
const farsi = ['۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹'];
String text = this;
for (int i = 0; i < english.length; i++) {
text = text.replaceAll(english[i], farsi[i]);
}
return text;
}
}
and use like this
print('0-1-2-3-4-5-6-7-8-9'.farsiNumber);
print('myNumber123'.farsiNumber);
output
۰-۱-۲-۳-۴-۵-۶-۷-۸-۹
myNumber۱۲۳
Upvotes: 0
Reputation: 1
import 'package:alert_dialog/alert_dialog.dart';
import 'package:flutter/material.dart';
class Home2 extends StatefulWidget {
const Home2({Key? key}) : super(key: key);
@override
State<Home2> createState() => _Home2State();
}
class _Home2State extends State<Home2> {
String TransformerNum = 'محمود 2026';
TextEditingController TransformerNumController = TextEditingController();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('ArbicNumbers'),),
body: SingleChildScrollView(
child: Column(
children: [
Container(
margin: EdgeInsets.only(top:220,left: 650),
child: Text('${TransformerNum}',style: TextStyle(fontFamily: '(A) Arslan Wessam A', fontSize: 25)),
),
Container(
margin: EdgeInsets.only(top:220,left: 650),
child: ElevatedButton(
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
Text('${TransformerNum}',style: TextStyle(fontFamily: '(A) Arslan Wessam A', fontSize: 25)),
SizedBox(width: 10,),
Text('اضف بيانات المحول ',style: TextStyle(fontFamily: '(A) Arslan Wessam A', fontSize: 25)),
],
),
onPressed: () async{
return alert(
context,
title: Column(
children: [
Container(
// margin: EdgeInsets.only(left:200, top: 200, right: 300),
child: TextFormField(
controller: TransformerNumController,
decoration: InputDecoration(
labelText: 'ادخل البيانات',
hintMaxLines: 1,
enabledBorder: const OutlineInputBorder(
borderSide: BorderSide(
color: Colors.blue,
)),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(200)),
prefixIcon: const Icon(
Icons.add_circle_outline_outlined,
color: Colors.blue,
),
fillColor: Colors.lightBlue[50],
filled: true,
hintText: '....... مثال 1او 2 او'),
)
),
],
),
textOK: IconButton(onPressed: (){
setState((){
TransformerNum = replaceFarsiNumber(TransformerNumController.text);
//TransformerNum = TransformerNumController.text;
});
Navigator.of(context).pop();
}, icon: Icon(Icons.add_circle))
);
},
),
)
],
),
),
);
}
String replaceFarsiNumber(String input) {
const english = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
const farsi = ['۰', '۱', '۲', '۳', '۴', '۵', '٦', '۷', '۸', '۹'];
for (int i = 0; i < english.length; i++) {
input = input.replaceAll(english[i], farsi[i]);
}
return input;
}
}
Upvotes: 0
Reputation: 46
Here is my solution:
String engNumberToFarsi(String number) {
Map numbers = {
'0' : '۰',
'1' : '۱',
'2' : '۲',
'3' : '۳',
'4' : '۴',
'5' : '۵',
'6' : '۶',
'7' : '۷',
'8' : '۸',
'9' : '۹',
};
numbers.forEach((key, value) => number = number.replaceAll(key, value));
return number;
}
Upvotes: 1
Reputation: 162
Depending on this answer: https://stackoverflow.com/a/63934087/14409491
extension NumberConverter on num {
static const Map<String, String> arabicDigits = <String, String>{
'0': '\u0660',
'1': '\u0661',
'2': '\u0662',
'3': '\u0663',
'4': '\u0664',
'5': '\u0665',
'6': '\u0666',
'7': '\u0667',
'8': '\u0668',
'9': '\u0669',
};
String toArabicDigits() {
final String number = toString();
StringBuffer sb = StringBuffer();
for (int i = 0; i < number.length; i++) {
sb.write(arabicDigits[number[i]] ?? number[i]);
}
return sb.toString();
}
}
Upvotes: 2
Reputation: 3679
Not sure in what context you're going to use the numbers, but I would rather define a const map:
const numberMap = {0: '۰', 1: '۱', 2:'۲', 3:'۳', 4:'٤', 5:'۵', 6:'٦', 7:'۷', 8:'۸',9: '۹'};
Then you can just call numberMap[number]
to reuse it.
Upvotes: 4
Reputation:
static String ConvertDigitsToLatin(String s) {
var sb = new StringBuffer();
for (int i = 0; i < s.length; i++) {
switch (s[i]) {
//Persian digits
case '\u06f0':
sb.write('0');
break;
case '\u06f1':
sb.write('1');
break;
case '\u06f2':
sb.write('2');
break;
case '\u06f3':
sb.write('3');
break;
case '\u06f4':
sb.write('4');
break;
case '\u06f5':
sb.write('5');
break;
case '\u06f6':
sb.write('6');
break;
case '\u06f7':
sb.write('7');
break;
case '\u06f8':
sb.write('8');
break;
case '\u06f9':
sb.write('9');
break;
//Arabic digits
case '\u0660':
sb.write('0');
break;
case '\u0661':
sb.write('1');
break;
case '\u0662':
sb.write('2');
break;
case '\u0663':
sb.write('3');
break;
case '\u0664':
sb.write('4');
break;
case '\u0665':
sb.write('5');
break;
case '\u0666':
sb.write('6');
break;
case '\u0667':
sb.write('7');
break;
case '\u0668':
sb.write('8');
break;
case '\u0669':
sb.write('9');
break;
default:
sb.write(s[i]);
break;
}
}
return sb.toString();
}
Upvotes: 5
Reputation: 4015
Example:
String replaceFarsiNumber(String input) {
const english = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
const farsi = ['۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹'];
for (int i = 0; i < english.length; i++) {
input = input.replaceAll(english[i], farsi[i]);
}
return input;
}
main() {
print(replaceFarsiNumber('0-1-2-3-4-5-6-7-8-9')); // ==> ۰-۱-۲-۳-۴-۵-۶-۷-۸-۹
}
Upvotes: 36