-
Notifications
You must be signed in to change notification settings - Fork 213
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Multiple Language support added #1071
Open
Dhruv80576
wants to merge
2
commits into
fossasia:flutter_app
Choose a base branch
from
Dhruv80576:settings
base: flutter_app
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
{ | ||
"title":"BadgeMusic", | ||
"speed":"Speed", | ||
"animation":"Animation", | ||
"effects":"Effects", | ||
"save":"Save", | ||
"reset":"Reset", | ||
"transfer":"Transfer", | ||
"anim_left":"Left", | ||
"anim_right":"Right", | ||
"anim_up":"Up", | ||
"anim_down":"Down", | ||
"anim_fixed":"Fixed", | ||
"anim_snowflake":"Snowflake", | ||
"anim_picture":"Picture", | ||
"anim_anim":"Animation", | ||
"anim_laser":"Laser", | ||
"effect_invert":"Invert", | ||
"effect_effect":"Effect", | ||
"effect_marquee":"Marquee", | ||
"create_badge":"Create Badge", | ||
"draw_badge":"Draw Badge", | ||
"saved_badges":"Saved Badges", | ||
"saved_cliparts":"Saved Cliparts", | ||
"settings":"Settings", | ||
"about":"About Us", | ||
"buy_bages":"Buy Badges", | ||
"share":"Share", | ||
"rate":"Rate Us", | ||
"feedback":"Feedback/Bug Reports", | ||
"privacy_policy":"Privacy Policy", | ||
"draw":"Draw", | ||
"erase":"Erase", | ||
"no_cliparts":"No saved clipart!", | ||
"no_clip_desc":"Looks like there are no saved cliparts yet.", | ||
"language":"Language", | ||
"select_badge":"Select Badge" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
{ | ||
"title": "徽章音乐", | ||
"speed": "速度", | ||
"animation": "动画", | ||
"effects": "效果", | ||
"save": "保存", | ||
"reset": "重置", | ||
"transfer": "传输", | ||
"anim_left": "左", | ||
"anim_right": "右", | ||
"anim_up": "上", | ||
"anim_down": "下", | ||
"anim_fixed": "固定", | ||
"anim_snowflake": "雪花", | ||
"anim_picture": "图片", | ||
"anim_anim": "动画", | ||
"anim_laser": "激光", | ||
"effect_invert": "反转", | ||
"effect_effect": "效果", | ||
"effect_marquee": "跑马灯", | ||
"create_badge": "创建徽章", | ||
"draw_badge": "绘制徽章", | ||
"saved_badges": "已保存的徽章", | ||
"saved_cliparts": "已保存的剪贴画", | ||
"settings": "设置", | ||
"about": "关于我们", | ||
"buy_bages": "购买徽章", | ||
"share": "分享", | ||
"rate": "评价我们", | ||
"feedback": "反馈/错误报告", | ||
"privacy_policy": "隐私政策", | ||
"draw": "绘制", | ||
"erase": "擦除", | ||
"no_cliparts": "没有保存的剪贴画!", | ||
"no_clip_desc": "看起来还没有保存的剪贴画。", | ||
"language": "语言", | ||
"select_badge": "选择徽章" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import 'dart:convert'; | ||
import 'package:flutter/material.dart'; | ||
import 'package:flutter/services.dart'; | ||
|
||
class AppLocalizations { | ||
final Locale? locale; | ||
|
||
AppLocalizations({ | ||
this.locale, | ||
}); | ||
|
||
// A static method to obtain an instance of AppLocalizations in the context | ||
static AppLocalizations? of(BuildContext context) { | ||
return Localizations.of<AppLocalizations>(context, AppLocalizations); | ||
} | ||
|
||
// A LocalizationsDelegate for this localization class | ||
static const LocalizationsDelegate<AppLocalizations> delegate = | ||
_AppLocalizationsDelegate(); | ||
|
||
// A map to store the localized strings | ||
late Map<String, String> _localizedStrings; | ||
|
||
// Method to load localized strings from JSON files | ||
Future loadJsonLanguage() async { | ||
String jsonString = | ||
await rootBundle.loadString("assets/lang/${locale!.languageCode}.json"); | ||
|
||
// Parse the JSON string into a map | ||
Map<String, dynamic> jsonMap = json.decode(jsonString); | ||
|
||
// Convert the map values to strings and store them in the _localizedStrings map | ||
_localizedStrings = jsonMap.map((key, value) { | ||
return MapEntry(key, value.toString()); | ||
}); | ||
} | ||
|
||
// Method to translate a key into a localized string | ||
String translate(String key) => _localizedStrings[key] ?? ""; | ||
|
||
// Method to translate a key with dynamic arguments | ||
String translateWithArgs(String key, Map<String, dynamic> args) { | ||
String translation = _localizedStrings[key] ?? ""; | ||
|
||
// Replace placeholders in the translation string with actual values from args | ||
args.forEach((key, value) { | ||
translation = translation.replaceAll("{$key}", value.toString()); | ||
}); | ||
|
||
return translation; | ||
} | ||
} | ||
|
||
class _AppLocalizationsDelegate | ||
extends LocalizationsDelegate<AppLocalizations> { | ||
const _AppLocalizationsDelegate(); | ||
|
||
@override | ||
bool isSupported(Locale locale) { | ||
// Define the supported locales (e.g., 'en' for English, 'ar' for Arabic) | ||
return ['en', 'zh'].contains(locale.languageCode); | ||
} | ||
|
||
@override | ||
Future<AppLocalizations> load(Locale locale) async { | ||
// Create an instance of AppLocalizations and load the appropriate JSON file | ||
AppLocalizations localizations = AppLocalizations(locale: locale); | ||
await localizations.loadJsonLanguage(); | ||
return localizations; | ||
} | ||
|
||
@override | ||
bool shouldReload(covariant LocalizationsDelegate<AppLocalizations> old) => | ||
false; | ||
} | ||
|
||
extension TranslateX on String { | ||
String tr(BuildContext context) { | ||
return AppLocalizations.of(context)!.translate(this); | ||
} | ||
} | ||
|
||
extension TranslateWithArg on String { | ||
String trWithArg(BuildContext context, Map<String, dynamic> args) { | ||
return AppLocalizations.of(context)!.translateWithArgs(this, args); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// lib/providers/locale_provider.dart | ||
import 'package:flutter/material.dart'; | ||
import 'package:provider/provider.dart'; | ||
import 'package:intl/intl.dart'; | ||
|
||
class LocaleProvider with ChangeNotifier { | ||
Locale _locale = Locale('en', 'US'); | ||
|
||
Locale get locale => _locale; | ||
|
||
void changeLocale(Locale newLocale) { | ||
_locale = newLocale; | ||
notifyListeners(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion: Consider adding debug logging for missing translation keys
In debug mode, log a warning when a translation key is missing to help catch issues during development.