PageRenderTime 5448ms CodeModel.GetById 51ms RepoModel.GetById 1ms app.codeStats 0ms

/docs/src/pages/options/quasar-language-packs.md

https://github.com/quasarframework/quasar
Markdown | 215 lines | 176 code | 39 blank | 0 comment | 0 complexity | 9250ff733cc4cc8c9168c0c2a3d52add MD5 | raw file
Possible License(s): MIT, Apache-2.0
  1. ---
  2. title: Quasar Language Packs
  3. desc: How to configure the Quasar language packs in a Quasar app.
  4. related:
  5. - /options/rtl-support
  6. - /options/app-internationalization
  7. ---
  8. A Quasar Language Pack refers to the internationalization of Quasar's own components, some of which have labels.
  9. ::: warning
  10. It should be noted that what is described below is the internationalization of Quasar components only. If you need to internationalize your own components, read [App Internationalization](/options/app-internationalization) documentation page.
  11. :::
  12. As mentioned above, some Quasar components have their own labels. When it comes to internationalization, one option is to configure labels through the label properties on each instance of Quasar components (like QTable). This is how you can customize the text to match the selected language. This however, takes time and adds unnecessary complexity to your website/app. **Instead**, you can use the Quasar Language Packs which have a number of standard label definitions translated for you, like "Cancel", "Clear", "Select", "Update", etc. No need to translate these again! And it comes out of the box.
  13. ::: tip
  14. For a complete list of available Quasar Languages, check [Quasar Languages on GitHub](https://github.com/quasarframework/quasar/tree/dev/ui/lang).
  15. <br><br>**If your desired language is not on that list**, then feel free to submit a PR to add it. It takes from 5 to 10 minutes at most. We kindly welcome any language!
  16. :::
  17. ## Configuring the default Language Pack
  18. Unless configured otherwise (see below), Quasar uses the `en-us` Language Pack by default.
  19. ### Hardcoded
  20. If the default Quasar Language Pack is not dynamically determined (does not depends on cookies for example), then you can:
  21. #### Quasar CLI
  22. Edit `/quasar.conf.js`:
  23. ```js
  24. framework: {
  25. lang: 'de'
  26. }
  27. ```
  28. #### Vue CLI
  29. Edit your `main.js`:
  30. ```js
  31. import langDe from 'quasar/lang/de'
  32. // ...
  33. // when not selecting to import all Quasar components:
  34. import { Quasar } from 'quasar'
  35. // OTHERWISE:
  36. import Quasar from 'quasar'
  37. // ...
  38. Vue.use(Quasar, {
  39. // ...,
  40. lang: langDe
  41. })
  42. ```
  43. #### Quasar UMD
  44. Include the language pack JS tag for your Quasar version and also tell Quasar to use it. Example:
  45. ```html
  46. <!-- include this after Quasar JS tag -->
  47. <script src="https://cdn.jsdelivr.net/npm/quasar@v1.0.0/dist/lang/de.umd.min.js"></script>
  48. <script>
  49. Quasar.lang.set(Quasar.lang.de)
  50. </script>
  51. ```
  52. Check what tags you need to include in your HTML files on [UMD / Standalone](/start/umd) page.
  53. ### Dynamical (non-SSR)
  54. Quasar CLI: If your desired Quasar Language Pack must be dynamically selected (example: depends on a cookie), then you need to create a boot file: `$ quasar new boot quasar-lang-pack`. This will create `/src/boot/quasar-lang-pack.js` file. Edit it to:
  55. ```js
  56. // for when you don't specify quasar.conf.js > framework: 'all'
  57. import { Quasar } from 'quasar'
  58. // OTHERWISE:
  59. import Quasar from 'quasar'
  60. export default async () => {
  61. const langIso = 'de' // ... some logic to determine it (use Cookies Plugin?)
  62. try {
  63. await import(
  64. /* webpackInclude: /(de|en-us)\.js$/ */
  65. 'quasar/lang/' + langIso
  66. )
  67. .then(lang => {
  68. Quasar.lang.set(lang.default)
  69. })
  70. }
  71. catch (err) {
  72. // Requested Quasar Language Pack does not exist,
  73. // let's not break the app, so catching error
  74. }
  75. }
  76. ```
  77. Then register this boot file into `/quasar.conf.js`:
  78. ```js
  79. boot: [
  80. 'quasar-lang-pack'
  81. ]
  82. ```
  83. ::: warning Always constrain a dynamic import
  84. Notice the use of the [Webpack magic comment](https://webpack.js.org/api/module-methods/#magic-comments) - `webpackInclude`. Otherwise all the available language packs will be bundled, resulting in an increase in the compilation time and the bundle size. See [Caveat for dynamic imports](https://quasar.dev/quasar-cli/lazy-loading#Caveat-for-dynamic-imports)
  85. :::
  86. ### Dynamical (SSR) <q-badge align="top" label="v1.11+" />
  87. When dealing with SSR, we can't use singleton objects because that would pollute sessions. As a result, as opposed to the dynamical example above (read it first!), you must also specify the `ssrContext` from your boot file:
  88. ```js
  89. // for when you don't specify quasar.conf.js > framework: 'all'
  90. import { Quasar } from 'quasar'
  91. // OTHERWISE:
  92. import Quasar from 'quasar'
  93. // ! NOTICE ssrContext param:
  94. export default async ({ ssrContext }) => {
  95. const langIso = 'de' // ... some logic to determine it (use Cookies Plugin?)
  96. try {
  97. await import(
  98. /* webpackInclude: /(de|en-us)\.js$/ */
  99. 'quasar/lang/' + langIso
  100. )
  101. .then(lang => {
  102. // ! NOTICE ssrContext param:
  103. Quasar.lang.set(lang.default, ssrContext)
  104. })
  105. }
  106. catch (err) {
  107. // Requested Quasar Language Pack does not exist,
  108. // let's not break the app, so catching error
  109. }
  110. }
  111. ```
  112. ## Change Quasar Language Pack at Runtime
  113. Example with a QSelect to dynamically change the Quasar components language:
  114. ```html
  115. <template>
  116. <q-select
  117. v-model="lang"
  118. :options="langOptions"
  119. label="Quasar Language"
  120. dense
  121. borderless
  122. emit-value
  123. map-options
  124. options-dense
  125. style="min-width: 150px"
  126. />
  127. </template>
  128. <script>
  129. import languages from 'quasar/lang/index.json'
  130. const appLanguages = languages.filter(lang =>
  131. [ 'de', 'en-us' ].includes(lang.isoName)
  132. )
  133. export default {
  134. data () {
  135. return {
  136. lang: this.$q.lang.isoName
  137. }
  138. },
  139. watch: {
  140. lang (lang) {
  141. // dynamic import, so loading on demand only
  142. import(
  143. /* webpackInclude: /(de|en-us)\.js$/ */
  144. 'quasar/lang/' + lang
  145. ).then(lang => {
  146. this.$q.lang.set(lang.default)
  147. })
  148. }
  149. },
  150. created () {
  151. this.langOptions = appLanguages.map(lang => ({
  152. label: lang.nativeName, value: lang.isoName
  153. }))
  154. }
  155. }
  156. </script>
  157. ```
  158. ## Using Quasar Language Pack in App Space
  159. Although the Quasar Language Packs **are designed only for Quasar components internal usage**, you can still use their labels for your own website/app components too.
  160. ```html
  161. "Close" label in current Quasar Language Pack is:
  162. {{ $q.lang.label.close }}
  163. ```
  164. Check a Quasar Language Pack on [GitHub](https://github.com/quasarframework/quasar/tree/dev/ui/lang) to see the structure of `$q.lang`.
  165. ## Detecting Locale
  166. There's also a method to determine user locale which is supplied by Quasar out of the box:
  167. ```js
  168. // outside of a Vue file
  169. // for when you don't specify quasar.conf.js > framework: 'all'
  170. import { Quasar } from 'quasar'
  171. // OTHERWISE:
  172. import Quasar from 'quasar'
  173. Quasar.lang.getLocale() // returns a string
  174. // inside of a Vue file
  175. this.$q.lang.getLocale() // returns a string
  176. ```