You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

140 lines
4.0 KiB

  1. # i18n
  2. [![GitHub Workflow Status](https://img.shields.io/github/workflow/status/unknwon/i18n/Go?logo=github&style=for-the-badge)](https://github.com/unknwon/i18n/actions?query=workflow%3AGo)
  3. [![codecov](https://img.shields.io/codecov/c/github/unknwon/i18n/master?logo=codecov&style=for-the-badge)](https://codecov.io/gh/unknwon/i18n)
  4. [![GoDoc](https://img.shields.io/badge/GoDoc-Reference-blue?style=for-the-badge&logo=go)](https://pkg.go.dev/github.com/unknwon/i18n?tab=doc)
  5. [![Sourcegraph](https://img.shields.io/badge/view%20on-Sourcegraph-brightgreen.svg?style=for-the-badge&logo=sourcegraph)](https://sourcegraph.com/github.com/unknwon/i18n)
  6. Package i18n is for app Internationalization and Localization.
  7. ## Introduction
  8. This package provides multiple-language options to improve user experience. Sites like [Go Walker](http://gowalker.org) and [gogs.io](http://gogs.io) are using this module to implement Chinese and English user interfaces.
  9. You can use following command to install this module:
  10. go get github.com/Unknwon/i18n
  11. ## Usage
  12. First of all, you have to import this package:
  13. ```go
  14. import "github.com/unknwon/i18n"
  15. ```
  16. The format of locale files is very like INI format configuration file, which is basically key-value pairs. But this module has some improvements. Every language corresponding to a locale file, for example, suppose there are two files called `locale_en-US.ini` and `locale_zh-CN.ini`.
  17. The name and extensions of locale files can be anything, but we strongly recommend you to follow the style of gogsweb.
  18. ## Minimal example
  19. Here are two simplest locale file examples:
  20. File `locale_en-US.ini`:
  21. ```ini
  22. hi = hello, %s
  23. bye = goodbye
  24. ```
  25. File `locale_zh-CN.ini`:
  26. ```ini
  27. hi = 您好,%s
  28. bye = 再见
  29. ```
  30. ### Do Translation
  31. There are two ways to do translation depends on which way is the best fit for your application or framework.
  32. Directly use package function to translate:
  33. ```go
  34. i18n.Tr("en-US", "hi", "Unknwon")
  35. i18n.Tr("en-US", "bye")
  36. ```
  37. Or create a struct and embed it:
  38. ```go
  39. type MyController struct{
  40. // ...other fields
  41. i18n.Locale
  42. }
  43. //...
  44. func ... {
  45. c := &MyController{
  46. Locale: i18n.Locale{"en-US"},
  47. }
  48. _ = c.Tr("hi", "Unknwon")
  49. _ = c.Tr("bye")
  50. }
  51. ```
  52. Code above will produce correspondingly:
  53. - English `en-US`:`hello, Unknwon`, `goodbye`
  54. - Chinese `zh-CN`:`您好,Unknwon`, `再见`
  55. ## Section
  56. For different pages, one key may map to different values. Therefore, i18n module also uses the section feature of INI format configuration to achieve section.
  57. For example, the key name is `about`, and we want to show `About` in the home page and `About Us` in about page. Then you can do following:
  58. Content in locale file:
  59. ```ini
  60. about = About
  61. [about]
  62. about = About Us
  63. ```
  64. Get `about` in home page:
  65. ```go
  66. i18n.Tr("en-US", "about")
  67. ```
  68. Get `about` in about page:
  69. ```go
  70. i18n.Tr("en-US", "about.about")
  71. ```
  72. ### Ambiguity
  73. Because dot `.` is sign of section in both [INI parser](https://github.com/go-ini/ini) and locale files, so when your key name contains `.` will cause ambiguity. At this point, you just need to add one more `.` in front of the key.
  74. For example, the key name is `about.`, then we can use:
  75. ```go
  76. i18n.Tr("en-US", ".about.")
  77. ```
  78. to get expect result.
  79. ## Helper tool
  80. Module i18n provides a command line helper tool beei18n for simplify steps of your development. You can install it as follows:
  81. go get github.com/unknwon/i18n/ui18n
  82. ### Sync locale files
  83. Command `sync` allows you use a exist local file as the template to create or sync other locale files:
  84. ui18n sync source_file.ini other1.ini other2.ini
  85. This command can operate 1 or more files in one command.
  86. ## More information
  87. - The first locale you load to the module is considered as **default locale**.
  88. - When matching non-default locale and didn't find the string, i18n will have a second try on default locale.
  89. - If i18n still cannot find string in the default locale, raw string will be returned. For instance, when the string is `hi` and it does not exist in locale file, simply return `hi` as output.