Created
November 17, 2021 04:38
-
-
Save chornthorn/9e65e27e66a5e2cb8cfb6c0e3a6dc7bc to your computer and use it in GitHub Desktop.
Flutter Transtale Function
This file contains hidden or 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
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