gettext有些不能翻译_使用gettext翻译您的Vue.js应用程序

gettext有些不能翻译

vue-gettext (vue-gettext)

Translate Vue.js applications with gettext.

使用gettext转换Vue.js应用程序。

vue-gettext is a plugin to translate Vue.js applications with gettext. It relies on the GNU gettext toolset and easygettext.

vue-gettext是一个使用gettext转换Vue.js应用程序的插件。 它依赖于GNU gettext工具集和easygettext。

贡献 (Contribution)

Please make sure to read the Pull request guidelines before making a pull request.

在发出请求之前,请确保阅读“请求请求准则”。

安装 (Installation)

NPM (NPM)

npm install vue-gettext

基本安装 (Basic installation)

Basic installation with ES6 modules:

使用ES6模块的基本安装:

// ES6
import Vue from 'vue'
import GetTextPlugin from 'vue-gettext'
import translations from './path/to/translations.json'

Vue.use(GetTextPlugin, {translations: translations})

组态 (Configuration)

There are a number of options you can use to configure the vue-gettext plugin:

您可以使用许多选项来配置vue-gettext插件:

OptionTypeRequirementDescription
translations{Object}requiredThe JSON file of the application's translations (produced by gettext-compile). It's exposed as a Vue global property as Vue.$translations
availableLanguages{Object}optionalAn object that represents the list of the available languages for the app whose keys are local names (e.g. en or en_US) and whose values are language names used for the display in UI, e.g. English (United States). It's exposed in all Vue instances via vm.$language.available
defaultLanguage{String}optionalThe local name of the default language, e.g. en_US. This will be the current active language. It's exposed in all Vue instances via vm.$language.current
languageVmMixin{Object}optionalA mixin that will be passed to the main languageVm instance (exposed via $language) that can be used, for example, to add custom computed properties
silent{Boolean}optionalEnable or disable logs/warnings for missing translations and untranslated keys. Default value is Vue.config.silent.
muteLanguages{Array}optionalDiscard warnings for missing translations for all languages of the list. This is useful to avoid messages from the language used in source code.
选项 类型 需求 描述
translations {Object} 需要 应用程序翻译的JSON文件(由gettext-compile )。 它作为Vue公开为Vue的全局属性Vue.$translations
availableLanguages {Object} 可选的 一个对象,代表该应用程序可用语言的列表,这些应用程序的键是本地名称 (例如enen_US ),其值是用于UI中显示的语言名称 ,例如English (United States) 。 它通过vm.$language.available在所有Vue实例中公开
defaultLanguage {String} 可选的 默认语言的本地名称 ,例如en_US 。 这将是当前使用的语言。 它通过vm.$language.current在所有Vue实例中公开
languageVmMixin {Object} 可选的 一个mixin ,将传递给主要的languageVm实例(通过$language公开),可用于添加自定义计算属性
silent {Boolean} 可选的 为缺少的翻译和未翻译的键启用或禁用日志/警告。 默认值为Vue.config.silent
muteLanguages {Array} 可选的 丢弃列表中所有语言缺少翻译的警告。 这对于避免来自源代码中使用的语言的消息很有用。

Example:

例:

// ES6
import Vue from 'vue'
import GetTextPlugin from 'vue-gettext'
import translations from './path/to/translations.json'

Vue.use(GetTextPlugin, {
  availableLanguages: {
    en_GB: 'British English',
    en_US: 'American English',
    es_US: 'Español',
    fr_FR: 'Français',
    it_IT: 'Italiano',
  },
  defaultLanguage: 'fr_FR',
  languageVmMixin: {
    computed: {
      currentKebabCase: function () {
        return this.current.toLowerCase().replace('_', '-')
      },
    },
  },
  translations: translations,
  silent: True,
})

vm.$language (vm.$language)

After the plugin initialization, a languageVm Vue instance is injected into every component as vm.$language.

插件初始化后,将languageVm Vue实例作为vm.$language注入到每个组件中。

It exposes the following properties:

它公开了以下属性:

  • vm.$language.available: an object that represents the list of the available languages (defined at configuration time)

    vm.$language.available :一个代表可用语言列表的对象(在配置时定义)

  • vm.$language.current: the current language (defined at configuration time)

    vm.$language.current :当前语言(在配置时定义)

  • whatever you passed to the plugin mixin

    无论您传递给插件mixin

You can use vm.$language.current and vm.$language.available to e.g. easily build a language switch component with a single template:

您可以使用vm.$language.currentvm.$language.available例如使用一个模板轻松构建语言切换组件:

<template>
  <div>
    <select name="language" v-model="$language.current">
      <option v-for="(language, key) in $language.available" :value="key">{{ language }}</option>
    </select>
  </div>
</template>

Vue.config.language (Vue.config.language)

After the plugin initialization, a global and reactive language property is added to Vue.config that you can use to get or set the current language outside of Vue instances.

插件初始化后, Vue.config全局和响应language属性添加到Vue.config ,您可以使用该属性在Vue实例外部获取或设置当前语言。

> Vue.config.language
'en_GB'
> Vue.config.language = 'fr_FR'

You can use Vue.config.language to e.g. configure a third party plugin in a filter:

您可以使用Vue.config.language例如在过滤器中配置第三方插件:

import moment from 'moment'
import Vue from 'vue'

const dateFormat = function (value, formatString) {
  moment.locale(Vue.config.language)
  return moment(value).format(arguments.length > 1 ? formatString : 'dddd D MMMM HH:mm:ss')
}

工作流程 (Workflow)

  1. Annotate your strings

    注释字符串

  2. Extract translations (make makemessages)

    提取翻译( make makemessages )

  3. Translate message files

    翻译消息文件

  4. Compile translations (make translations)

    编译翻译( make translations )

Annotate    |       Extract        |              Translate                 |        Compile
--------------------------------------------------------------------------------------------------------
component.js
component.vue ---> /tmp/template.pot ---> app/locale/fr_FR/LC_MESSAGES/app.po ---> app/translations.json
template.html

1a)在模板( .html.vue文件)中注释字符串 (1a) Annotating strings in templates (.html or .vue files))

使用组件或指令 (Use the component or the directive)

Strings are marked as translatable in your templates using either the translate component or the v-translate directive:

使用translate组件或v-translate指令,字符串在模板中被标记为可翻译的:

<translate>Hello!</translate>
<span v-translate>Hello!</span>

This will automatically be translated. For instance, in French, it might read Bonjour !.

这将自动翻译。 例如,在法语中,它可能显示为Bonjour!

单数 (Singular)
<translate>Hello!</translate>
复数 (Plural)
<translate :translate-n="count" translate-plural="%{ count } cars">%{ count } car</translate>
语境 (Context)
<translate translate-context="Verb">Foo</translate>
评论 (Comment)
<translate translate-comment="My comment for translators">Foo</translate>
自订参数 (Custom parameters)

You can set up translation strings that are agnostic to how your app state is structured. This way you can change variable names within your app, it won't break your translation strings.

您可以设置与应用状态结构无关的翻译字符串。 这样,您可以在应用程序中更改变量名,不会破坏您的翻译字符串。

<translate :translate-params="{name: userFullName}">Foo %{name}</translate>

HTML支持:组件和指令之间的区别 (HTML support: difference between the component and the directive)

It proves to be tricky to support interpolation with HTML content in Vue.js components because it's hard to access the raw content of the component itself.

在Vue.js 组件中支持HTML内容插值非常困难,因为很难访问组件本身的原始内容。

So if you need to include HTML content in your translations you may use the directive.

因此,如果您需要在翻译中包含HTML内容,则可以使用指令

The directive has the same set of capabilities as the component, except for translate-params which should be passed in as an expression.

该指令具有与组件相同的功能集, 除了应该作为表达式传递的translate-params

<p
  v-translate='{count: carNumbers}'
  :translate-n="carNumbers"
  translate-plural="<strong>%{ count }</strong> cars"
  translate-comment="My comment for translators"
  >
  <strong>%{ count }</strong> car
</p>

translate组件的自定义HTML标记 (Custom HTML tag for the translate component)

When rendered, the content of the translate component will be wrapped in a span element by default. You can also use another tag:

呈现时,默认情况下, translate组件的内容将包装在span元素中。 您还可以使用另一个标签:

<translate tag="h1">Hello!</translate>

翻译自: https://vuejsexamples.com/translate-your-vue-js-applications-with-gettext/

gettext有些不能翻译

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现群聊功能,需要使用XMPP协议和strophe.js库进行连接和通信。具体步骤如下: 1. 引入strophe.js库。 在项目中安装strophe.js库,并在需要使用的组件或页面中引入: ``` import Strophe from 'strophe.js'; ``` 2. 创建XMPP连接。 在组件或页面中创建XMPP连接,并设置连接参数: ``` const connection = new Strophe.Connection('ws://example.com:5280/ws-xmpp'); connection.connect(jid, password, onConnect); ``` 其中,'ws://example.com:5280/ws-xmpp'是XMPP服务器的WebSocket地址,jid和password是用户的登录名和密码,onConnect是连接成功后的回调函数。 3. 加入群聊房间。 在连接成功后,可以使用Strophe.muc库加入群聊房间: ``` const roomJID = 'groupchat@example.com'; const nickName = 'my_nickname'; const muc = Strophe.muc.join(connection, roomJID, nickName, onRoomMessage, onRoomPresence); ``` 其中,roomJID是群聊房间的JID,nickName是用户在群聊中的昵称,onRoomMessage和onRoomPresence是收到房间消息和房间成员状态变化的回调函数。 4. 发送群聊消息。 使用Strophe.muc库发送群聊消息: ``` muc.groupchat('Hello, everyone!'); ``` 其中,'Hello, everyone!'是需要发送的消息内容。 5. 接收群聊消息。 在加入群聊房间后,通过onRoomMessage回调函数接收群聊消息: ``` function onRoomMessage(msg) { const from = Strophe.getBareJidFromJid(msg.getAttribute('from')); const body = Strophe.getText(msg.getElementsByTagName('body')[0]); console.log(`Received message from ${from}: ${body}`); } ``` 其中,from是消息发送者的JID,body是消息内容。 以上就是使用strophe.js连接服务器实现群聊功能的基本步骤。需要注意的是,在实现过程中还需要处理连接断开和错误等异常情况。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值