Code Monkey home page Code Monkey logo

vuetranslate's Introduction

VueTranslate

A VueJS (1.x, 2.0+) plugin for basic translations.

What is this?

Is a plugin to handle basic translations for your components, it adds a mixin and a directive to handle it the most comfortable way.

Like Vue-i18n?

Yes and no, Vue-i18n is a great plugin and is a lot more complete than this. This handle translations too, but is a more basic idea and smaller file (is just one file!).

What to expect?

Just translations, it is that simple.

Example

import Vue from 'vue';
import VueTranslate from 'vue-translate-plugin';

Vue.use(VueTranslate);

var myComp = Vue.extend({
	template: `<div>
	        {{ t('Hello World') }}
	        <span v-translate>How are you?</span>
	    </div>`,

    mounted() {
        // Define what language you want to use.
        // This can be called in something like a header with a language selector menu
        // Or any other case, doesn't need to be called in all components, but
        // at least in one, so it sets the global language of the plugin
    	this.$translate.setLang('es_DO');
    },

    // The translations itself, keyed by language or anything else you one
    locales: {
    	es_DO: {
        	'Hello World': 'Hola Mundo',
        	'How are you?': 'Como estás?'
        }
    }
});

var vm = new Vue({
	el: '#app',

	components: {myComp},

	template: `<div>
	    <my-comp></my-comp>
	</div>`
});

Usage

Loading translations

You can do this in three different ways:

  • A locales option in your component:
Vue.component({
	...
	locales: {
		spanish: {
			'hello world': 'hola mundo'
		}
	},
	...
})
  • Inside a component's method:
Vue.component({
	methods: {
		loadMysuperTranslation() {
			this.$translate.setLocales({
				spanish: {
					'hello world': 'hola mundo'
				}
			});
		}
	}
});
  • Globally when loading the plugin:
Vue.use(VueTranslate);

Vue.locales({
	spanish: {
		'hello world': 'hola mundo'
	}
});

Changing the language to use

Use the setLang method of the $translate property, like this:

Vue.component({
	methods: {
		showAppInSpanish() {
			this.$translate.setLang('spanish');
		}
	}
});

Events

You can listen to custom events emitted by the $translate property:

this.$translate.$on('language:changed', language => {
	console.log('The user choose '+language);
})

Parameters

You can use the method textWithParams if you would like to insert parameters in your translation strings.

this.$translate.textWithParams('translation.string', {
  keyA: 'Glenn',
  keyB: 'John'
})

{{ tWithParams('translation.string', { keyA: 'Glenn', keyB: 'John' }) }}

// In locales.js
'translation.string': 'My name is %keyA%. My brother is named %keyB%.'

// Result
'My name is Glenn. My brother is named John.'
language:init

When the first language is set.

language:changed

When the language to use was changed from the previous value.

language:modified

Everytime a language is changed, either is the first time or not.

locales:loaded

When locales are loaded either by any of the 3 options

vuetranslate's People

Contributors

bananaacid avatar dependabot[bot] avatar glenngijsberts avatar javisperez avatar mean-cj avatar wonsikin avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

vuetranslate's Issues

Same keys get overridden

When defining locales like this:

locales: {
  en: {
    'key': 'value'
  }
}

Another component with the same key value pair in the same environment overrides the component.

Use in variable

Is is possible to use the translation in a variable? Something like $('#error').text( {{ t('error min max') }} ); ?

Not obvious way to localize attributes (like placeholder)

I'm trying hard to guess how to localize attributes, like placeholder inside an <input> tag. Tried several ways, but still had no luck.

Is there a trick I'm not aware of? If so, can you add it to the docs, so newbies like me can figure this out? Thanks!

this.$translate.textWithParams is returning the translation string

Last week I've created the PR with the new method this.$translate.textWithParams. Since I'm using it now in a app I've came to the conclussion that it's not working since it's returning the translation string instead of a message with replacements.

I'll dig into it asap.

Vue.t should refer to textWithParams

having Vue.t() to point to $translate.text() is unnecessary, pointing it to $translate.textWithParams() yields the same effect, since you if-out on missing params anyways. Someone wanting clear method names, would use $translate,textWithParams() anyways.

Getting text from globally translation file set

I have a file that loads the translation plugin as it's set on the example:

Vue.use(VueTranslate);

Vue.locales({
	spanish: {
		'hello-world': 'hola mundo'
	}
        pt-br: {
                'hello-wolrd': Olá mundo
});

But I how do I get the hello-world in pt-br translation, for example?

I tried something like this.$translate.text('hello-world') on my script but it just appear hello-world instead of 'hola mundoorOlá mundo` when it's set as pt-br.

v-translate crashes on IE11 when trying to translate text passed in as prop

Hi

I have the following line on my Vue component:

<span class="omo-purchase-frequency-header__info__time" v-translate>{{ infoText }}</span>

While this component is rendered for the first time on IE11, it works. However, when the component is rendered again with different intoText property value, IE crashes with the following errors:

[Vue warn] Error in nextTick: "Error: Invalid argument:"
Error: Invalid argument.

This bug does NOT happen if v-translate is removed or the {{ infoText }} is replaced with a hardcoded text. In other words, this bug only happens when v-translate is used to translate a text passed in as a property, and the value of that property is changed. I also checked that infoText is never null, all the possible values that are used as intoText are string values and there is a translation for all of them. Also, this bug does not happen on Firefox and Chrome, only IE11.

Plugin version used: 1.2.0
Vue.js version used: 2.5.2, also tested with the latest version 2.5.16 without success

Translations file?

Hello! Thanks for sharing!!

Is it possible to store all translations in a single file and use v-translate globally?

i.e. I'm using Vuex to keep track of a dynamic language shifter and wondering what the best way to implement is. Any thoughts welcome!

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.