EddyVerbruggen / nativescript-i18n

This is a plugin for Nativescript that implements native i18n in an easy manner.
65 stars 30 forks source link

translate programmatically with Angular 2 #57

Closed magar91 closed 7 years ago

magar91 commented 7 years ago

how can I call the L() function to translate programmatically inside of a .ts class component instead of an .html? I used to be able to do it with global.L() in previous versions of nativescript but with the new version 3.0.1 I can't use the global variable as I used to

rborn commented 7 years ago

@magar91 I don't know, no change was made to the way I declare L in the global scope.

rborn commented 7 years ago

@magar91 do you put require("globals"); at the beginning of your file?

magar91 commented 7 years ago

I'm using the plugin with angular 2 but I didn't use require("globals") before and it worked fine, I will try adding it. I'll come back with an answer soon

magar91 commented 7 years ago

@rborn I tried doing adding require("globals") but still I can't call the L() function in my .ts file, any idea how I can use the L() function?

EDIT: I looked around the project issues and found this

I added the following import to my .ts file

import * as application from "tns-core-modules/application";

and used the L() function like this

application.getResources().L(value);

I can use the L() function now inside my .ts class

rborn commented 7 years ago

@magar91 Thank you I opened this ticket to see why it's doing this.

rborn commented 7 years ago

@magar91 there are some replies in the NS ticket, could you give them a shot? Thank you.

magar91 commented 7 years ago

Hi @rborn I tried adding this declare var global : any; as suggested here and it works perfectly

rborn commented 7 years ago

@magar91 where did you do this? can it be done in the plugin? So it becomes seamless for other devs?

magar91 commented 7 years ago

I added it in the file where I used the function, here is an example of where I used it:

import { Component, OnInit } from "@angular/core";

import { Item } from "./item";
import { ItemService } from "./item.service";

declare var global:any;

@Component({
    selector: "ns-items",
    moduleId: module.id,
    templateUrl: "./items.component.html",
})
export class ItemsComponent implements OnInit {
    items: Item[];

    constructor(private itemService: ItemService) { }

    ngOnInit(): void {
        console.info(global.L("translate"))
        this.items = this.itemService.getItems();
    }
}

to be honest I don't know if it can be done in the plugin, I'm sorry

rborn commented 7 years ago

ok, thank you, I'll update the docs with your explanations 👍

isamilo commented 4 years ago

anyone can help please "ERROR in ../node_modules/nativescript-i18n/angular.ts Module build failed (from ../node_modules/@ngtools/webpack/src/index.js): Error: D:\Projects\masaa manne\BarPoint\node_modules\nativescript-i18n\angular.ts is missing from the TypeScript compilation. Please make sure it is in your tsconfig via the 'files' or 'include' property" I try to use localization in my Project