Skip to content

Instantly share code, notes, and snippets.

@chornthorn
Created November 17, 2021 04:38
Show Gist options
  • Save chornthorn/9e65e27e66a5e2cb8cfb6c0e3a6dc7bc to your computer and use it in GitHub Desktop.
Save chornthorn/9e65e27e66a5e2cb8cfb6c0e3a6dc7bc to your computer and use it in GitHub Desktop.
Flutter Transtale Function
void main() {
Map<String, dynamic> map = {
"hello": {
"thorn": "Bong Thorn",
"second": {
"sala": "OneSala"
}
}
};
final rusult = _getTranslation("hello.second.sala", map);
print(rusult);
}
String? _getTranslation(String key, Map<String, dynamic> map) {
List<String> keys = key.split('.');
if (keys.length > 1) {
var firstKey = keys.first;
if (map.containsKey(firstKey) && map[firstKey] is! String) {
return _getTranslation(
key.substring(key.indexOf('.') + 1), map[firstKey]);
}
}
return map[key];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment