在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):icerockdev/moko-resources开源软件地址(OpenSource Url):https://github.com/icerockdev/moko-resources开源编程语言(OpenSource Language):Kotlin 100.0%开源软件介绍(OpenSource Introduction):Mobile Kotlin resourcesThis is a Kotlin MultiPlatform library that provides access to the resources on macOs, iOS, Android the JVM and JS/Browser with the support of the default system localization. Table of ContentsFeatures
Requirements
Installationroot build.gradle buildscript {
repositories {
gradlePluginPortal()
}
dependencies {
classpath "dev.icerock.moko:resources-generator:0.20.1"
}
}
allprojects {
repositories {
mavenCentral()
}
} project build.gradle apply plugin: "dev.icerock.mobile.multiplatform-resources"
dependencies {
commonMainApi("dev.icerock.moko:resources:0.20.1")
androidMainApi("dev.icerock.moko:resources-compose:0.20.1")
jvmMainApi("dev.icerock.moko:resources-compose:0.20.1")
commonTestImplementation("dev.icerock.moko:resources-test:0.20.1")
}
multiplatformResources {
multiplatformResourcesPackage = "org.example.library" // required
multiplatformResourcesClassName = "SharedRes" // optional, default MR
multiplatformResourcesVisibility = MRVisibility.Internal // optional, default Public
iosBaseLocalizationRegion = "en" // optional, default "en"
multiplatformResourcesSourceSet = "commonClientMain" // optional, default "commonMain"
} To use
If your project includes a build type, for example
ios-app Info.plist: <key>CFBundleLocalizations</key>
<array>
<string>en</string>
<string>ru</string>
</array> in array should be added all used languages. JS/Browser generates json files which is included in webpack by default. For more details about JS see this example Static kotlin frameworks supportIf project configured with static framework output (for example by "$SRCROOT/../gradlew" -p "$SRCROOT/../" :yourframeworkproject:copyFrameworkResourcesToApp \
-Pmoko.resources.PLATFORM_NAME=$PLATFORM_NAME \
-Pmoko.resources.CONFIGURATION=$CONFIGURATION \
-Pmoko.resources.BUILT_PRODUCTS_DIR=$BUILT_PRODUCTS_DIR \
-Pmoko.resources.CONTENTS_FOLDER_PATH=$CONTENTS_FOLDER_PATH Please replace To disable warnings about static framework in gradle set flag: multiplatformResources {
disableStaticFrameworkWarning = true
} With Pods dependencies in KotlinWhen you use "$SRCROOT/../gradlew" -p "$SRCROOT/../" :shared:copyFrameworkResourcesToApp \
-Pmoko.resources.PLATFORM_NAME=$PLATFORM_NAME \
-Pmoko.resources.CONFIGURATION=$CONFIGURATION \
-Pmoko.resources.BUILT_PRODUCTS_DIR=$BUILT_PRODUCTS_DIR \
-Pmoko.resources.CONTENTS_FOLDER_PATH=$CONTENTS_FOLDER_PATH\
-Pkotlin.native.cocoapods.platform=$PLATFORM_NAME \
-Pkotlin.native.cocoapods.archs="$ARCHS" \
-Pkotlin.native.cocoapods.configuration=$CONFIGURATION iOS executableWhen you use "$SRCROOT/../gradlew" -p "$SRCROOT/../" :shared:copyResourcesDebugExecutableIosSimulatorArm64 \
-Pmoko.resources.BUILT_PRODUCTS_DIR=$BUILT_PRODUCTS_DIR \
-Pmoko.resources.CONTENTS_FOLDER_PATH=$CONTENTS_FOLDER_PATH
Configured sample you can see in UsageExample 1 - simple localization stringThe first step is a create a file <?xml version="1.0" encoding="UTF-8" ?>
<resources>
<string name="my_string">My default localization string</string>
</resources> Next - create a file <?xml version="1.0" encoding="UTF-8" ?>
<resources>
<string name="my_string">Моя строка локализации по умолчанию</string>
</resources> After adding the resources we can call a gradle sync or execute a gradle task fun getMyString(): StringDesc {
return StringDesc.Resource(MR.strings.my_string)
} After this we can use our functions on the platform side: val string = getMyString().toString(context = this) iOS: let string = getMyString().localized() JS: val strings = MR.stringsLoader.getOrLoad() // loading localization from a remote file
val string = getMyString().localized(strings) Note: MR directly from native sideAndroid: val string = MR.strings.my_string.desc().toString(context = this) iOS: let string = MR.strings().my_string.desc().localized() Get resourceId for Jetpack Compose / SwiftUIAndroid: val resId = MR.strings.my_string.resourceId for example in Compose: text = stringResource(id = MR.strings.email.resourceId) iOS: LocalizedStringKey(MR.strings().email.resourceId) Note: more info in issue #126. Example 2 - formatted localization stringIn <?xml version="1.0" encoding="UTF-8" ?>
<resources>
<string name="my_string_formatted">My format '%s'</string>
</resources> Then add the localized values for other languages like in example #1.
Now create the following function in fun getMyFormatDesc(input: String): StringDesc {
return StringDesc.ResourceFormatted(MR.strings.my_string_formatted, input)
} To create formatted strings from resources you can also use extension fun getMyFormatDesc(input: String): StringDesc {
return MR.strings.my_string_formatted.format(input)
} Now add support on the platform side like in example #1: val string = getMyFormatDesc("hello").toString(context = this) iOS: let string = getMyFormatDesc(input: "hello").localized() Warning: Do no mix positioned placeholders with unpositioned ones within a string, as this may lead to different behaviour on different platforms. Stick to one style for each string. Example 3 - plural stringThe first step is to create a file <?xml version="1.0" encoding="UTF-8" ?>
<resources>
<plural name="my_plural">
<item quantity="zero">zero</item>
<item quantity="one">one</item>
<item quantity="two">two</item>
<item quantity="few">few</item>
<item quantity="many">many</item>
<item quantity="other">other</item>
</plural>
</resources> Then add the localized values for other languages like in example #1. fun getMyPluralDesc(quantity: Int): StringDesc {
return StringDesc.Plural(MR.plurals.my_plural, quantity)
} Now add support on the platform side like in example #1: val string = getMyPluralDesc(10).toString(context = this) iOS: let string = getMyPluralDesc(quantity: 10).localized() Example 4 - plural formatted stringThe first step is to create file <?xml version="1.0" encoding="UTF-8" ?>
<resources>
<plural name="my_plural">
<item quantity="zero">no items</item>
<item quantity="one">%d item</item>
<item quantity="two">%d items</item>
<item quantity="few">%d items</item>
<item quantity="many">%d items</item>
<item quantity="other">%d items</item>
</plural>
</resources> Then add the localized values for other languages like in example #1. fun getMyPluralFormattedDesc(quantity: Int): StringDesc {
// we pass quantity as selector for correct plural string and for pass quantity as argument for formatting
return StringDesc.PluralFormatted(MR.plurals.my_plural, quantity, quantity)
} To create formatted plural strings from resources you can also use extension fun getMyPluralFormattedDesc(quantity: Int): StringDesc {
// we pass quantity as selector for correct plural string and for pass quantity as argument for formatting
return MR.plurals.my_plural.format(quantity, quantity)
} And like in example #1, add the platform-side support: val string = getMyPluralFormattedDesc(10).toString(context = this) iOS: let string = getMyPluralFormattedDesc(quantity: 10).localized() Example 5 - pass raw string or resourceIf we already use some resources as a placeholder value, we can use fun getUserName(user: User?): StringDesc {
if(user != null) {
return StringDesc.Raw(user.name)
} else {
return StringDesc.Resource(MR.strings.name_placeholder)
}
} And just like in example 1 usage on platform side: val string1 = getUserName(user).toString(context = this) // we got name from User model
val string2 = getUserName(null).toString(context = this) // we got name_placeholder from resources iOS: let string1 = getUserName(user: user).localized() // we got name from User model
let string2 = getUserName(user: null).localized() // we got name_placeholder from resources Example 6 - Select localization in runtimeYou can force StringDesc.localeType = StringDesc.LocaleType.Custom("es") and return to system behaviour (when localization depends on device settings): StringDesc.localeType = StringDesc.LocaleType.System() Example 7 - pass imageImage resources directory is
If we add to We got autogenerated
You can get images by their name too in fun getImageByFileName(name: String): ImageResource {
val fallbackImage = MR.images.transparent
return MR.images.getImageByFileName(name) ?: fallbackImage
}
Example 8 - pass fontFonts resources directory is
If we add to
We got autogenerated
Example 9 - pass colorsColors resources directory is <?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- format: #RRGGBB[AA] or 0xRRGGBB[AA] or RRGGBB[AA] where [AA] - optional -->
<color name="valueColor">#B02743FF</color>
<color name="referenceColor">@color/valueColor</color>
<color name="themedColor">
<light>0xB92743FF</light>
<dark>7CCFEEFF</dark>
</color>
<color name="themedReferenceColor">
<light>@color/valueColor</light>
<dark>@color/referenceColor</dark>
</color>
</resources> If you want use one color without light/dark theme selection: <color name |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论