Comments (10)
Very useful feature and a bit of blocker for me, especially with the lack of reflection in Native.
from compose-multiplatform.
from compose-multiplatform.
Wow thank you! Any chance that it will be available the same for strings?
from compose-multiplatform.
No chance. If you want to read strings by path then just read a text file by a path.
String items don't have a file path
from compose-multiplatform.
No chance. If you want to read strings by path then just read a text file by a path.
What about getting a string by key, eg. getString(key = "en")
?
from compose-multiplatform.
No. How will it work with qualifiers and arguments?
There is no a such thing as a full qualified key for the string in a text representation. (as path for other resource types) We need to combine path + key in the original xml. It is not what we want. Use text files instead.
from compose-multiplatform.
If you notice the API Res.drawable.byPath("drawable/my_icon.xml")
doesn't work with qualifiers and environment. It associates a concrete file with a new resource instance.
If you have two icons: drawable-night/my_icon.xml
and drawable-light/my_icon.xml
, you are supposed to select a right icon by your own.
I understand that you would like to have something like a runtime search in your string resources based on a string key and a current environment but it is a huge performance problem. That's why we converted XML files to the internal format and use generated classes instead. To iterate by files in the runtime is not possible.
So, for your case you have to save strings to the regular txt file and read it as string on your side.
from compose-multiplatform.
If you notice the API
Res.drawable.byPath("drawable/my_icon.xml")
doesn't work with qualifiers and environment. It associates a concrete file with a new resource instance. If you have two icons:drawable-night/my_icon.xml
anddrawable-light/my_icon.xml
, you are supposed to select a right icon by your own.I understand that you would like to have something like a runtime search in your string resources based on a string key and a current environment but it is a huge performance problem. That's why we converted XML files to the internal format and use generated classes instead. To iterate by files in the runtime is not possible.
So, for your case you have to save strings to the regular txt file and read it as string on your side.
My current implementation consisted in creating a mapper that maps a string (the key) with the Resource. I had to do it like that because I was not able to use reflection. The main problem is that I need to mantain this file manually and it is very susceptible to bugs.
Sorry because of my ignorance because I don't know how the resources are internally working, but it wouldn't be possible to generate this mapping internally so we could access them through it?
from compose-multiplatform.
It is possible but the case is rare enough to maintain it. It requires more resources to support it than a real profit.
And the preferable way is static accessors for strings.
from compose-multiplatform.
I fully agree that the recommended and performant way to access resources, especially string resources, is through static accessors.
Android supports accessing resources by key, it discourages this practice for performance reasons. However, this capability exists for special cases where it might be necessary.
@Discouraged(message = "Use of this function is discouraged because resource reflection makes "
+ "it harder to perform build optimizations and compile-time "
+ "verification of code. It is much more efficient to retrieve "
+ "resources by identifier (e.g. `R.foo.bar`) than by name (e.g. "
+ "`getIdentifier(\"bar\", \"foo\", null)`).")
public int getIdentifier(String name, String defType, String defPackage)
My proposal is to introduce an opt-in feature that allows accessing resources by key. Should be discouraged, but available for the cases where is needed.
This feature could look something like the following:
fun Res.string.byPath(key: String): StringResource? {
switch(key){
"key1" -> Res.string.resource_with_key_1
"key2" -> Res.string.resource_with_key_2
else -> null
}
}
from compose-multiplatform.
Related Issues (20)
- Change visibility of helper functions (Resources) HOT 1
- Using scss in compose multiplatform HOT 1
- Failed to load native library:libjansi.jnilib. HOT 4
- iOS: Bottom sheet content isn't fully visible when keyboard is shown HOT 3
- Scroll functionality in LWJGL example causes unexplained freeze HOT 6
- Desktop window not capturable after upgrade from Compose 1.5 to 1.6 HOT 3
- Compose resources fail in Android Studio preview HOT 5
- How can I set shortcuts' name that different with packageName HOT 4
- [Desktop] Can't capture native inputs in windows using windowHandle HOT 4
- CupertinoOverscrollEffect keeps LazyColumn offseted up even more items are added in
- TextFiled Cursor Issues HOT 1
- iOS. Crash when exceeding some height value in vertically scrollable content of ModalBottomSheet HOT 1
- Compose Resource incorrectly reads string chunks from a file
- Add androidx.lifecycle.compose.LifecycleEventEffect HOT 2
- Make `SwingPanel` interop blending by default
- Ress Class name - Compose multiplatform resources HOT 1
- Compose layout system doesn't handle `additionalSafeAreaInsets` correctly. HOT 1
- Crash when using `rememberWindowState`: DpSize is unspecified
- Documentation on moving existing Android & IOS production app to KMP state
- [Web][Wasm] Issue with Color Rendering in Compose Web: Inconsistent Lightness in Arabic Fonts HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from compose-multiplatform.