I’m new to Kotlin and I wanted to start a project for a Desktop application.
To do it I initially used the multiplatform wizard.
Now, I want to add multilanguage support to my simple application and I found some tutorials for creating the strings.xml
file.
I created it in a new folder located in composeApp/src/commonMain/composeResources/values
which is at the same level of the wizard default created composeApp/src/commonMain/composeResources/drawable
.
My xml
looks like:
<code><resources>
<string name="no_ports">No ports available.</string>
</resources>
</code>
<code><resources>
<string name="no_ports">No ports available.</string>
</resources>
</code>
<resources>
<string name="no_ports">No ports available.</string>
</resources>
When I try to run my application
<code>package org.openswim
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.material.Button
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import kotlinx.coroutines.*
import org.jetbrains.compose.resources.stringResource
import org.jetbrains.compose.ui.tooling.preview.Preview
import com.fazecast.jSerialComm.SerialPort
import openswim.composeapp.generated.resources.Res
import openswim.composeapp.generated.resources.compose_multiplatform
@Composable
@Preview
fun App() {
MaterialTheme {
var showContent by remember { mutableStateOf(false) }
val coroutineScope = rememberCoroutineScope()
var ports by remember { mutableStateOf(emptyArray<SerialPort>()) }
// Start a coroutine to fetch ports
LaunchedEffect(Unit) {
coroutineScope.launch {
while (true) {
ports = SerialPort.getCommPorts()
delay(500) // Fetch every 0.5 seconds
}
}
}
Column(Modifier.fillMaxWidth(), horizontalAlignment = Alignment.CenterHorizontally) {
Button(onClick = { showContent = !showContent }) {
Text("Show ports")
}
AnimatedVisibility(showContent) {
Column(Modifier.fillMaxWidth(), horizontalAlignment = Alignment.CenterHorizontally) {
Text("Available Serial Ports:")
if (ports.isEmpty()) {
Text(stringResource(Res.string.no_ports))
} else {
ports.forEach { port ->
Text("Name: ${port.getSystemPortName()}, Baud Rate: ${port.getBaudRate()}, Description: ${port.getPortDescription()}")
}
}
}
}
}
}
}
</code>
<code>package org.openswim
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.material.Button
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import kotlinx.coroutines.*
import org.jetbrains.compose.resources.stringResource
import org.jetbrains.compose.ui.tooling.preview.Preview
import com.fazecast.jSerialComm.SerialPort
import openswim.composeapp.generated.resources.Res
import openswim.composeapp.generated.resources.compose_multiplatform
@Composable
@Preview
fun App() {
MaterialTheme {
var showContent by remember { mutableStateOf(false) }
val coroutineScope = rememberCoroutineScope()
var ports by remember { mutableStateOf(emptyArray<SerialPort>()) }
// Start a coroutine to fetch ports
LaunchedEffect(Unit) {
coroutineScope.launch {
while (true) {
ports = SerialPort.getCommPorts()
delay(500) // Fetch every 0.5 seconds
}
}
}
Column(Modifier.fillMaxWidth(), horizontalAlignment = Alignment.CenterHorizontally) {
Button(onClick = { showContent = !showContent }) {
Text("Show ports")
}
AnimatedVisibility(showContent) {
Column(Modifier.fillMaxWidth(), horizontalAlignment = Alignment.CenterHorizontally) {
Text("Available Serial Ports:")
if (ports.isEmpty()) {
Text(stringResource(Res.string.no_ports))
} else {
ports.forEach { port ->
Text("Name: ${port.getSystemPortName()}, Baud Rate: ${port.getBaudRate()}, Description: ${port.getPortDescription()}")
}
}
}
}
}
}
}
</code>
package org.openswim
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.material.Button
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import kotlinx.coroutines.*
import org.jetbrains.compose.resources.stringResource
import org.jetbrains.compose.ui.tooling.preview.Preview
import com.fazecast.jSerialComm.SerialPort
import openswim.composeapp.generated.resources.Res
import openswim.composeapp.generated.resources.compose_multiplatform
@Composable
@Preview
fun App() {
MaterialTheme {
var showContent by remember { mutableStateOf(false) }
val coroutineScope = rememberCoroutineScope()
var ports by remember { mutableStateOf(emptyArray<SerialPort>()) }
// Start a coroutine to fetch ports
LaunchedEffect(Unit) {
coroutineScope.launch {
while (true) {
ports = SerialPort.getCommPorts()
delay(500) // Fetch every 0.5 seconds
}
}
}
Column(Modifier.fillMaxWidth(), horizontalAlignment = Alignment.CenterHorizontally) {
Button(onClick = { showContent = !showContent }) {
Text("Show ports")
}
AnimatedVisibility(showContent) {
Column(Modifier.fillMaxWidth(), horizontalAlignment = Alignment.CenterHorizontally) {
Text("Available Serial Ports:")
if (ports.isEmpty()) {
Text(stringResource(Res.string.no_ports))
} else {
ports.forEach { port ->
Text("Name: ${port.getSystemPortName()}, Baud Rate: ${port.getBaudRate()}, Description: ${port.getPortDescription()}")
}
}
}
}
}
}
}
I got this error:
<code>Unresolved reference 'no_ports'.
</code>
<code>Unresolved reference 'no_ports'.
</code>
Unresolved reference 'no_ports'.
but I’m not able to understand what’s missing.