Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When numberFormats not specified for component, does not fallback to use numberFormat specified on VueI18n instance #168

Closed
17cliu opened this issue May 30, 2017 · 8 comments
Labels
Type: Bug Bug or Bug fixes

Comments

@17cliu
Copy link

17cliu commented May 30, 2017

Versions

vue 2.2.6
vue-i18n 7.0.0

Bug description

I have component-based localization for my app. I initialize my VueI18n instance with both messages and numberFormats, and these are meant to be shared across the entire app. In only the individual components where I have specified additional component-specific translations, I can access these "shared" messages but not the shared numberFormats.

Example code

main.js

import Vue from 'vue';
import VueI18n from 'vue-i18n';

Vue.use(VueI18n);

// i18n config for app
const i18n = new VueI18n({
    locale: 'en-US',
    messages: {
        'en-US': {
            hello: 'hello world',
        },
        'ko-KR': {
            hello: '안녕하세요, 세상',
        }
    },
    numberFormats: {
        'en-US': {
            currency: {
                style: 'currency',
                currency: 'USD'
            }
        },
        'ko-KR': {
            currency: {
                style: 'currency',
                currency: 'KRW',
                currencyDisplay: 'symbol'
            }
        }
    }
});

// Init app, with i18n instance
new Vue({ i18n, /* ... */ });

Apple.vue (has component-specific i18n; $n(100, 'currency') unexpectedly throws error)

<template>
    <div>
        <!-- Template output: "hello world" -->
        <p>{{ $t('hello') }}</p>

        <!-- Template output: "i like apples" -->
        <p>{{ $t('apple') }}</p>

        <!-- Expected: "$100" -->
        <!-- Actual: Throws error, unless I repeat the `numberFormat` definition in this component. -->
        <p>{{ $n(100, 'currency') }}</p>
    </div>
</template>

<script>
export default {
    name: 'apple',
    i18n: {
        messages: {
            'en-US': {
                apple: 'i like apples',
            },
            'ko-KR': {
                apple: '사과를 좋아해요',
            }
        }
    }
};
</script>

Banana.vue (does NOT have component-specific i18n; therefore $n(100, 'currency') works as expected)

<template>
    <div>
        <!-- Template output: "hello world" -->
        <p>{{ $t('hello') }}</p>

        <!-- Template output: "$100" -->
        <p>{{ $n(100, 'currency') }}</p>
    </div>
</template>

<script>
export default {
    name: 'banana'
    // no translations for this component
};
</script>

Error thrown when evaluating $n(100, 'currency') in Apple.vue:

[Vue warn]: Error in render function: "TypeError: Cannot read property 'currency' of undefined"
@kazupon
Copy link
Owner

kazupon commented Jun 1, 2017

Thank you for your reporting!

Sorry, I forget fallback localization of DateTime and Numer. 😭
I'll implement it for v7.1

@kazupon kazupon added Type: Bug Bug or Bug fixes and removed improvement labels Jun 2, 2017
@kazupon kazupon closed this as completed in be9e1bd Jun 2, 2017
@morgler
Copy link

morgler commented May 8, 2018

Is this issue fixed in 7.6.0? I have a similar combination, where some messages and the dateTimeFormats are defined when instantiating Vue, but I also have some local translations in <i18n> tags with the SFC.

Although the component seems to render the global message correctly, I still get warnings for every globally defined translation key (message or dateTimeFormat) in the console:

Fall back to translate the keypath 'test' with root locale.

@SaraiDuek
Copy link

Same here, The translation and formatting is working well but still i get:
[vue-i18n] Fall back to 'en' number formats from 'en number formats.
for each translation.

Just to note, I have the flag silentTranslationWarn: true on.
From the docs, it's not clear if this flag is also meant for number formatting but if not, It might be nice to add it to it, or add a flag for suppressing number and dates warnings (especially until the bug is fixed).
If this flag supposed to suppress these warnings too, it is not working for me in this case.

@rudolfbatke
Copy link

I receive the the same warnings when I use $n(cellValue, 'currency') or $n(cellValue, 'percent'):
Fall back to 'en' number formats from 'de number formats.
Fall back to number localization of root: key 'currency' .
Fall back to number localization of root: key 'percent' .

How can I disable this warnings? The flag silentTranslationWarn: true doesn't help me too.

The numberFormats config is this:

const numberFormats = {
  en: {
    currency: {
      style: 'currency',
      currency: 'EUR',
    },
    percent: {
      style: 'percent',
      minimumFractionDigits: 1,
    },
  },
  de: {
    currency: {
      style: 'currency',
      currency: 'EUR',
    },
    percent: {
      style: 'percent',
      minimumFractionDigits: 1,
    },
  },
};

@albernhagen
Copy link

Was there ever any update on this? I also have a similar problem as to @rudiba

@PizzaPete
Copy link

Same problem as @rudiba still occurs to me.

@vdachev
Copy link

vdachev commented Mar 27, 2019

Shouldn't the recently introduced silentFallbackWarn setting affect the behavior for number and date/time formatting as well?

@kepi0809
Copy link

I had the same issue with v8.11.2 luckily I updated to the latest version (8.15.5) and it works as expected

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Bug Bug or Bug fixes
Projects
None yet
Development

No branches or pull requests

9 participants