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

Few issues fixed #34

Merged
merged 12 commits into from
Jan 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
demo
*.ts
!angular.ts
!*.d.ts
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,26 @@ Assuming you have defined in **strings.xml** the definitions and in the model th
<string formatted="false" name="multi_replace">We can replace directly in xml: %s or from the model: %s</string>
~~~


To define a custom path for the i18n files (other than `App_Resources/i18n`), add this configuration to your project's package.json

~~~
"nativescript-i18n": {
"customLangPath": "app/resources/i18n"
}
~~~


Language defaults to english if the phone's language doesn't match any of your defined languages. If you want to set your own default language, add this configuration to your project's package.json

**Keep in mind that on iOS the language picked by the device will be based on the order in** `Settings` -> `Language & Region` -> `Preferred language order`

~~~
"nativescript-i18n": {
"defaultLang": "es"
}
~~~

####IMPORTANT !!####

- for all the strings definitions that have a replacement you need to add `formatted=false`
Expand Down Expand Up @@ -228,3 +248,5 @@ The following ideas are inspired by [this comment](https://github.com/NativeScri
- [ ] Do we need a cache at the module level so we don't have to cross the native bridge everytime? (a benchmark should be done to decide this)
- [x] Make the cache aware of the current language and language change
- [x] Angular support
- [x] Custom path for the language files ([#28](https://github.com/rborn/nativescript-i18n/issues/28))
- [x] Set default language for app ([#11](https://github.com/rborn/nativescript-i18n/issues/11))
8 changes: 0 additions & 8 deletions demo/app/App_Resources/Android/values-es/strings.xml

This file was deleted.

8 changes: 0 additions & 8 deletions demo/app/App_Resources/Android/values/strings.xml

This file was deleted.

2 changes: 1 addition & 1 deletion demo/app/App_Resources/iOS/es.lproj/Localizable.strings
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"app_name"="{N} i18n es";
"title_activity_kimera"="demo i18 es";
"hello"="Hola!";
"hello_replace"="Hola %s!";
"hello_replace"="Hola %s! con comillas simples \' y dobles \"";
"multi_replace"="Se puede sustituir directamente en xml: %s o desde el modelo: %s";
File renamed without changes.
File renamed without changes.
4 changes: 4 additions & 0 deletions demo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,9 @@
"babylon": "6.8.0",
"filewalker": "0.1.2",
"lazy": "1.0.11"
},
"nativescript-i18n": {
"customLangPath": "app/resources/i18n",
"defaultLang": "es"
}
}
74 changes: 44 additions & 30 deletions lib/before-prepare.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,65 @@
var fs = require('fs-extra');
var fs = require('fs');
var path = require('path');
var parser = require('xmldom').DOMParser;
var plist = require('plist');

module.exports = function(logger, platformsData, projectData, hookArgs) {
var platform = hookArgs.platform.toLowerCase();
var appResourcesDirectoryPath = projectData.appResourcesDirectoryPath;
var i18nFolderPath = path.join(projectData.projectDir, 'app', 'i18n');
var pjson = require(path.join(projectData.projectDir, 'package.json'));
var defaultLang = 'en';

if (pjson['nativescript-i18n']) {
if (pjson['nativescript-i18n'].customLangPath) {
var customLangPath = path.join(projectData.projectDir, pjson['nativescript-i18n'].customLangPath);
if (fs.existsSync(customLangPath)) {
i18nFolderPath = customLangPath;
}
}
if (pjson['nativescript-i18n'].defaultLang) {
defaultLang = pjson['nativescript-i18n'].defaultLang;
}
}


return new Promise(function(resolve, reject) {
if (fs.existsSync(i18nFolderPath)) {
if (platform === 'android') {
fs.readdirSync(i18nFolderPath).forEach(function(lang) {
var outputFile;
if (lang === 'en') {

if (lang === defaultLang) {
var androidPath = path.join(appResourcesDirectoryPath, 'Android', 'values');
if (!fs.existsSync(androidPath)) {
fs.mkdirSync(androidPath);
}
outputFile = path.join(appResourcesDirectoryPath, 'Android', 'values', 'strings.xml');
} else {
var androidPath = path.join(appResourcesDirectoryPath, 'Android', 'values-' + lang);
if (!fs.existsSync(androidPath)) {
fs.mkdirSync(androidPath);
}
outputFile = path.join(appResourcesDirectoryPath, 'Android', 'values-' + lang, 'strings.xml');
}

fs.copy(path.join(i18nFolderPath, lang, 'strings.xml'), outputFile, {
clobber: true
},
function(err) {
if (err) {
console.error(err);
reject();
}
});
var writeOk = fs.writeFile(outputFile, fs.readFileSync(path.join(i18nFolderPath, lang, 'strings.xml'), 'utf8'));
if (!writeOk) {
reject();
}
});
} else if (platform == 'ios') {
var plist = require('plist');
var infoPlistFile = path.join(appResourcesDirectoryPath, 'iOS', 'info.plist');
var plistObj = plist.parse(fs.readFileSync(infoPlistFile, 'utf8'));
plistObj["CFBundleDevelopmentRegion"] = defaultLang;
fs.writeFileSync(infoPlistFile, plist.build(plistObj));

fs.readdirSync(i18nFolderPath).forEach(function(lang) {
var iosLangPath = path.join(appResourcesDirectoryPath, 'iOS', lang + '.lproj');
if (!fs.existsSync(iosLangPath)) {
fs.mkdirSync(iosLangPath);
}

var outputFile = path.join(appResourcesDirectoryPath, 'iOS', lang + '.lproj', 'Localizable.strings');
var fileData = fs.readFileSync(path.join(i18nFolderPath, lang, 'strings.xml'), 'utf8');
var doc = new parser().parseFromString(fileData);
Expand All @@ -43,27 +73,11 @@ module.exports = function(logger, platformsData, projectData, hookArgs) {
if (def.getAttribute('name') == 'app_name') {
var outputPlistFile = path.join(appResourcesDirectoryPath, 'iOS', lang + '.lproj', 'InfoPlist.strings');
var infoPlistData='"CFBundleDisplayName"="'+unescape(def.textContent)+'";\n';
fs.ensureFile(outputPlistFile, function(err) {
if (!err) {
fs.writeFile(outputPlistFile, infoPlistData);
} else {
console.error(err);
reject();
}
});
fs.writeFile(outputPlistFile, infoPlistData);
}


}

fs.ensureFile(outputFile, function(err) {
if (!err) {
fs.writeFile(outputFile, result);
} else {
console.error(err);
reject();
}
});
fs.writeFile(outputFile, result);
});
} else if (platform == 'windows') {
//TODO PRs gladly accepted :P
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nativescript-i18n",
"version": "0.0.9",
"version": "0.1.1",
"description": "An i18n nativescript plugin using native standards",
"main": "i18n.js",
"nativescript": {
Expand Down Expand Up @@ -35,8 +35,8 @@
"license": "MIT",
"dependencies": {
"format": "^0.2.2",
"fs-extra": "^0.30.0",
"nativescript-hook": "^0.2.1",
"plist": "^2.0.1",
"xmldom": "^0.1.22"
}
}
}