示例——Localization.ts

作者:追风剑情 发布于:2018-11-16 15:56 分类:TypeScript

示例

Localization.ts

  1. /**
  2. * 本地化
  3. */
  4. class Localization {
  5.  
  6. private static _dic: {[key: string]: string} = {};
  7.  
  8. private constructor() {
  9. }
  10.  
  11. public static loadString(str: string)
  12. {
  13. if (Localization.isNullOrEmpty(str)) {
  14. return "";
  15. }
  16. var lineArr: Array<string> = str.split('\n');
  17. var line: string;
  18. var kv: Array<string>;
  19. var len: number = lineArr.length;
  20. for (var i: number = 0; i < len; i++) {
  21. line = lineArr[i];
  22. if (Localization.isNullOrEmpty(line)) {
  23. continue;
  24. }
  25. kv = line.split('\t');
  26. if (kv.length < 2) {
  27. continue;
  28. }
  29. Localization._dic[kv[0]] = kv[1];
  30. }
  31. }
  32.  
  33. /**
  34. * 获取字符串
  35. */
  36. public static get(key: string, ...rest): string
  37. {
  38. //检查key
  39. if (Localization.isNullOrEmpty(key)) {
  40. return "";
  41. }
  42. var str: string = Localization._dic[key] as string
  43. if (Localization.isNullOrEmpty(key)) {
  44. return "";
  45. }
  46. //检查参数
  47. var p: string;
  48. var len: number = rest.length;
  49. var args: Array<string> = null;
  50. if (len == 1 && (rest[0] instanceof Array)) {
  51. args = rest[0];
  52. if (args != null) {
  53. len = args.length;
  54. }else{
  55. len = 0
  56. }
  57. }
  58. else {
  59. args = rest;
  60. }
  61. //替换所有参数
  62. for (var i: number = 0; i < len; i++) {
  63. p = args[i];
  64. if (p == null) {
  65. p = "";
  66. }
  67. str = str.replace(new RegExp("\\{" + i + "\\}", "g"), p);
  68. }
  69. return str;
  70. }
  71.  
  72. private static isNullOrEmpty(str: string): boolean
  73. {
  74. return (str == null || str.trim() == "");
  75. }
  76. }


ts_test.ts

  1. /// <reference path="Localization.ts" />
  2. let content: string = `
  3. key1 {0}只{1}
  4. key2 {0}条{1}
  5. key3 vvvvvvvvvv3
  6. `;
  7. Localization.loadString(content);
  8. console.log("key1="+Localization.get("key1", 1, "鸡"));
  9. console.log("key2="+Localization.get("key2", [2, "鱼"]));
  10. console.log("key3=" + Localization.get("key3"));


输出后的test.js

  1. /**
  2. * 本地化
  3. */
  4. var Localization = /** @class */ (function () {
  5. function Localization() {
  6. }
  7. Localization.loadString = function (str) {
  8. if (Localization.isNullOrEmpty(str)) {
  9. return "";
  10. }
  11. var lineArr = str.split('\n');
  12. var line;
  13. var kv;
  14. var len = lineArr.length;
  15. for (var i = 0; i < len; i++) {
  16. line = lineArr[i];
  17. if (Localization.isNullOrEmpty(line)) {
  18. continue;
  19. }
  20. kv = line.split('\t');
  21. if (kv.length < 2) {
  22. continue;
  23. }
  24. Localization._dic[kv[0]] = kv[1];
  25. }
  26. };
  27. /**
  28. * 获取字符串
  29. */
  30. Localization.get = function (key) {
  31. var rest = [];
  32. for (var _i = 1; _i < arguments.length; _i++) {
  33. rest[_i - 1] = arguments[_i];
  34. }
  35. //检查key
  36. if (Localization.isNullOrEmpty(key)) {
  37. return "";
  38. }
  39. var str = Localization._dic[key];
  40. if (Localization.isNullOrEmpty(key)) {
  41. return "";
  42. }
  43. //检查参数
  44. var p;
  45. var len = rest.length;
  46. var args = null;
  47. if (len == 1 && (rest[0] instanceof Array)) {
  48. args = rest[0];
  49. if (args != null) {
  50. len = args.length;
  51. }
  52. else {
  53. len = 0;
  54. }
  55. }
  56. else {
  57. args = rest;
  58. }
  59. //替换所有参数
  60. for (var i = 0; i < len; i++) {
  61. p = args[i];
  62. if (p == null) {
  63. p = "";
  64. }
  65. str = str.replace(new RegExp("\\{" + i + "\\}", "g"), p);
  66. }
  67. return str;
  68. };
  69. Localization.isNullOrEmpty = function (str) {
  70. return (str == null || str.trim() == "");
  71. };
  72. Localization._dic = {};
  73. return Localization;
  74. }());
  75. /// <reference path="Localization.ts" />
  76. var content = "\nkey1\t{0}\u53EA{1}\nkey2\t{0}\u6761{1}\nkey3\tvvvvvvvvvv3\n";
  77. Localization.loadString(content);
  78. console.log("key1=" + Localization.get("key1", 1, "鸡"));
  79. console.log("key2=" + Localization.get("key2", [2, "鱼"]));
  80. console.log("key3=" + Localization.get("key3"));


运行测试

1111.png

标签: TypeScript

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号