misc/localization
로컬라이제이션.
Localization.
Localizationextends Object
로컬라이제이션.
- 언어 자동 감지 + 번역 테이블 + {0}{1} 자리 표시자 치환 + 폴백을 한 곳에 모았다.
- 키는 원문 그대로 써도 된다. 표에 없으면 원문(키)을 그대로 돌려주므로 점진 번역이 된다. 사용 예:
Localization.setTable("en", { "확인": "OK", "{0}점 획득": "Got {0} points" });
Localization.setLanguage(Localization.detectLanguage(["ko", "en"]));
Localization.text("{0}점 획득", 120);- 언어 태그를 뒤 조각부터 하나씩 떼며 맞추고 별칭도 본다. (setLanguageAliases)
- 오른쪽에서 왼쪽으로 쓰는 언어(isRightToLeft, setRightToLeft)면 배치를 뒤집고(mirrorLayoutX, readLayoutTextAlign), 글 사이에 넣는 값을 방향 격리 문자로 묶는다. (isolateText, textNamed 의 "{이름}" 자리, 빗금으로만 이은 값들은 통째로 한 번 묶는다)
Localization.
- Gathers automatic language detection + translation tables + {0}{1} placeholder substitution + fallback in one place.
- Keys may be the source text as is. If missing from the table, the source text (key) is returned as is, so translation can be gradual. Usage example:
Localization.setTable("en", { "Confirm": "OK", "{0} points earned": "Got {0} points" });
Localization.setLanguage(Localization.detectLanguage(["ko", "en"]));
Localization.text("{0} points earned", 120);- Matches a language tag by dropping trailing subtags one at a time, and also checks aliases. (setLanguageAliases)
- For a right-to-left language (isRightToLeft, setRightToLeft), mirrors the layout (mirrorLayoutX, readLayoutTextAlign), and wraps values inserted in text with directional isolate characters. (isolateText, the "{name}" placeholders of textNamed, values joined only by slashes are wrapped once as a whole)
import { Localization } from "./build/vanilla.js";
Localization.setTable("en", { "greeting": "Hello", "score": "Got {0} points", "restart": "Restart in {seconds}" });
Localization.setTable("fr", { "greeting": "Bonjour", "score": "{0} points obtenus" });
Localization.setFallbackLanguage("en");
const languageCode = Localization.selectLanguage(["fr-CA", "en-US"], ["en", "fr"], "en");
Localization.setLanguage(languageCode);
console.log(languageCode, Localization.text("greeting"), Localization.text("score", 120));
console.log(Localization.textNamed("restart", { seconds: 3 }));브라우저 언어 감지. (정적)
- navigator.languages 우선순위를 훑어 지원 목록에 있는 첫 언어를 고른다. (selectLanguage) "zh-TW" 같은 지역 표기는 "zh-tw" → "zh" 순으로 대조한다.
Detects the browser language. (static)
- Scans navigator.languages in priority order and picks the first language in the supported list. (selectLanguage) A regional tag like "zh-TW" is matched in the order "zh-tw" → "zh".
- supportedLanguageCodesstring[]
- 지원하는 언어 코드 목록. (대소문자는 가리지 않는다)List of supported language codes. (case-insensitive)
- defaultLanguageCodestring
- 맞는 것이 없을 때 돌려줄 코드.Code returned when nothing matches.
반환Returnsstring 고른 언어 코드. (찾았으면 소문자, 없으면 기본 코드 그대로)The chosen language code. (lowercase if found, otherwise the default code as is)
짝Counterpart: vanilla-engine Localization.detect_language
선호 언어 목록에서 지원하는 첫 언어 고르기. (정적, detectLanguage 의 대조 부분)
- 태그의 뒤 조각부터 하나씩 떼며 지원 목록의 코드와 별칭을 본다. ("zh-Hant-TW" → "zh-hant-tw", "zh-hant", "zh", 밑줄은 하이픈으로)
Picks the first supported language from a preferred language list. (static, the matching part of detectLanguage)
- Drops trailing subtags of the tag one at a time and checks the codes and aliases in the supported list. ("zh-Hant-TW" → "zh-hant-tw", "zh-hant", "zh", underscores become hyphens)
- candidateListstring[]
- 선호 차례의 언어 태그 목록. (navigator.languages 꼴)Language tags in order of preference. (navigator.languages form)
- supportedLanguageCodesstring[]
- 지원하는 언어 코드 목록. (대소문자는 가리지 않는다)List of supported language codes. (case-insensitive)
- defaultLanguageCodestring
- 맞는 것이 없을 때 돌려줄 코드.Code returned when nothing matches.
반환Returnsstring 고른 언어 코드. (찾았으면 소문자, 없으면 기본 코드 그대로)The chosen language code. (lowercase if found, otherwise the default code as is)
짝Counterpart: vanilla-engine Localization.select_language
import { Localization } from "./build/vanilla.js";
Localization.setLanguageAliases("zh-hant", ["zh-tw", "zh-hk"]);
const languageCode = Localization.selectLanguage(["zh-TW", "en-US"], ["en", "zh-hant"], "en");
console.log(languageCode);언어의 별칭 설정. (정적, 예: "zh-hant" 에 "zh-tw", "zh-hk", 태그를 맞출 때 별칭도 그 언어로 본다)
Sets aliases for a language. (static, e.g. "zh-tw", "zh-hk" for "zh-hant", aliases also count as that language when matching tags)
- languageCodestring
- 별칭이 가리킬 언어 코드.Language code the aliases point to.
- aliasListstring[]
- 별칭 태그 목록. (소문자로 바꾸고 밑줄은 하이픈으로 바꾼다)List of alias tags. (lowercased, underscores become hyphens)
짝Counterpart: vanilla-engine Localization.set_language_aliases
오른쪽에서 왼쪽으로 쓰는 언어로 정하거나 풀기. (정적, 처음은 ar, fa, he, ur, ps, yi, dv)
Sets or unsets a language as right-to-left. (static, initially ar, fa, he, ur, ps, yi, dv)
- languageCodestring
- 언어 코드나 태그. (첫 조각만 쓴다)Language code or tag. (only the first subtag is used)
- isRightToLeftboolean
- 오른쪽에서 왼쪽으로 쓰면 참. (거짓이면 목록에서 뺀다)True if written right to left. (removed from the list if false)
짝Counterpart: vanilla-engine Localization.set_right_to_left
지금 언어가 오른쪽에서 왼쪽으로 쓰는 언어인지. (정적, 글자를 그 방향으로 굽고 배치를 좌우로 뒤집을 때)
- setRightToLeft 처럼 소문자로, 밑줄은 하이픈으로 읽은 첫 조각을 본다. ("ar_EG" 도 ar)
Whether the current language is right-to-left. (static, for baking text in that direction and mirroring the layout horizontally)
- Like setRightToLeft, it checks the first part read in lowercase with underscores as hyphens. ("ar_EG" is ar too)
반환Returnsboolean 지금 언어의 첫 조각이 오른쪽에서 왼쪽 목록에 있으면 참.True if the first subtag of the current language is in the right-to-left list.
짝Counterpart: vanilla-engine Localization.is_right_to_left
배치의 가로 자리. (정적, 오른쪽에서 왼쪽 언어면 부모 너비 안에서 좌우를 뒤집은 왼쪽, 아니면 그대로)
- 창과 표의 왼쪽, 오른쪽 배치를 언어 방향에 맞춰 뒤집을 때 쓴다. 화면 구석의 자리나 슬라이더의 방향처럼 뒤집지 않을 것은 부르지 않는다.
Horizontal position of the layout. (static, for a right-to-left language the left mirrored within the parent width, otherwise unchanged)
- Used to mirror the left and right placement of windows and tables to match the language direction. Not called for things that should not be mirrored, like screen corner positions or slider direction.
- xnumber
- 왼쪽에서 오른쪽 언어일 때의 왼쪽.Left for a left-to-right language.
- widthnumber
- 놓을 것의 너비.Width of the thing being placed.
- parentWidthnumber
- 부모의 너비.Width of the parent.
반환Returnsnumber 지금 언어 방향에 맞춘 왼쪽.Left position matched to the current language direction.
짝Counterpart: vanilla-engine Localization.mirror_layout_x
배치의 글자 정렬. (정적, 오른쪽에서 왼쪽 언어면 "left" 와 "right" 를 바꾸고, 나머지는 그대로)
Text alignment of the layout. (static, for a right-to-left language swaps "left" and "right", others unchanged)
- textAlignstring
- TextAlign 의 값.Value of TextAlign.
반환Returnsstring 지금 언어 방향에 맞춘 정렬 값.Alignment value matched to the current language direction.
짝Counterpart: vanilla-engine Localization.read_layout_text_align
글 사이에 끼우는 값 묶기. (정적, 오른쪽에서 왼쪽 언어면 방향 격리 문자로 묶고, 아니면 그대로)
- 숫자와 단위(123°/s, +60%)나 사람 이름이 앞뒤 글과 섞여 흩어지지 않고 제 방향으로 한 덩어리로 놓이게 한다.
Wraps a value inserted in text. (static, for a right-to-left language wraps it with directional isolate characters, otherwise unchanged)
- Keeps numbers with units (123°/s, +60%) or people's names placed as one chunk in their own direction, instead of mixing and scattering with the surrounding text.
- textstring
- 묶을 값의 글.Text of the value to wrap.
반환Returnsstring 오른쪽에서 왼쪽 언어면 방향 격리 문자로 묶은 글, 아니면 그대로. (빈 글은 그대로)Text wrapped in directional isolate characters for right-to-left languages, otherwise unchanged. (empty text is unchanged)
짝Counterpart: vanilla-engine Localization.isolate_text
이름 자리 채우기. (정적, "Restart {seconds}" + { seconds: 3 } → "Restart 3")
- 번역문을 찾은 뒤(text) 채운다. 오른쪽에서 왼쪽 언어면 넣은 값을 방향 격리 문자로 묶는다.
- 빗금으로만 이은 값들("{count}/{limit}")은 값마다가 아니라 통째로 한 번 묶는다. (값마다 묶으면 1/40 이 40/1 로 보인다)
- 이름이 없는 자리는 그대로 둔다.
Fills named placeholders. (static, "Restart {seconds}" + { seconds: 3 } → "Restart 3")
- Fills after finding the translation (text). For a right-to-left language, wraps the inserted values with directional isolate characters.
- Values joined only by slashes ("{count}/{limit}") are wrapped once as a whole, not per value. (wrapping per value makes 1/40 look like 40/1)
- Placeholders with no matching name are left as is.
- keystring
- 번역 키. (원문)Translation key. (source text)
- valuesobject
- 자리 이름 → 값.Placeholder name → value.
반환Returnsstring 자리를 채운 번역문.The translated text with placeholders filled.
짝Counterpart: vanilla-engine Localization.text_named
import { Localization } from "./build/vanilla.js";
Localization.setTable("ar", { "progress": "{count}/{limit}" });
Localization.setLanguage("ar");
const progressText = Localization.textNamed("progress", { count: 1, limit: 40 });
console.log(Localization.isRightToLeft(), progressText.length);
Localization.setLanguage("en");번역 테이블 설정. (정적)
Sets a translation table. (static)
- languageCodestring
- 언어 코드. (소문자로 바꿔 둔다)Language code. (stored lowercased)
- tableobject
- 키(원문) → 번역문. 값은 문자열 또는 문자열 배열.Key (source text) → translation. The value is a string or a string array.
짝Counterpart: vanilla-engine Localization.set_table
JSON 객체로 번역 표 설정. (정적, JsonAsset 으로 읽은 data 같은 것, 값이 글이면 글, 글 배열이면 글만 모은 목록, 그 밖의 값은 건너뛴다)
Sets a translation table from a JSON object. (static, such as data read with JsonAsset, a string value as a string, a string array as a list of only its strings, other values skipped)
- languageCodestring
- 언어 코드. (소문자로 바꿔 둔다)Language code. (stored lowercased)
- tableJsonobject
- 키 → 글 또는 글 배열인 JSON 객체. (객체가 아니면 빈 표)JSON object of key → text or text array. (an empty table if not an object)
짝Counterpart: vanilla-engine Localization.set_table_from_json
현재 언어 설정. (정적)
Sets the current language. (static)
- languageCodestring
- 언어 코드. (소문자로 바꿔 둔다)Language code. (stored lowercased)
짝Counterpart: vanilla-engine Localization.set_language
현재 언어 반환. (정적)
Returns the current language. (static)
반환Returnsstring 지금 언어 코드. (소문자, 처음은 "ko")The current language code. (lowercase, "ko" at first)
짝Counterpart: vanilla-engine Localization.get_language
폴백 언어 설정. (정적, 현재 언어 표에 없으면 이 언어 표를 본다)
Sets the fallback language. (static, looks in this language's table when missing from the current language's table)
- languageCodestring
- 폴백 언어 코드. (소문자로 바꿔 둔다, 처음은 "ko")Fallback language code. (stored lowercased, "ko" at first)
짝Counterpart: vanilla-engine Localization.set_fallback_language
번역문 조회. (정적)
- 현재 언어 → 폴백 언어 → 키 원문 순으로 찾는다.
- {0} {1} … 자리 표시자를 인자로 치환한다.
Looks up a translation. (static)
- Searches in the order current language → fallback language → key source text.
- Substitutes the {0} {1} … placeholders with the arguments.
- keystring
- 번역 키. (원문)Translation key. (source text)
- ...substitutions...*
- {0}, {1} 자리에 차례로 넣을 값. (String 으로 바꾼다)Values put into {0}, {1} in order. (converted with String)
반환Returnsstring 번역문. 표에 없으면 키 원문에 자리만 채운 글.The translated text. If not in the table, the key text with placeholders filled.
짝Counterpart: vanilla-engine Localization.text
배열형 번역 조회. (정적, 대사 목록처럼 여러 줄인 항목)
Looks up an array translation. (static, for multi-line entries like dialogue lists)
- keystring
- 번역 키. (원문)Translation key. (source text)
반환Returnsstring[] 번역문 목록. (글 하나면 그것만 담은 배열, 없으면 [키])List of translated texts. (an array with just that text if it is a single string, [key] if missing)
짝Counterpart: vanilla-engine Localization.text_list
원시 조회. (정적)
Raw lookup. (static)
- keystring
- 번역 키. (원문)Translation key. (source text)
반환Returnsstring | string[] | undefined 현재 언어 표, 폴백 언어 표 차례로 찾은 값. 없으면 undefined.Value found in the current language table, then the fallback table. undefined if missing.
짝Counterpart: vanilla-engine Localization.lookup
모든 테이블 비우기. (정적, 테스트용)
Clears all tables. (static, for testing)
짝Counterpart: vanilla-engine Localization.clear