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

Setting another value for a translation key should be updated #216

Open
rabr2ro opened this issue Aug 30, 2016 · 11 comments
Open

Setting another value for a translation key should be updated #216

rabr2ro opened this issue Aug 30, 2016 · 11 comments

Comments

@rabr2ro
Copy link

rabr2ro commented Aug 30, 2016

I'm submitting a ... (check one with "x")

[x] bug report => check the FAQ and search github for a similar issue or PR before submitting
[ ] support request => check the FAQ and search github for a similar issue before submitting
[ ] feature request

Current behavior

From what I could see, translations member, within TranslationService, for a specific language, is not a dictionary but an object tree. For example:

translations[EN] = {
    'user': {
         'firstName': 'First Name',
         'lastName': 'Last Name'
    }
}

so, to use one of the keys in the pipe, you simply write

{{ 'user.firstName' | translate }}

The signature for setting another value for one of the key is:

public set(key: string, value: string, lang: string = this.currentLang): void

so, if one wants to change from code the value of the firstName, would inject TranslationService and do

this.translationService.set('user.firstName', 'Only Name')

but the current implementation is not having the desired result, because, after executing the previous set, the translations object inside TranslatonService will look like this:

translations[EN] = {
    'user': {
         'firstName': 'First Name',
         'lastName': 'Last Name'
    },
    'user.firstName': 'Only Name'
}

So, either I do not use correctly the set method, in which case the documentation should be updated, or it's internal implementation should be updated

this.translations[lang][key] = value;

Reproduction of the problem
If the current behavior is a bug or you can illustrate your feature request better with an example, please provide the steps to reproduce and if possible a minimal demo of the problem via https://plnkr.co or similar (you can use this template as a starting point: http://plnkr.co/edit/tpl:btpW3l0jr5beJVjohy1Q).

Please tell us about your environment:

  • ng2-translate version: 2.4.2
  • Angular version: 2.0.0-rc.5
  • Browser: [ Chrome XX | Firefox XX ]
  • Language: [ TypeScript 1.8 ]
@ocombe
Copy link
Member

ocombe commented Aug 30, 2016

Good point, I'll have to fix that :)

@rabr2ro
Copy link
Author

rabr2ro commented Aug 30, 2016

Thank you. I have a possible implementation for the problem, but it's in my own service that is a wrapper for the TranslationService. If you want, I can put here the code that I have (is TypeScript), you can take a look and you can refine it and included in the TranslationService implementation. Let me know if you would like to see what I have.

@ocombe
Copy link
Member

ocombe commented Aug 30, 2016

sure, that would help, thanks

@rabr2ro
Copy link
Author

rabr2ro commented Aug 30, 2016

Ok. So here it is:

private set(key: string, value: string): void {
        let translations: any = (<any>this._translationService).translations[this._translationService.currentLang];
        let path: string[] = key.split('.');
        this.setDeepValue(translations, value, path);

        this._translationService.onTranslationChange.emit(<TranslationChangeEvent>{
            translations: translations,
            lang: this._translationService.currentLang
        });
    }

private setDeepValue(obj: any, value: any, path: string[]): void {
        if (path.length > 1) {
            let fragment: string = path.shift();
            if (obj[fragment] == null || typeof obj[fragment] !== 'object') {
                obj[fragment] = {};
            }
            this.setDeepValue(obj[fragment], value, path);
        } else {
            obj[path[0]] = value;
        }
    }

where this is the instance of my wrapper service, which has TranslationService injected as _translationService member. I had to do a cast to any, so that I can address internal translations member, within the service. setDeepValue it's an adaptation of a function that I found on StackOverflow :)

@StefH
Copy link

StefH commented Jul 4, 2017

@ocombe
Any update on this issue ?

@Mwoagh
Copy link

Mwoagh commented Aug 28, 2017

Yes, I would also like this fixed.

@topikus
Copy link

topikus commented Nov 20, 2017

Any update on this issue ?

@anasvn
Copy link

anasvn commented Jun 25, 2020

any updates. Why this is still getting issue in 2020?

@krrr25
Copy link

krrr25 commented Jul 25, 2020

@ocombe
Any updates on this feature?

@jaznjaz
Copy link

jaznjaz commented Oct 26, 2021

2021 still no updates ?

@Mitthraw
Copy link

Well we are now in 2022 and this is still not working...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

9 participants