Internationalization and localization of a Kotlin / Native application

こ γ‚“ に け は, or good afternoon in Japanese.





No matter how popular the English language is, users are still more comfortable and familiar in their native language environment.





Therefore, below we will take a step-by-step look at the process of adapting a console application for Linux on Kotlin / Native to Russian and English locales.





The good old GNU gettext will help us with this .





As a result, we will make sure that this is not scary at all.





At the same time, let's see the integration with C libraries, which significantly expands the capabilities of Kotlin / Native.





What we write: a translator of cardinal numbers into Japanese.





What is expected from the reader: knowledge of the programming languages ​​Kotlin, C, a basic level of familiarity with the Linux OS (in particular Bash).





What you need in the process: any Linux distribution, any version of IntelliJ IDEA, gettext-devel installed or similar.





Motivation

Actually why Kotlin / Native and what does Japanese have to do with it?

For about half a year, I slowly and sadly learn Japanese from the textbook β€œMinna no Nihongo”: partly for the translation of the lyrics, partly just out of interest in culture.





Later I decided to switch from system development to application development, from desktop to mobile platforms, respectively, from C ++ / Qt / STL to Kotlin / JVM / Android SDK.





, . , , NIH- - , ?





Qt/QML/C++: - .





Qt Open Source, - .





Kotlin Kotlin/Native.





, - .





Kotlin/Native

Kotlin " " () Java, JVM.





JetBrains, Kotlin, .





, (Kotlin/JS) "" (Kotlin/Native).





Kotlin/Native 2017 , .





Kotlin LLVM, JVM .





, .





:





  • (Apache-2.0 license)





  • LLVM , iOS













  • "" Kotlin





  • interop C





  • : low-level , POSIX





:





  • JVM, (: Native )





  • GUI, C interop (GTK, libui)





  • Android Studio,









Kotlin/Native , Github.





(i18n): , .





(l18n): , .





: "" , , , . .





GNU gettext

, .





GNU Translation Project.





:













  • runtime









GNU gettext

Kotlin/JVM Android Android SDK, , JVM.  





Kotlin/Native .





Qt , Qt, C++ .





GNU gettext:





  • ( )





  • (Win/Mac/Linux, Android/iOS )

















: Linux, .





: stdin, .





, .





Github IntelliJ IDEA.





: ~18 Ryzen 3900X + 32GB DDR4-3600 + NVM-E SSD. .





, .





:





$ file build/bin/native/debugExecutable/JapaneseNumeralTranslator.kexe
build/bin/native/debugExecutable/JapaneseNumeralTranslator.kexe: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 2.6.16, BuildID[xxHash]=a0971dbf76e9db60, with debug_info, not stripped

$ ldd build/bin/native/debugExecutable/JapaneseNumeralTranslator.kexe
	linux-vdso.so.1 (0x00007fff890d7000)
	libdl.so.2 => /lib64/libdl.so.2 (0x00007f348e47a000)
	libm.so.6 => /lib64/libm.so.6 (0x00007f348e334000)
	libpthread.so.0 => /lib64/libpthread.so.0 (0x00007f348e312000)
	libgcc_s.so.1 => /lib64/libgcc_s.so.1 (0x00007f348e2f7000)
	libc.so.6 => /lib64/libc.so.6 (0x00007f348e12c000)
	/lib64/ld-linux-x86-64.so.2 (0x00007f348e4a0000)
	libresolv.so.2 => /lib64/libresolv.so.2 (0x00007f348e112000)
	libutil.so.1 => /lib64/libutil.so.1 (0x00007f348e10b000)
	libcrypt.so.1 => /lib64/libcrypt.so.1 (0x00007f348e0d1000)
	librt.so.1 => /lib64/librt.so.1 (0x00007f348e0c6000)

$ file build/bin/native/releaseExecutable/JapaneseNumeralTranslator.kexe 
build/bin/native/releaseExecutable/JapaneseNumeralTranslator.kexe: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 2.6.16, BuildID[xxHash]=c76aff5e0db3fdae, not stripped

$ ldd build/bin/native/releaseExecutable/JapaneseNumeralTranslator.kexe 
	linux-vdso.so.1 (0x00007ffff69c2000)
	libdl.so.2 => /lib64/libdl.so.2 (0x00007f41ad9dd000)
	libm.so.6 => /lib64/libm.so.6 (0x00007f41ad897000)
	libpthread.so.0 => /lib64/libpthread.so.0 (0x00007f41ad875000)
	libgcc_s.so.1 => /lib64/libgcc_s.so.1 (0x00007f41ad85a000)
	libc.so.6 => /lib64/libc.so.6 (0x00007f41ad68f000)
	/lib64/ld-linux-x86-64.so.2 (0x00007f41ada03000)
	libresolv.so.2 => /lib64/libresolv.so.2 (0x00007f41ad675000)
	libutil.so.1 => /lib64/libutil.so.1 (0x00007f41ad66e000)
	libcrypt.so.1 => /lib64/libcrypt.so.1 (0x00007f41ad634000)
	librt.so.1 => /lib64/librt.so.1 (0x00007f41ad629000)

$ ls -lh build/bin/native/debugExecutable/JapaneseNumeralTranslator.kexe
-rwxr-xr-x. 1 eraxillan eraxillan 1.8M Mar  7 13:24 build/bin/native/debugExecutable/JapaneseNumeralTranslator.kexe

$ ls -lh build/bin/native/releaseExecutable/JapaneseNumeralTranslator.kexe 
-rwxr-xr-x. 1 eraxillan eraxillan 529K Mar  7 13:24 build/bin/native/releaseExecutable/JapaneseNumeralTranslator.kexe

      
      



, - .





, Community Edition.





, file .  





, IDE, .





, .





printf-driven , Android AOSP.





"" gettext().





, tr(), .





import kotlinx.cinterop.*
import platform.linux.*
import platform.posix.*

fun tr(key: String): String = gettext(key)?.toKString() ?: ""
      
      



POSIX-, Locale.kt.





: , .





, locale -a.





Bash- gettext:





  • create_localization.sh





  • update_localization.sh





, .





pot- (""), .





# Extract all tr() wrapped strings to po/jnt.pot file
xgettext --keyword=tr --language=java \
    --add-comments --sort-output \
    --copyright-holder='Alexander Kamyshnikov <axill777@gmail.com>' \
    --package-name='Japanese numeral translator' \
    --package-version='1.0' \
    --msgid-bugs-address='axill777@gmail.com' \
    -o po/jnt.pot --files-from=KT_FILES
      
      



po- ( ):





# Generate locale sources
# NOTE: --no-translator option is a workaround to supress email input request
msginit --no-translator --input=po/jnt.pot --locale=en_US.UTF-8 --output po/en_US/jnt.po
msginit --no-translator --input=po/jnt.pot --locale=ru_RU.UTF-8 --output po/ru_RU/jnt.po
      
      



mo- ( ):





# Generate locale binary files
msgfmt --output-file=po/en_US/jnt.mo po/en_US/jnt.po
msgfmt --output-file=po/ru_RU/jnt.mo po/ru_RU/jnt.po
      
      



, Kotlin/Native , "" mo- .





.





, Gradle, .





mo- , gettext .





RPM/DEB-, mo- /usr/share/locale



.





, , .





During development, you just need to periodically call update_localization.sh , translate new lines, and call this script again to generate mo files.





あ γ‚Š が と う ご ざ い ま す, or thanks for your attention!





Sources of

Definitions taken from Django documentation 





Why internationalization and localization matter





Kotlin Native: watch your files





DxGetText - GNU Gettext for Delphi and C ++ Builder





PS : I plan to use Kotlin / Native further within the framework of cross-platform libraries. If there is interest, I can modify the demo, for example, port it to Windows.








All Articles