Giter Club home page Giter Club logo

locolaser-kotlin-mpp-example's Introduction

LocoLaser Kotlin Multiplatform Example

LocoLaser - Localization tool that generate localization files for various platforms depends on common source.
This example shows how to use LocoLaser in Kotlin Mobile Multiplatform projects to generate localized strings repository class with common interface for both mobile platforms: Android and iOS.

1 Step: Apply gradle plugin

Example uses standard way to apply custom gradle plugin.
In build.gradle of the project was added maven repository and classpath:

buildscript {
  repositories {
    maven {
      // 1.1: Add maven repository
      url "https://plugins.gradle.org/m2/"
    }
    
    ...
  }
  dependencies {
    // 1.2: Add classpath dependency
    classpath "gradle.plugin.ru.pocketbyte.locolaser:plugin:1.0.4"
    
    ...
  }
}

Apply plugin in build.gradle of common module:

// 1.3: Apply LocoLaser plugin
apply plugin: "ru.pocketbyte.locolaser"
2 Step: Add 'localize' dependency

Choose which type of artifact you will use and add them as localize dependency. This example uses artifact to work with Kotlin Mobile Multiplatform projects:

dependencies {
    // 2.1: Add dependency for Kotlin mobile platforms
    localize "ru.pocketbyte.locolaser:platform-kotlin-mpp:1.3.0"
    // 2.2: Add dependency for JSON platform (for JS i18next)
    localize "ru.pocketbyte.locolaser:platform-json:1.3.0"
    
    ...
}
3 Step: Create configuration file

How to build config file you can find in Wiki: Platform: Kotlin Mobile Multiplatform By default this config file should have name"localize_config.json" and be placed in the root folder of the project.

4 Step: Run task 'localize' before build

This example run :localize task before each build. To do it add task dependencies in build.gradle of common module:

// 4 Run task ‘localize’ before each build
preBuild.dependsOn {
    project.tasks.getByPath(':common:localize')
}
5 Step: Init repository

As a final step instance of Strings repository should be created. This example use common object Repository with ‘expect’ modifier. This object should be implemented for each platform independently. In Android application it's better to implement standard singleton and initialize it in Application class in method onCreate:

// 5.1 Init Android Repository
Repository.initInstance(AndroidStringRepository(this))

In iOS application it's better to initialize property right in property declaration

JS implementation require i18next object as parameter So at first first add dependency with externals into common build.gradle:

kotlin {
    sourceSets {
        jsMain {
            // 5.2 Add i18next dependency
            dependencies {
                api "ru.pocketbyte.locolaser:i18next-externals:1.0"
            }
        }
    }
}

Then you should initialize it before use:

i18next.init(localizationConfig) { _, _ ->
    // 5.3 Init JS Repository
    Repository.init(JsStringRepository(i18next))
    ...
}
Usage in code

Now any string can be received using code as following:

Repository.str.screen_main_hello_text
6 Step: Plurals

Current version of Kotlin/Native doesn't support variadic Objective-C methods (see issue JetBrains/kotlin-native#1834). But this feature needs to implement plurals in iOS and macOS. If you want to use plurals, you should implement some workaround to make it work on iOS and macOS.

At first add LocalizedPlural.def into [common module]/src/iosMain/c_interop/.

Then add interop into build.gradle:

kotlin {
    targets {
        ...
    
        fromPreset(iOSTarget, 'ios') {
            // 6.2 Add interop to Objective-C code that implements work work with plurals
            compilations.main.cinterops{
                LocalizedPlural {
                    defFile project.file("src/iosMain/c_interop/LocalizedPlural.def")
                }
            }
            
            ...
        }
        ...
    }
}

Thats all. Now you able to use plurals on iOS/macOS. Any plural string can be received using code as following:

val count = 10
Repository.str.screen_main_plural_string(count)
7 Step: Move generated files into build folder

Because String Repository classes generated depends on string resources, no need to hold them together with other source classes and commit into git. This example overrides res_dir of kotlin classes and put them into folders ./build/generated/locolaser/[platform]. To tell gradle plugin about this files, add source dirs into gradle.build of common module:

kotlin {
    sourceSets {
        commonMain {
            // 7.1 Add source dir for generated common classes
            kotlin.srcDir('./build/generated/locolaser/common/')
            
            ...
        }

        androidMain {
            // 7.2 Add source dir for generated android classes
            kotlin.srcDir('./build/generated/locolaser/android/')
            
            ...
        }

        jsMain {
            // 7.3 Add source dir for generated js classes
            kotlin.srcDir('./build/generated/locolaser/js/')

            ...
        }

        iosMain {
            // 7.4 Add source dir for generated iOS classes
            kotlin.srcDir('./build/generated/locolaser/ios/')
            
            ...
        }
    }
    ...
}

License

Copyright © 2019 Denis Shurygin. All rights reserved.
Contacts: <[email protected]>

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

locolaser-kotlin-mpp-example's People

Contributors

kamisempai avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.