Code Monkey home page Code Monkey logo

v-localize's Introduction

V-Localize

npm

build npm version Join the chat at https://gitter.im/v-localize/Lobby

NPM

About

V-Localize is a very simple localization plugin for Vue.js. Your localizations will be available anywhere in your web application wrapped in a Vue instance.

Support

Chome Edge Firefox Opera Safari
4.0+ ✔ 3.5+ ✔ 10.50+ ✔ 4.0+ ✔

V-Localize was developed using Vue.js 2.x, support for previous versions is not available.

Usage

To install via Bower, simply do the following:

bower install v-localize

To install via NPM:

npm install v-localize

For a quick start using jsdelivr:

<script src="https://cdn.jsdelivr.net/npm/v-localize/dist/v-localize.js"></script>

Installing the plugin is then as simple as:

import Localize from 'v-localize';

Vue.use(Localize);

let localize = Localize.config({
  default: 'en-US',
  available: ['en-US', 'es-SP', {
    locale: 'ar-MS',
    orientation: 'rtl',
    font: {
      size: 'smaller'
    }
  }],
  fallback: '?',
  localizations: {
    "en-US": {
      header: {
        title: 'English'
      }
    },
    "es-SP": {
      header: {
        title: 'Spanish'
      }
    },
    "ar-MS": {
      header: {
        title: 'Arabic'
      }
    }
  }
});

new Vue({
  el: '#app',
  localize
});

Once your Vue app has been instantiated, the language can be changed by calling $locale(args*) from your Vue instance or virtual node.

<button v-on:click="$locale({l: 'en-US'})">English</button>
<button v-on:click="$locale({l: 'es-SP'})">Spanish</button>

You can specify your localizations like so:

<!-- add a localized title to this element targeting en-US -->
<h1 v-localize="{i: 'header.title', t: 'en-US', attr: 'title'}">Hello World</h1>
<!-- replace this element's text with localized item -->
<h2 v-localize="{i: 'header.title'}"></h2>

Alternatively, you can get your current localization by calling $locale() without specifying a language.

<!-- fetch current locale -->
<h1>Locale: {{ $locale() }}</h1>

For fetching a specific locale item programatically within a component method:

export default {
  name: 'some-component',
  methods: {
    getTitle() {
      window.alert(this.$locale({i: 'title'}))
    },
    getSpanishTitle() {
      window.alert(this.$locale({i: 'title', t: 'sp-ES'}))
    }
  }
}

Configuration

The plugin takes 5 options,

debug: If enabled, will spit warnings and errors to console.

default: Default language key to target if not set already.

available: List of available localizations, can optionally specify locale options. ex;

['en-US', 'es-SP', 'pr-BR', {
  locale: 'ar-MS',
  orientation: 'rtl'
}]

fallbackContent: Use the existing node's text content if enabled and requested localization is not found.

fallback: Default text to show if localization for current language not found. If not specified, will default to 'N/A'.

webStore: If the Vue instance is accessed within a web context and option webStore is enabled, plugin will store the locale in local storage for the next visit.

localizations: JSON object for localizations.

{
 "en-US": { // language branch
   "header": "Hello World!", // localization
   "nav": {
     "home": "Home" // nested localization
   }
 }
}

Locale Options

Locale configuration currently supports the following options,

orientation: Text direction of target element, useful for orientation of script languages.

font.family: Font family to change to. Re: https://www.w3schools.com/jsref/prop_style_fontfamily.asp

font.size: Font size to scale to. Re: https://www.w3schools.com/jsref/prop_style_fontsize.asp

Contributors

Contributing guidelines are as follows,

  • Any new features must include either a unit test, e2e test, or both.

    • Branches for bugs and features should be structured like so, issue-x-username.
  • Before putting in a pull request, be sure to verify you've built all your changes.

    Travis will build your changes before testing and publishing, but bower pulls from this repository directly.

  • Include your name and email in the contributors list.


Copyright (c) 2019 John Nolette Licensed under the MIT license.

v-localize's People

Contributors

gitter-badger avatar neetjn 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

Watchers

 avatar  avatar

v-localize's Issues

Add support for Vue.js 3

Help us to manage our issues by answering the following:

  1. Describe your issue:

v-localize should support Vue.js 3.x

  1. Can you reproduce the issue?

  2. On which browser/OS does the issue appear?

  3. Which version of v-localize does it affect?

  4. How would you tag this issue?

  • Question
  • Bug
  • Discussion
  • Feature request
  • Tip
  • Enhancement
  • Performance

Add new optional for optional local storage to cache last locale

v-localize currently assumes it's running in a browser on initialization, and this may not be the case if the given application is using SSR and/or initializing Vue on the server.

I'd like to add an option webStore which will also appropriately check if window is defined.

Cleanup - Convert to Coffeescript

The project could use an overhaul after I add the webStorage option. I'd like to rewrite the project in Coffeescript for some simplicity (and practice).

Migrate from Travis CI to GH actions

Help us to manage our issues by answering the following:

  1. Describe your issue:

The v-localize build process should be migrated from Travis CI to Github actions. Github actions would give us more control over our environment and provide a speedier build process and feedback loop.

  1. Can you reproduce the issue?

  2. On which browser/OS does the issue appear?

  3. Which version of v-localize does it affect?

  4. How would you tag this issue?

  • Question
  • Bug
  • Discussion
  • Feature request
  • Tip
  • Enhancement
  • Performance

Examples needed?

I don't see any examples in the readme of how to actually implement this in .vue files. How are elements labeled to be translated?

Locale Options

Add capability for locale options -- including setting page scrolling from ltr to rtl for script languages.

Minor improvements.

  1. ops.available.forEach(function(locale) instead of ops.available.forEach((locale) => { to support older browsers. There is no need to be fancy here - you saved exactly 4 (four) characters.

  2. Don't use fancy strings here: console.error(`v-localize:\n\tCould not find localization for "${binding.value.item}" in "${localize.locale}" language.`); and everywhere else. Again, why? You don't even save anything here...

  3. This component should not use localStorage internally - the decision of where to store current locale should be left to the user.

Cannot read property 'installed'

I followed every step of your guide, and I'm receiving the following error:

chrome error

I've attached my code for you.

import Vue from 'vue'
import App from './App'
import router from './router'

Vue.config.productionTip = false

// i18n
import { Localize } from 'v-localize'
Vue.use(Localize)

var localize = Localize.config({
  default: 'en',
  available: ['en', 'sp', 'pr', 'ch'],
  not_found: 'en',
  localizations: {
    en: {
      header: {
        title: 'English'
      }
    },
    sp: {
      header: {
        title: 'Spanish'
      }
    },
    pr: {
      header: {
        title: 'Portuguese'
      }
    }
  }
})

new Vue({
  el: '#app',
  localize,
  router,
  template: '<App/>',
  components: { App }
})

Cannot read property installed error

Uncaught TypeError: Cannot read property 'installed' of undefined
    at Function.Vue.use (eval at <anonymous>

I'm using vue-cli. Steps I've done:

  1. npm installed
  2. import { Localize } from 'v-localize';
  3. Vue.use(Localize);

What could be causing this?

IE 11 support

Help us to manage our issues by answering the following:

  1. Describe your issue:

TBD

  1. Can you reproduce the issue?

TBD

  1. On which browser/OS does the issue appear?

TBD

  1. Which version of v-localize does it affect?

TBD

  1. How would you tag this issue?

TBD

  • Question
  • Bug
  • Discussion
  • Feature request
  • Tip
  • Enhancement
  • Performance

Clean up, expand unit tests.

Our unit tests are currently ran using jasmine, and a basic karma setup. For the coverage, I created a custom context view with simple locale selection/display exercising both the v-localize directive and $locale mixin.

We can probably just replace jasmine with the Jest test runner, and go ahead and make use of vue-test-utils.

Add capability/setting to fallback to original element text.

IMPORTANT

Your issue might be labeled as invalid and eventually closed if:

  • You have deleted completely the issue template for no reason
  • You can not reproduce the issue
  • Your question is not an issue nor a feature request but it's only a support/consulting request (use stack overflow for it)
  • Your post violates our code of conduct

Help us to manage our issues by answering the following:

  1. Describe your issue:

v-localize should have a setting to allow falling back to the original DOM node's text content.

  1. Can you reproduce the issue?

  2. On which browser/OS does the issue appear?

  3. Which version of v-localize does it affect?

  4. How would you tag this issue?

  • Question
  • Bug
  • Discussion
  • Feature request
  • Tip
  • Enhancement
  • Performance

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.